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