1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * This file is part of MoleCuilder.
|
---|
8 | *
|
---|
9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * PcpParser_helper.cpp
|
---|
25 | *
|
---|
26 | * Created on: Oct 27, 2011
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "CodePatterns/MemDebug.hpp"
|
---|
36 |
|
---|
37 | #include <boost/tokenizer.hpp>
|
---|
38 | #include <map>
|
---|
39 | #include <sstream>
|
---|
40 | #include <string>
|
---|
41 | #include <vector>
|
---|
42 |
|
---|
43 | #include "CodePatterns/Log.hpp"
|
---|
44 | #include "Element/element.hpp"
|
---|
45 | #include "Element/periodentafel.hpp"
|
---|
46 | #include "Helpers/helpers.hpp"
|
---|
47 | #include "molecule.hpp"
|
---|
48 | #include "Parser/ConfigFileBuffer.hpp"
|
---|
49 |
|
---|
50 | /** Reads parameter from a parsed file.
|
---|
51 | * The file is either parsed for a certain keyword or if null is given for
|
---|
52 | * the value in row yth and column xth. If the keyword was necessity#critical,
|
---|
53 | * then an error is thrown and the programme aborted.
|
---|
54 | * \warning value is modified (both in contents and position)!
|
---|
55 | * \param verbose 1 - print found value to stderr, 0 - don't
|
---|
56 | * \param *file file to be parsed
|
---|
57 | * \param name Name of value in file (at least 3 chars!)
|
---|
58 | * \param sequential 1 - do not reset file pointer to begin of file, 0 - set to beginning
|
---|
59 | * (if file is sequentially parsed this can be way faster! However, beware of multiple values per line, as whole line is read -
|
---|
60 | * best approach: 0 0 0 1 (not resetted only on last value of line) - and of yth, which is now
|
---|
61 | * counted from this unresetted position!)
|
---|
62 | * \param xth Which among a number of parameters it is (in grid case it's row number as grid is read as a whole!)
|
---|
63 | * \param yth In grid case specifying column number, otherwise the yth \a name matching line
|
---|
64 | * \param type Type of the Parameter to be read
|
---|
65 | * \param value address of the value to be read (must have been allocated)
|
---|
66 | * \param repetition determines, if the keyword appears multiply in the config file, which repetition shall be parsed, i.e. 1 if not multiply
|
---|
67 | * \param critical necessity of this keyword being specified (optional, critical)
|
---|
68 | * \return 1 - found, 0 - not found
|
---|
69 | * \note Routine is taken from the pcp project and hack-a-slack adapted to C++
|
---|
70 | */
|
---|
71 | int ParseForParameter(const int verbose, ifstream * const file, const char * const name, const int sequential, const int xth, const int yth, const int type, void * value, const int repetition, const int critical) {
|
---|
72 | int i = 0;
|
---|
73 | int j = 0; // loop variables
|
---|
74 | int length = 0;
|
---|
75 | int maxlength = -1;
|
---|
76 | long file_position = file->tellg(); // mark current position
|
---|
77 | char *dummy1 = NULL;
|
---|
78 | char *dummy = NULL;
|
---|
79 | char free_dummy[MAXSTRINGSIZE]; // pointers in the line that is read in per step
|
---|
80 | dummy1 = free_dummy;
|
---|
81 |
|
---|
82 | //fprintf(stderr,"Parsing for %s\n",name);
|
---|
83 | if (repetition == 0)
|
---|
84 | //Error(SomeError, "ParseForParameter(): argument repetition must not be 0!");
|
---|
85 | return 0;
|
---|
86 |
|
---|
87 | int line = 0; // marks line where parameter was found
|
---|
88 | int found = (type >= grid) ? 0 : (-yth + 1); // marks if yth parameter name was found
|
---|
89 | while((found != repetition)) {
|
---|
90 | dummy1 = dummy = free_dummy;
|
---|
91 | do {
|
---|
92 | file->getline(dummy1, 256); // Read the whole line
|
---|
93 | if (file->eof()) {
|
---|
94 | if ((critical) && (found == 0)) {
|
---|
95 | //Error(InitReading, name);
|
---|
96 | fprintf(stderr,"Error:InitReading, critical %s not found\n", name);
|
---|
97 | exit(255);
|
---|
98 | } else {
|
---|
99 | //if (!sequential)
|
---|
100 | file->clear();
|
---|
101 | file->seekg(file_position, ios::beg); // rewind to start position
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | line++;
|
---|
106 | } while (dummy != NULL && dummy1 != NULL && ((dummy1[0] == '#') || (dummy1[0] == '\0'))); // skip commentary and empty lines
|
---|
107 |
|
---|
108 | // C++ getline removes newline at end, thus re-add
|
---|
109 | if ((dummy1 != NULL) && (strchr(dummy1,'\n') == NULL)) {
|
---|
110 | i = strlen(dummy1);
|
---|
111 | dummy1[i] = '\n';
|
---|
112 | dummy1[i+1] = '\0';
|
---|
113 | }
|
---|
114 | //fprintf(stderr,"line %i ends at %i, newline at %i\n", line, strlen(dummy1), strchr(dummy1,'\n')-free_dummy);
|
---|
115 |
|
---|
116 | if (dummy1 == NULL) {
|
---|
117 | if (verbose) fprintf(stderr,"Error reading line %i\n",line);
|
---|
118 | } else {
|
---|
119 | //fprintf(stderr,"Now parsing the line %i: %s\n", line, dummy1);
|
---|
120 | }
|
---|
121 | // Seek for possible end of keyword on line if given ...
|
---|
122 | if (name != NULL) {
|
---|
123 | dummy = strchr(dummy1,'\t'); // set dummy on first tab or space which ever's nearer
|
---|
124 | if (dummy == NULL) {
|
---|
125 | dummy = strchr(dummy1, ' '); // if not found seek for space
|
---|
126 | while ((dummy != NULL) && ((*dummy == '\t') || (*dummy == ' '))) // skip some more tabs and spaces if necessary
|
---|
127 | dummy++;
|
---|
128 | }
|
---|
129 | if (dummy == NULL) {
|
---|
130 | dummy = strchr(dummy1, '\n'); // set on line end then (whole line = keyword)
|
---|
131 | //fprintf(stderr,"Error: Cannot find tabs or spaces on line %i in search for %s\n", line, name);
|
---|
132 | //Error(FileOpenParams, NULL);
|
---|
133 | } else {
|
---|
134 | //fprintf(stderr,"found tab at %i\n",(char *)dummy-(char *)dummy1);
|
---|
135 | }
|
---|
136 | } else dummy = dummy1;
|
---|
137 | // ... and check if it is the keyword!
|
---|
138 | //fprintf(stderr,"name %p, dummy %i/%c, dummy1 %i/%c, strlen(name) %i\n", &name, dummy, *dummy, dummy1, *dummy1, strlen(name));
|
---|
139 | if ((name == NULL) || (((dummy-dummy1 >= 3) && (strncmp(dummy1, name, strlen(name)) == 0)) && ((unsigned int)(dummy-dummy1) == strlen(name)))) {
|
---|
140 | found++; // found the parameter!
|
---|
141 | //fprintf(stderr,"found %s at line %i between %i and %i\n", name, line, dummy1, dummy);
|
---|
142 |
|
---|
143 | if (found == repetition) {
|
---|
144 | for (i=0;i<xth;i++) { // i = rows
|
---|
145 | if (type >= grid) {
|
---|
146 | // grid structure means that grid starts on the next line, not right after keyword
|
---|
147 | dummy1 = dummy = free_dummy;
|
---|
148 | do {
|
---|
149 | file->getline(dummy1, 256); // Read the whole line, skip commentary and empty ones
|
---|
150 | if (file->eof()) {
|
---|
151 | if ((critical) && (found == 0)) {
|
---|
152 | //Error(InitReading, name);
|
---|
153 | fprintf(stderr,"Error:InitReading, critical %s not found\n", name);
|
---|
154 | exit(255);
|
---|
155 | } else {
|
---|
156 | //if (!sequential)
|
---|
157 | file->clear();
|
---|
158 | file->seekg(file_position, ios::beg); // rewind to start position
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | line++;
|
---|
163 | } while ((dummy1[0] == '#') || (dummy1[0] == '\n'));
|
---|
164 | if (dummy1 == NULL){
|
---|
165 | if (verbose) fprintf(stderr,"Error reading line %i\n", line);
|
---|
166 | } else {
|
---|
167 | //fprintf(stderr,"Reading next line %i: %s\n", line, dummy1);
|
---|
168 | }
|
---|
169 | } else { // simple int, strings or doubles start in the same line
|
---|
170 | while ((*dummy == '\t') || (*dummy == ' ')) // skip interjacent tabs and spaces
|
---|
171 | dummy++;
|
---|
172 | }
|
---|
173 | // C++ getline removes newline at end, thus re-add
|
---|
174 | if ((dummy1 != NULL) && (strchr(dummy1,'\n') == NULL)) {
|
---|
175 | j = strlen(dummy1);
|
---|
176 | dummy1[j] = '\n';
|
---|
177 | dummy1[j+1] = '\0';
|
---|
178 | }
|
---|
179 |
|
---|
180 | int start = (type >= grid) ? 0 : yth-1 ;
|
---|
181 | for (j=start;j<yth;j++) { // j = columns
|
---|
182 | // check for lower triangular area and upper triangular area
|
---|
183 | if ( ((i > j) && (type == upper_trigrid)) || ((j > i) && (type == lower_trigrid))) {
|
---|
184 | *((double *)value) = 0.0;
|
---|
185 | fprintf(stderr,"%f\t",*((double *)value));
|
---|
186 | value = (void *)((long)value + sizeof(double));
|
---|
187 | //value += sizeof(double);
|
---|
188 | } else {
|
---|
189 | // otherwise we must skip all interjacent tabs and spaces and find next value
|
---|
190 | dummy1 = dummy;
|
---|
191 | dummy = strchr(dummy1, '\t'); // seek for tab or space
|
---|
192 | if (dummy == NULL)
|
---|
193 | dummy = strchr(dummy1, ' '); // if not found seek for space
|
---|
194 | if (dummy == NULL) { // if still zero returned ...
|
---|
195 | dummy = strchr(dummy1, '\n'); // ... at line end then
|
---|
196 | if ((j < yth-1) && (type < 4)) { // check if xth value or not yet
|
---|
197 | if (critical) {
|
---|
198 | if (verbose) fprintf(stderr,"Error: EoL at %i and still missing %i value(s) for parameter %s\n", line, yth-j, name);
|
---|
199 | //return 0;
|
---|
200 | exit(255);
|
---|
201 | //Error(FileOpenParams, NULL);
|
---|
202 | } else {
|
---|
203 | //if (!sequential)
|
---|
204 | file->clear();
|
---|
205 | file->seekg(file_position, ios::beg); // rewind to start position
|
---|
206 | return 0;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | } else {
|
---|
210 | //fprintf(stderr,"found tab at %i\n",(char *)dummy-(char *)free_dummy);
|
---|
211 | }
|
---|
212 | if (*dummy1 == '#') {
|
---|
213 | // found comment, skipping rest of line
|
---|
214 | //if (verbose) fprintf(stderr,"Error: '#' at %i and still missing %i value(s) for parameter %s\n", line, yth-j, name);
|
---|
215 | if (!sequential) { // here we need it!
|
---|
216 | file->seekg(file_position, ios::beg); // rewind to start position
|
---|
217 | }
|
---|
218 | return 0;
|
---|
219 | }
|
---|
220 | //fprintf(stderr,"value from %i to %i\n",(char *)dummy1-(char *)free_dummy,(char *)dummy-(char *)free_dummy);
|
---|
221 | switch(type) {
|
---|
222 | case (row_int):
|
---|
223 | *((int *)value) = atoi(dummy1);
|
---|
224 | if ((verbose) && (i==0) && (j==0)) fprintf(stderr,"%s = ", name);
|
---|
225 | if (verbose) fprintf(stderr,"%i\t",*((int *)value));
|
---|
226 | value = (void *)((long)value + sizeof(int));
|
---|
227 | //value += sizeof(int);
|
---|
228 | break;
|
---|
229 | case(row_double):
|
---|
230 | case(grid):
|
---|
231 | case(lower_trigrid):
|
---|
232 | case(upper_trigrid):
|
---|
233 | *((double *)value) = atof(dummy1);
|
---|
234 | if ((verbose) && (i==0) && (j==0)) fprintf(stderr,"%s = ", name);
|
---|
235 | if (verbose) fprintf(stderr,"%lg\t",*((double *)value));
|
---|
236 | value = (void *)((long)value + sizeof(double));
|
---|
237 | //value += sizeof(double);
|
---|
238 | break;
|
---|
239 | case(double_type):
|
---|
240 | *((double *)value) = atof(dummy1);
|
---|
241 | if ((verbose) && (i == xth-1)) fprintf(stderr,"%s = %lg\n", name, *((double *) value));
|
---|
242 | //value += sizeof(double);
|
---|
243 | break;
|
---|
244 | case(int_type):
|
---|
245 | *((int *)value) = atoi(dummy1);
|
---|
246 | if ((verbose) && (i == xth-1)) fprintf(stderr,"%s = %i\n", name, *((int *) value));
|
---|
247 | //value += sizeof(int);
|
---|
248 | break;
|
---|
249 | default:
|
---|
250 | case(string_type):
|
---|
251 | if (value != NULL) {
|
---|
252 | //if (maxlength == -1) maxlength = strlen((char *)value); // get maximum size of string array
|
---|
253 | maxlength = MAXSTRINGSIZE;
|
---|
254 | length = maxlength > (dummy-dummy1) ? (dummy-dummy1) : maxlength; // cap at maximum
|
---|
255 | strncpy((char *)value, dummy1, length); // copy as much
|
---|
256 | ((char *)value)[length] = '\0'; // and set end marker
|
---|
257 | if ((verbose) && (i == xth-1)) fprintf(stderr,"%s is '%s' (%i chars)\n",name,((char *) value), length);
|
---|
258 | //value += sizeof(char);
|
---|
259 | } else {
|
---|
260 | }
|
---|
261 | break;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | while (*dummy == '\t')
|
---|
265 | dummy++;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 | if ((type >= row_int) && (verbose))
|
---|
272 | fprintf(stderr,"\n");
|
---|
273 | if (!sequential) {
|
---|
274 | file->clear();
|
---|
275 | file->seekg(file_position, ios::beg); // rewind to start position
|
---|
276 | }
|
---|
277 | //fprintf(stderr, "End of Parsing\n\n");
|
---|
278 |
|
---|
279 | return (found); // true if found, false if not
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | /** Reads parameter from a parsed file.
|
---|
284 | * The file is either parsed for a certain keyword or if null is given for
|
---|
285 | * the value in row yth and column xth. If the keyword was necessity#critical,
|
---|
286 | * then an error is thrown and the programme aborted.
|
---|
287 | * \warning value is modified (both in contents and position)!
|
---|
288 | * \param verbose 1 - print found value to stderr, 0 - don't
|
---|
289 | * \param *FileBuffer pointer to buffer structure
|
---|
290 | * \param name Name of value in file (at least 3 chars!)
|
---|
291 | * \param sequential 1 - do not reset file pointer to begin of file, 0 - set to beginning
|
---|
292 | * (if file is sequentially parsed this can be way faster! However, beware of multiple values per line, as whole line is read -
|
---|
293 | * best approach: 0 0 0 1 (not resetted only on last value of line) - and of yth, which is now
|
---|
294 | * counted from this unresetted position!)
|
---|
295 | * \param xth Which among a number of parameters it is (in grid case it's row number as grid is read as a whole!)
|
---|
296 | * \param yth In grid case specifying column number, otherwise the yth \a name matching line
|
---|
297 | * \param type Type of the Parameter to be read
|
---|
298 | * \param value address of the value to be read (must have been allocated)
|
---|
299 | * \param repetition determines, if the keyword appears multiply in the config file, which repetition shall be parsed, i.e. 1 if not multiply
|
---|
300 | * \param critical necessity of this keyword being specified (optional, critical)
|
---|
301 | * \return 1 - found, 0 - not found
|
---|
302 | * \note Routine is taken from the pcp project and hack-a-slack adapted to C++
|
---|
303 | */
|
---|
304 | int ParseForParameter(const int verbose, struct ConfigFileBuffer * const FileBuffer, const char * const name, const int sequential, const int xth, const int yth, const int type, void * value, const int repetition, const int critical) {
|
---|
305 | int i = 0;
|
---|
306 | int j = 0; // loop variables
|
---|
307 | int length = 0;
|
---|
308 | int maxlength = -1;
|
---|
309 | int OldCurrentLine = FileBuffer->CurrentLine;
|
---|
310 | char *dummy1 = NULL;
|
---|
311 | char *dummy = NULL; // pointers in the line that is read in per step
|
---|
312 | char *free_dummy = NULL;
|
---|
313 |
|
---|
314 | if (verbose) fprintf(stderr,"Begin of Parsing for %s\n",name);
|
---|
315 | if (repetition == 0)
|
---|
316 | //Error(SomeError, "ParseForParameter(): argument repetition must not be 0!");
|
---|
317 | return 0;
|
---|
318 |
|
---|
319 | int found = (type >= grid) ? 0 : (-yth + 1); // marks if yth parameter name was found
|
---|
320 | while((found != repetition)) {
|
---|
321 | dummy1 = dummy = NULL;
|
---|
322 | do {
|
---|
323 | if (FileBuffer->CurrentLine < FileBuffer->NoLines)
|
---|
324 | free_dummy = dummy1 = FileBuffer->buffer[ FileBuffer->LineMapping[FileBuffer->CurrentLine++] ];
|
---|
325 | if (FileBuffer->CurrentLine >= FileBuffer->NoLines) {
|
---|
326 | if ((critical) && (found == 0)) {
|
---|
327 | //Error(InitReading, name);
|
---|
328 | fprintf(stderr,"Error:InitReading, critical %s not found\n", name);
|
---|
329 | return 0;
|
---|
330 | } else {
|
---|
331 | //fprintf(stdout,"Rewinding to OldCurrentLine due to search till end of file.\n");
|
---|
332 | FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
|
---|
333 | return 0;
|
---|
334 | }
|
---|
335 | }
|
---|
336 | if (dummy1 == NULL) {
|
---|
337 | if (verbose) fprintf(stderr,"Error reading line %i\n",FileBuffer->CurrentLine);
|
---|
338 | } else {
|
---|
339 | if (verbose) fprintf(stderr,"Now parsing the line %i: %s\n", FileBuffer->CurrentLine, dummy1);
|
---|
340 | }
|
---|
341 | //FileBuffer->CurrentLine++;
|
---|
342 | } while (dummy1 != NULL && ((dummy1[0] == '#') || (dummy1[0] == '\0'))); // skip commentary and empty lines
|
---|
343 |
|
---|
344 | // Seek for possible end of keyword on line if given ...
|
---|
345 | if (name != NULL) {
|
---|
346 | dummy = strchr(dummy1,'\t'); // set dummy on first tab or space which ever's nearer
|
---|
347 | if (dummy == NULL) {
|
---|
348 | dummy = strchr(dummy1, ' '); // if not found seek for space
|
---|
349 | while ((dummy != NULL) && ((*dummy == '\t') || (*dummy == ' '))) // skip some more tabs and spaces if necessary
|
---|
350 | dummy++;
|
---|
351 | }
|
---|
352 | if (dummy == NULL) {
|
---|
353 | dummy = strchr(dummy1, '\n'); // set on line end then (whole line = keyword)
|
---|
354 | //fprintf(stderr,"Error: Cannot find tabs or spaces on line %i in search for %s\n", line, name);
|
---|
355 | //Error(FileOpenParams, NULL);
|
---|
356 | } else {
|
---|
357 | if (verbose) fprintf(stderr,"found tab at line %i at position %li\n",FileBuffer->CurrentLine, (char *)dummy-(char *)dummy1);
|
---|
358 | }
|
---|
359 | } else dummy = dummy1;
|
---|
360 | // ... and check if it is the keyword!
|
---|
361 | //fprintf(stderr,"name %p, dummy %i/%c, dummy1 %i/%c, strlen(name) %i\n", &name, dummy, *dummy, dummy1, *dummy1, strlen(name));
|
---|
362 | if ((name == NULL) || (((dummy-dummy1 >= 3) && (strncmp(dummy1, name, strlen(name)) == 0)) && ((unsigned int)(dummy-dummy1) == strlen(name)))) {
|
---|
363 | found++; // found the parameter!
|
---|
364 | if (verbose) fprintf(stderr,"found %s at line %i between %li and %li\n", name, FileBuffer->CurrentLine, (unsigned long)dummy1, (unsigned long)dummy);
|
---|
365 |
|
---|
366 | if (found == repetition) {
|
---|
367 | for (i=0;i<xth;i++) { // i = rows
|
---|
368 | if (type >= grid) {
|
---|
369 | // grid structure means that grid starts on the next line, not right after keyword
|
---|
370 | dummy1 = dummy = NULL;
|
---|
371 | do {
|
---|
372 | dummy1 = FileBuffer->buffer[ FileBuffer->LineMapping[ FileBuffer->CurrentLine++] ];
|
---|
373 | if (FileBuffer->CurrentLine >= FileBuffer->NoLines) {
|
---|
374 | if ((critical) && (found == 0)) {
|
---|
375 | //Error(InitReading, name);
|
---|
376 | fprintf(stderr,"Error:InitReading, critical %s not found\n", name);
|
---|
377 | exit(255);
|
---|
378 | } else {
|
---|
379 | //fprintf(stdout,"Rewinding to OldCurrentLine due to search till end of line.\n");
|
---|
380 | FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
|
---|
381 | return 0;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | if (dummy1 == NULL) {
|
---|
385 | if (verbose) fprintf(stderr,"Error reading line %i\n", FileBuffer->CurrentLine);
|
---|
386 | } else {
|
---|
387 | if (verbose) fprintf(stderr,"Reading next line %i: %s\n", FileBuffer->CurrentLine, dummy1);
|
---|
388 | }
|
---|
389 | //FileBuffer->CurrentLine++;
|
---|
390 | } while ((dummy1 != NULL) && ((dummy1[0] == '#') || (dummy1[0] == '\n')));
|
---|
391 | dummy = dummy1;
|
---|
392 | } else { // simple int, strings or doubles start in the same line
|
---|
393 | while ((*dummy == '\t') || (*dummy == ' ')) // skip interjacent tabs and spaces
|
---|
394 | dummy++;
|
---|
395 | }
|
---|
396 |
|
---|
397 | for (j=((type >= grid) ? 0 : yth-1);j<yth;j++) { // j = columns
|
---|
398 | // check for lower triangular area and upper triangular area
|
---|
399 | if ( ((i > j) && (type == upper_trigrid)) || ((j > i) && (type == lower_trigrid))) {
|
---|
400 | *((double *)value) = 0.0;
|
---|
401 | fprintf(stderr,"%f\t",*((double *)value));
|
---|
402 | value = (void *)((long)value + sizeof(double));
|
---|
403 | //value += sizeof(double);
|
---|
404 | } else {
|
---|
405 | // otherwise we must skip all interjacent tabs and spaces and find next value
|
---|
406 | dummy1 = dummy;
|
---|
407 | dummy = strchr(dummy1, '\t'); // seek for tab or space
|
---|
408 | if (dummy == NULL)
|
---|
409 | dummy = strchr(dummy1, ' '); // if not found seek for space
|
---|
410 | if (dummy == NULL) { // if still zero returned ...
|
---|
411 | dummy = strchr(dummy1, '\n'); // ... at line end then
|
---|
412 | if ((j < yth-1) && (type < 4)) { // check if xth value or not yet
|
---|
413 | if (critical) {
|
---|
414 | if (verbose) fprintf(stderr,"Error: EoL at %i and still missing %i value(s) for parameter %s\n", FileBuffer->CurrentLine, yth-j, name);
|
---|
415 | //return 0;
|
---|
416 | exit(255);
|
---|
417 | //Error(FileOpenParams, NULL);
|
---|
418 | } else {
|
---|
419 | if (!sequential) { // here we need it!
|
---|
420 | //fprintf(stdout,"Rewinding to OldCurrentLine due to end of line and sequential %d.\n", sequential);
|
---|
421 | FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
|
---|
422 | }
|
---|
423 | return 0;
|
---|
424 | }
|
---|
425 | }
|
---|
426 | } else {
|
---|
427 | if (verbose) fprintf(stderr,"found tab at line %i at position %li\n",FileBuffer->CurrentLine, (char *)dummy-(char *)free_dummy);
|
---|
428 | }
|
---|
429 | if (*dummy1 == '#') {
|
---|
430 | // found comment, skipping rest of line
|
---|
431 | //if (verbose) fprintf(stderr,"Error: '#' at %i and still missing %i value(s) for parameter %s\n", line, yth-j, name);
|
---|
432 | if (!sequential) { // here we need it!
|
---|
433 | //fprintf(stdout,"Rewinding to OldCurrentLine due to comment and sequential %d.\n", sequential);
|
---|
434 | FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
|
---|
435 | }
|
---|
436 | return 0;
|
---|
437 | }
|
---|
438 | if (verbose) fprintf(stderr,"value from %li to %li\n",(char *)dummy1-(char *)free_dummy,(char *)dummy-(char *)free_dummy);
|
---|
439 | switch(type) {
|
---|
440 | case (row_int):
|
---|
441 | *((int *)value) = atoi(dummy1);
|
---|
442 | if ((verbose) && (i==0) && (j==0)) fprintf(stderr,"%s = ", name);
|
---|
443 | if (verbose) fprintf(stderr,"%i\t",*((int *)value));
|
---|
444 | value = (void *)((long)value + sizeof(int));
|
---|
445 | //value += sizeof(int);
|
---|
446 | break;
|
---|
447 | case(row_double):
|
---|
448 | case(grid):
|
---|
449 | case(lower_trigrid):
|
---|
450 | case(upper_trigrid):
|
---|
451 | *((double *)value) = atof(dummy1);
|
---|
452 | if ((verbose) && (i==0) && (j==0)) fprintf(stderr,"%s = ", name);
|
---|
453 | if (verbose) fprintf(stderr,"%lg\t",*((double *)value));
|
---|
454 | value = (void *)((long)value + sizeof(double));
|
---|
455 | //value += sizeof(double);
|
---|
456 | break;
|
---|
457 | case(double_type):
|
---|
458 | *((double *)value) = atof(dummy1);
|
---|
459 | if ((verbose) && (i == xth-1)) fprintf(stderr,"%s = %lg\n", name, *((double *) value));
|
---|
460 | //value += sizeof(double);
|
---|
461 | break;
|
---|
462 | case(int_type):
|
---|
463 | *((int *)value) = atoi(dummy1);
|
---|
464 | if ((verbose) && (i == xth-1)) fprintf(stderr,"%s = %i\n", name, *((int *) value));
|
---|
465 | //value += sizeof(int);
|
---|
466 | break;
|
---|
467 | default:
|
---|
468 | case(string_type):
|
---|
469 | if (value != NULL) {
|
---|
470 | //if (maxlength == -1) maxlength = strlen((char *)value); // get maximum size of string array
|
---|
471 | maxlength = MAXSTRINGSIZE;
|
---|
472 | length = maxlength > (dummy-dummy1) ? (dummy-dummy1) : maxlength; // cap at maximum
|
---|
473 | strncpy((char *)value, dummy1, length); // copy as much
|
---|
474 | ((char *)value)[length] = '\0'; // and set end marker
|
---|
475 | if ((verbose) && (i == xth-1)) fprintf(stderr,"%s is '%s' (%i chars)\n",name,((char *) value), length);
|
---|
476 | //value += sizeof(char);
|
---|
477 | } else {
|
---|
478 | }
|
---|
479 | break;
|
---|
480 | }
|
---|
481 | }
|
---|
482 | while (*dummy == '\t')
|
---|
483 | dummy++;
|
---|
484 | }
|
---|
485 | }
|
---|
486 | }
|
---|
487 | }
|
---|
488 | }
|
---|
489 | if ((type >= row_int) && (verbose)) fprintf(stderr,"\n");
|
---|
490 | if (!sequential) {
|
---|
491 | //fprintf(stdout,"Rewinding to OldCurrentLine due to sequential %d.\n", sequential);
|
---|
492 | FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
|
---|
493 | }
|
---|
494 | if (verbose) fprintf(stderr, "End of Parsing for %s\n\n",name);
|
---|
495 |
|
---|
496 | return (found); // true if found, false if not
|
---|
497 | }
|
---|
498 |
|
---|
499 | /** Loads a molecule from a ConfigFileBuffer.
|
---|
500 | * \param *mol molecule to load
|
---|
501 | * \param *FileBuffer ConfigFileBuffer to use
|
---|
502 | * \param *periode periodentafel for finding elements
|
---|
503 | * \param FastParsing whether to parse trajectories or not
|
---|
504 | */
|
---|
505 | void LoadMolecule(molecule * const &mol, struct ConfigFileBuffer * const &FileBuffer, const periodentafel * const periode, const bool FastParsing)
|
---|
506 | {
|
---|
507 | int MaxTypes = 0;
|
---|
508 | const element *elementhash[MAX_ELEMENTS];
|
---|
509 | char name[MAXSTRINGSIZE];
|
---|
510 | int Z = -1;
|
---|
511 | int No[MAX_ELEMENTS];
|
---|
512 | int verbose = DoLog(4);
|
---|
513 | double value[3];
|
---|
514 |
|
---|
515 | if (mol == NULL) {
|
---|
516 | ELOG(0, "Molecule is not allocated in LoadMolecule(), exit.");
|
---|
517 | performCriticalExit();
|
---|
518 | }
|
---|
519 |
|
---|
520 | ParseForParameter(verbose,FileBuffer,"MaxTypes", 0, 1, 1, int_type, &(MaxTypes), 1, critical);
|
---|
521 | if (MaxTypes == 0) {
|
---|
522 | ELOG(1, "There are no atoms according to MaxTypes in this config file." << endl);
|
---|
523 | //performCriticalExit();
|
---|
524 | } else {
|
---|
525 | // prescan number of ions per type
|
---|
526 | LOG(0, "STATUS: Prescanning ions per type: " << endl);
|
---|
527 | int NoAtoms = 0;
|
---|
528 | for (int i=0; i < MaxTypes; i++) {
|
---|
529 | sprintf(name,"Ion_Type%i",i+1);
|
---|
530 | ParseForParameter(verbose,FileBuffer, (const char*)name, 0, 1, 1, int_type, &No[i], 1, critical);
|
---|
531 | ParseForParameter(verbose,FileBuffer, name, 0, 2, 1, int_type, &Z, 1, critical);
|
---|
532 | elementhash[i] = periode->FindElement(Z);
|
---|
533 | LOG(1, i << ". Z = " << elementhash[i]->getAtomicNumber() << " with " << No[i] << " ions.");
|
---|
534 | NoAtoms += No[i];
|
---|
535 | }
|
---|
536 | int repetition = -1; // which repeated keyword shall be read
|
---|
537 |
|
---|
538 | // sort the lines via the LineMapping
|
---|
539 | sprintf(name,"Ion_Type%i",MaxTypes);
|
---|
540 | if (!ParseForParameter(verbose,FileBuffer, (const char*)name, 1, 1, 1, int_type, &value[0], 1, critical)) {
|
---|
541 | ELOG(0, "There are no atoms in the config file!" << endl);
|
---|
542 | performCriticalExit();
|
---|
543 | return;
|
---|
544 | }
|
---|
545 |
|
---|
546 | FileBuffer->CurrentLine++; // skip to next line
|
---|
547 | FileBuffer->MapIonTypesInBuffer(NoAtoms);
|
---|
548 | for (int i=FileBuffer->CurrentLine; i<FileBuffer->NoLines;++i) {
|
---|
549 | LOG(4, FileBuffer->buffer[ FileBuffer->LineMapping[i] ]);
|
---|
550 | }
|
---|
551 |
|
---|
552 | std::vector<map<int, atom *> > AtomList(MaxTypes);
|
---|
553 | map<int, atom *> LinearList;
|
---|
554 | atom *neues = NULL;
|
---|
555 | Vector tempVector;
|
---|
556 | int _fixedion;
|
---|
557 |
|
---|
558 | typedef boost::tokenizer<boost::char_separator<char> >
|
---|
559 | tokenizer;
|
---|
560 | boost::char_separator<char> sep("\t ");
|
---|
561 | ConvertTo<double> toDouble;
|
---|
562 | ConvertTo<int> toInt;
|
---|
563 |
|
---|
564 | for (int i=0; i < MaxTypes; i++) {
|
---|
565 | for(int j=0;j<No[i];j++) {
|
---|
566 | int step = 0;
|
---|
567 | std::stringstream keyword_stream;
|
---|
568 | keyword_stream << "Ion_Type" << i+1 << "_" << j+1;
|
---|
569 | const std::string keyword = keyword_stream.str();
|
---|
570 | LOG(3, "INFO: Parsing for " << keyword << "." << std::endl);
|
---|
571 | while (true) {
|
---|
572 | const std::string line(FileBuffer->buffer[ FileBuffer->LineMapping[FileBuffer->CurrentLine] ]);
|
---|
573 | const std::string line_without_comment = line.substr(0,line.find("#"));
|
---|
574 | tokenizer tokens(line_without_comment, sep);
|
---|
575 | if (tokens.begin() != tokens.end()) {
|
---|
576 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
577 | const std::string token = *tok_iter++;
|
---|
578 | if (token == keyword) {
|
---|
579 | LOG(3, "INFO: Found keyword " << keyword << " in line " << FileBuffer->CurrentLine << std::endl);
|
---|
580 | if (step == 0) {
|
---|
581 | neues = World::getInstance().createAtom();
|
---|
582 | AtomList[i][j] = neues;
|
---|
583 | LOG(4, "Filling LinearList [ (FileBuffer->LineMapping[" << FileBuffer->CurrentLine << "]) = " << FileBuffer->LineMapping[FileBuffer->CurrentLine] << " with " << neues << endl);
|
---|
584 | LinearList[ FileBuffer->LineMapping[FileBuffer->CurrentLine] ] = neues;
|
---|
585 | neues->setType(elementhash[i]); // find element type
|
---|
586 | } else
|
---|
587 | neues = AtomList[i][j];
|
---|
588 |
|
---|
589 | // count tokens
|
---|
590 | size_t tokens_size = 0;
|
---|
591 | for (tokenizer::iterator tokiter = tokens.begin(); tokiter != tokens.end(); ++tokiter)
|
---|
592 | ++tokens_size;
|
---|
593 | LOG(3, "INFO: Line contains " << tokens_size << " tokens." << std::endl);
|
---|
594 | // and parse
|
---|
595 | tempVector.Zero();
|
---|
596 | if (tokens_size >= 5) { // only AtomicPosition and FixedIon
|
---|
597 | for (int i=0;i<NDIM;++i)
|
---|
598 | tempVector[i] = toDouble(*tok_iter++);
|
---|
599 | neues->setPositionAtStep(step, tempVector);
|
---|
600 | _fixedion = toInt(*tok_iter++);
|
---|
601 | neues->setFixedIon(_fixedion == 1);
|
---|
602 | LOG(3, "INFO: Parsing AtomicPosition " << tempVector << " and FixedIon " << _fixedion << "." << std::endl);
|
---|
603 | }
|
---|
604 | tempVector.Zero();
|
---|
605 | if (tokens_size >= 8) { // AtomicVelocity
|
---|
606 | for (int i=0;i<NDIM;++i)
|
---|
607 | tempVector[i] = toDouble(*tok_iter++);
|
---|
608 | LOG(3, "INFO: Parsing AtomicVelocity " << tempVector << "." << std::endl);
|
---|
609 | }
|
---|
610 | neues->setAtomicVelocityAtStep(step, tempVector);
|
---|
611 | tempVector.Zero();
|
---|
612 | if (tokens_size >= 11) { // AtomicForce
|
---|
613 | LOG(3, "INFO: Parsing AtomicForce" << std::endl);
|
---|
614 | for (int i=0;i<NDIM;++i)
|
---|
615 | tempVector[i] = toDouble(*tok_iter++);
|
---|
616 | }
|
---|
617 | neues->setAtomicForceAtStep(step, tempVector);
|
---|
618 | std::stringstream output;
|
---|
619 | output << "Parsed position of step " << (step+1) << ": ";
|
---|
620 | output << neues->getPositionAtStep(step); // next step
|
---|
621 | output << "\t";
|
---|
622 | output << (neues->getFixedIon() ? "true" : "false");
|
---|
623 | output << "\t";
|
---|
624 | output << neues->getAtomicVelocityAtStep(step); // next step
|
---|
625 | output << "\t";
|
---|
626 | output << neues->getAtomicForceAtStep(step); // next step
|
---|
627 | LOG(2, output.str());
|
---|
628 |
|
---|
629 | step++;
|
---|
630 | } else {
|
---|
631 | if ((repetition > step) || (repetition == -1))
|
---|
632 | repetition = step;
|
---|
633 | break;
|
---|
634 | }
|
---|
635 | }
|
---|
636 | FileBuffer->CurrentLine++;
|
---|
637 | }
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | if (repetition <= 1) // if onyl one step, desactivate use of trajectories
|
---|
642 | mol->MDSteps = 0;
|
---|
643 | else {
|
---|
644 | LOG(0, "Found " << repetition << " trajectory step(s).");
|
---|
645 | mol->MDSteps = repetition;
|
---|
646 | }
|
---|
647 |
|
---|
648 | // put atoms into the molecule in their original order
|
---|
649 | for(map<int, atom*>::iterator runner = LinearList.begin(); runner != LinearList.end(); ++runner) {
|
---|
650 | mol->AddAtom(runner->second);
|
---|
651 | }
|
---|
652 | }
|
---|
653 | }
|
---|