ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/InitializeFromFile.cpp
Revision: 206
Committed: Thu Dec 12 21:21:59 2002 UTC (21 years, 7 months ago) by chuckv
File size: 14497 byte(s)
Log Message:
Initial mpi io conversion.

File Contents

# Content
1 #include <iostream>
2 #include <cmath>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10
11 #include "ReadWrite.hpp"
12 #include "simError.h"
13
14
15 InitializeFromFile :: InitializeFromFile( char *in_name ){
16 #ifdef IS_MPI
17 if (worldRank == 0) {
18 #endif
19
20 c_in_file = fopen(in_name, "r");
21 if(c_in_file == NULL){
22 sprintf(painCave.errMsg,
23 "Cannot open file: %s\n", in_name);
24 painCave.isFatal = 1;
25 simError();
26 }
27
28 strcpy( c_in_name, in_name);
29 #ifdef IS_MPI
30 }
31 strcpy( checkPointMsg, "Infile opened for reading successfully." );
32 MPIcheckPoint();
33 #endif
34 return;
35 }
36
37 InitializeFromFile :: ~InitializeFromFile( ){
38 #ifdef IS_MPI
39 if (worldRank == 0) {
40 #endif
41 int error;
42 error = fclose( c_in_file );
43 if( error ){
44 sprintf( painCave.errMsg,
45 "Error closing %s\n", c_in_name );
46 simError();
47 }
48 #ifdef IS_MPI
49 }
50 strcpy( checkPointMsg, "Infile closed successfully." );
51 MPIcheckPoint();
52 #endif
53
54 return;
55 }
56
57
58 void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){
59
60 int i; // loop counter
61
62 int n_atoms; // the number of atoms
63 char read_buffer[2000]; //the line buffer for reading
64 #ifdef IS_MPI
65 char send_buffer[2000];
66 #endif
67
68 char *eof_test; // ptr to see when we reach the end of the file
69 char *foo; // the pointer to the current string token
70
71 double rx, ry, rz; // position place holders
72 double vx, vy, vz; // velocity placeholders
73 double q[4]; // the quaternions
74 double jx, jy, jz; // angular velocity placeholders;
75 double qSqr, qLength; // needed to normalize the quaternion vector.
76
77 entry_plug = the_entry_plug
78
79
80 #ifndef IS_MPI
81 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
82 if( eof_test == NULL ){
83 std::cerr << "error reading 1st line of" << c_in_name << "\n";
84 }
85
86 (void)sscanf(read_buffer, "%d", &n_atoms);
87
88 Atom **atoms = entry_plug->atoms;
89 DirectionalAtom* dAtom;
90
91 if( n_atoms != entry_plug->n_atoms ){
92 sprintf( painCave.errMsg,
93 "Initialize from File error. %s n_atoms, %d, "
94 "does not match the BASS file's n_atoms, %d.\n",
95 c_in_name, n_atoms, entry_plug->n_atoms );
96 painCave.isFatal = 1;
97 simError();
98 }
99
100 //read and toss the comment line
101
102 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
103 if(eof_test == NULL){
104 sprintf( painCave.errMsg,
105 "error in reading commment in %s\n", c_in_name);
106 painCave.isFatal = 1;
107 simError();
108 }
109
110 for( i=0; i < n_atoms; i++){
111
112 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
113 if(eof_test == NULL){
114 sprintf(painCave.errMsg,
115 "error in reading file %s\n"
116 "natoms = %d; index = %d\n"
117 "error reading the line from the file.\n",
118 c_in_name, n_atoms, i );
119 painCave.isFatal = 1;
120 simError();
121 }
122
123 foo = strtok(read_buffer, " ,;\t");
124
125 // check the atom name to the current atom
126
127 if( strcmp( foo, atoms[i]->getType() ) ){
128 sprintf( painCave.errMsg,
129 "Initialize from file error. Atom %s at index %d "
130 "in file %s does not"
131 " match the BASS atom %s.\n",
132 foo, i, c_in_name, atoms[i]->getType() );
133 painCave.isFatal = 1;
134 simError();
135 }
136
137 // get the positions
138
139 foo = strtok(NULL, " ,;\t");
140 if(foo == NULL){
141 sprintf( painCave.errMsg,
142 "error in reading postition x from %s\n"
143 "natoms = %d, index = %d\n",
144 c_in_name, n_atoms, i );
145 painCave.isFatal = 1;
146 simError();
147 }
148 (void)sscanf( foo, "%lf", &rx );
149
150 foo = strtok(NULL, " ,;\t");
151 if(foo == NULL){
152 sprintf( painCave.errMsg,
153 "error in reading postition y from %s\n"
154 "natoms = %d, index = %d\n",
155 c_in_name, n_atoms, i );
156 painCave.isFatal = 1;
157 simError();
158 }
159 (void)sscanf( foo, "%lf", &ry );
160
161 foo = strtok(NULL, " ,;\t");
162 if(foo == NULL){
163 sprintf( painCave.errMsg,
164 "error in reading postition z from %s\n"
165 "natoms = %d, index = %d\n",
166 c_in_name, n_atoms, i );
167 painCave.isFatal = 1;
168 simError();
169 }
170 (void)sscanf( foo, "%lf", &rz );
171
172 // get the velocities
173
174 foo = strtok(NULL, " ,;\t");
175 if(foo == NULL){
176 sprintf( painCave.errMsg,
177 "error in reading velocity x from %s\n"
178 "natoms = %d, index = %d\n",
179 c_in_name, n_atoms, i );
180 painCave.isFatal = 1;
181 simError();
182 }
183 (void)sscanf( foo, "%lf", &vx );
184
185 foo = strtok(NULL, " ,;\t");
186 if(foo == NULL){
187 sprintf( painCave.errMsg,
188 "error in reading velocity y from %s\n"
189 "natoms = %d, index = %d\n",
190 c_in_name, n_atoms, i );
191 painCave.isFatal = 1;
192 simError();
193 }
194 (void)sscanf( foo, "%lf", &vy );
195
196 foo = strtok(NULL, " ,;\t");
197 if(foo == NULL){
198 sprintf( painCave.errMsg,
199 "error in reading velocity z from %s\n"
200 "natoms = %d, index = %d\n",
201 c_in_name, n_atoms, i );
202 painCave.isFatal = 1;
203 simError();
204 }
205 (void)sscanf( foo, "%lf", &vz );
206
207
208 // get the quaternions
209
210 if( atoms[i]->isDirectional() ){
211
212 foo = strtok(NULL, " ,;\t");
213 if(foo == NULL){
214 sprintf(painCave.errMsg,
215 "error in reading quaternion 0 from %s\n"
216 "natoms = %d, index = %d\n",
217 c_in_name, n_atoms, i );
218 painCave.isFatal = 1;
219 simError();
220 }
221 (void)sscanf( foo, "%lf", &q[0] );
222
223 foo = strtok(NULL, " ,;\t");
224 if(foo == NULL){
225 sprintf( painCave.errMsg,
226 "error in reading quaternion 1 from %s\n"
227 "natoms = %d, index = %d\n",
228 c_in_name, n_atoms, i );
229 painCave.isFatal = 1;
230 simError();
231 }
232 (void)sscanf( foo, "%lf", &q[1] );
233
234 foo = strtok(NULL, " ,;\t");
235 if(foo == NULL){
236 sprintf( painCave.errMsg,
237 "error in reading quaternion 2 from %s\n"
238 "natoms = %d, index = %d\n",
239 c_in_name, n_atoms, i );
240 painCave.isFatal = 1;
241 simError();
242 }
243 (void)sscanf( foo, "%lf", &q[2] );
244
245 foo = strtok(NULL, " ,;\t");
246 if(foo == NULL){
247 sprintf( painCave.errMsg,
248 "error in reading quaternion 3 from %s\n"
249 "natoms = %d, index = %d\n",
250 c_in_name, n_atoms, i );
251 painCave.isFatal = 1;
252 simError();
253 }
254 (void)sscanf( foo, "%lf", &q[3] );
255
256 // get the angular velocities
257
258 foo = strtok(NULL, " ,;\t");
259 if(foo == NULL){
260 sprintf( painCave.errMsg,
261 "error in reading angular momentum jx from %s\n"
262 "natoms = %d, index = %d\n",
263 c_in_name, n_atoms, i );
264 painCave.isFatal = 1;
265 simError();
266 }
267 (void)sscanf( foo, "%lf", &jx );
268
269 foo = strtok(NULL, " ,;\t");
270 if(foo == NULL){
271 sprintf( painCave.errMsg,
272 "error in reading angular momentum jy from %s\n"
273 "natoms = %d, index = %d\n",
274 c_in_name, n_atoms, i );
275 painCave.isFatal = 1;
276 simError();
277 }
278 (void)sscanf( foo, "%lf", &jy );
279
280 foo = strtok(NULL, " ,;\t");
281 if(foo == NULL){
282 sprintf( painCave.errMsg,
283 "error in reading angular momentum jz from %s\n"
284 "natoms = %d, index = %d\n",
285 c_in_name, n_atoms, i );
286 painCave.isFatal = 1;
287 simError();
288 }
289 (void)sscanf( foo, "%lf", &jz );
290
291 dAtom = ( DirectionalAtom* )atoms[i];
292
293 // check that the quaternion vector is normalized
294
295 qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]);
296
297 qLength = sqrt( qSqr );
298 q[0] = q[0] / qLength;
299 q[1] = q[1] / qLength;
300 q[2] = q[2] / qLength;
301 q[3] = q[3] / qLength;
302
303 dAtom->setQ( q );
304
305 // add the angular velocities
306
307 dAtom->setJx( jx );
308 dAtom->setJy( jy );
309 dAtom->setJz( jz );
310 }
311
312 // add the positions and velocities to the atom
313
314 atoms[i]->setX( rx );
315 atoms[i]->setY( ry );
316 atoms[i]->setZ( rz );
317
318 atoms[i]->set_vx( vx );
319 atoms[i]->set_vy( vy );
320 atoms[i]->set_vz( vz );
321
322 }
323
324
325 // MPI Section of code..........
326 #else //IS_MPI
327
328
329
330
331 if (worldRank == 0) {
332 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
333 if( eof_test == NULL ){
334 sprintf( painCave.errMsg,
335 "Error reading 1st line of %d \n ",c_in_name);
336 painCave.isFatal = 1;
337 simError();
338 }
339
340 (void)sscanf(read_buffer, "%d", &n_atoms);
341
342 Atom **atoms = entry_plug->atoms;
343 DirectionalAtom* dAtom;
344
345 if( n_atoms != entry_plug->n_atoms ){
346 sprintf( painCave.errMsg,
347 "Initialize from File error. %s n_atoms, %d, "
348 "does not match the BASS file's n_atoms, %d.\n",
349 c_in_name, n_atoms, entry_plug->n_atoms );
350 painCave.isFatal = 1;
351 simError();
352 }
353
354 //read and toss the comment line
355
356 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
357 if(eof_test == NULL){
358 sprintf( painCave.errMsg,
359 "error in reading commment in %s\n", c_in_name);
360 painCave.isFatal = 1;
361 simError();
362 }
363 }
364 for( i=0; i < n_atoms; i++){
365
366 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
367 if(eof_test == NULL){
368 sprintf(painCave.errMsg,
369 "error in reading file %s\n"
370 "natoms = %d; index = %d\n"
371 "error reading the line from the file.\n",
372 c_in_name, n_atoms, i );
373 painCave.isFatal = 1;
374 simError();
375 }
376
377 foo = strtok(read_buffer, " ,;\t");
378
379 // check the atom name to the current atom
380
381 if( strcmp( foo, atoms[i]->getType() ) ){
382 sprintf( painCave.errMsg,
383 "Initialize from file error. Atom %s at index %d "
384 "in file %s does not"
385 " match the BASS atom %s.\n",
386 foo, i, c_in_name, atoms[i]->getType() );
387 painCave.isFatal = 1;
388 simError();
389 }
390
391 // get the positions
392
393 foo = strtok(NULL, " ,;\t");
394 if(foo == NULL){
395 sprintf( painCave.errMsg,
396 "error in reading postition x from %s\n"
397 "natoms = %d, index = %d\n",
398 c_in_name, n_atoms, i );
399 painCave.isFatal = 1;
400 simError();
401 }
402 (void)sscanf( foo, "%lf", &rx );
403
404 foo = strtok(NULL, " ,;\t");
405 if(foo == NULL){
406 sprintf( painCave.errMsg,
407 "error in reading postition y from %s\n"
408 "natoms = %d, index = %d\n",
409 c_in_name, n_atoms, i );
410 painCave.isFatal = 1;
411 simError();
412 }
413 (void)sscanf( foo, "%lf", &ry );
414
415 foo = strtok(NULL, " ,;\t");
416 if(foo == NULL){
417 sprintf( painCave.errMsg,
418 "error in reading postition z from %s\n"
419 "natoms = %d, index = %d\n",
420 c_in_name, n_atoms, i );
421 painCave.isFatal = 1;
422 simError();
423 }
424 (void)sscanf( foo, "%lf", &rz );
425
426 // get the velocities
427
428 foo = strtok(NULL, " ,;\t");
429 if(foo == NULL){
430 sprintf( painCave.errMsg,
431 "error in reading velocity x from %s\n"
432 "natoms = %d, index = %d\n",
433 c_in_name, n_atoms, i );
434 painCave.isFatal = 1;
435 simError();
436 }
437 (void)sscanf( foo, "%lf", &vx );
438
439 foo = strtok(NULL, " ,;\t");
440 if(foo == NULL){
441 sprintf( painCave.errMsg,
442 "error in reading velocity y from %s\n"
443 "natoms = %d, index = %d\n",
444 c_in_name, n_atoms, i );
445 painCave.isFatal = 1;
446 simError();
447 }
448 (void)sscanf( foo, "%lf", &vy );
449
450 foo = strtok(NULL, " ,;\t");
451 if(foo == NULL){
452 sprintf( painCave.errMsg,
453 "error in reading velocity z from %s\n"
454 "natoms = %d, index = %d\n",
455 c_in_name, n_atoms, i );
456 painCave.isFatal = 1;
457 simError();
458 }
459 (void)sscanf( foo, "%lf", &vz );
460
461
462 // get the quaternions
463
464 if( atoms[i]->isDirectional() ){
465
466 foo = strtok(NULL, " ,;\t");
467 if(foo == NULL){
468 sprintf(painCave.errMsg,
469 "error in reading quaternion 0 from %s\n"
470 "natoms = %d, index = %d\n",
471 c_in_name, n_atoms, i );
472 painCave.isFatal = 1;
473 simError();
474 }
475 (void)sscanf( foo, "%lf", &q[0] );
476
477 foo = strtok(NULL, " ,;\t");
478 if(foo == NULL){
479 sprintf( painCave.errMsg,
480 "error in reading quaternion 1 from %s\n"
481 "natoms = %d, index = %d\n",
482 c_in_name, n_atoms, i );
483 painCave.isFatal = 1;
484 simError();
485 }
486 (void)sscanf( foo, "%lf", &q[1] );
487
488 foo = strtok(NULL, " ,;\t");
489 if(foo == NULL){
490 sprintf( painCave.errMsg,
491 "error in reading quaternion 2 from %s\n"
492 "natoms = %d, index = %d\n",
493 c_in_name, n_atoms, i );
494 painCave.isFatal = 1;
495 simError();
496 }
497 (void)sscanf( foo, "%lf", &q[2] );
498
499 foo = strtok(NULL, " ,;\t");
500 if(foo == NULL){
501 sprintf( painCave.errMsg,
502 "error in reading quaternion 3 from %s\n"
503 "natoms = %d, index = %d\n",
504 c_in_name, n_atoms, i );
505 painCave.isFatal = 1;
506 simError();
507 }
508 (void)sscanf( foo, "%lf", &q[3] );
509
510 // get the angular velocities
511
512 foo = strtok(NULL, " ,;\t");
513 if(foo == NULL){
514 sprintf( painCave.errMsg,
515 "error in reading angular momentum jx from %s\n"
516 "natoms = %d, index = %d\n",
517 c_in_name, n_atoms, i );
518 painCave.isFatal = 1;
519 simError();
520 }
521 (void)sscanf( foo, "%lf", &jx );
522
523 foo = strtok(NULL, " ,;\t");
524 if(foo == NULL){
525 sprintf( painCave.errMsg,
526 "error in reading angular momentum jy from %s\n"
527 "natoms = %d, index = %d\n",
528 c_in_name, n_atoms, i );
529 painCave.isFatal = 1;
530 simError();
531 }
532 (void)sscanf( foo, "%lf", &jy );
533
534 foo = strtok(NULL, " ,;\t");
535 if(foo == NULL){
536 sprintf( painCave.errMsg,
537 "error in reading angular momentum jz from %s\n"
538 "natoms = %d, index = %d\n",
539 c_in_name, n_atoms, i );
540 painCave.isFatal = 1;
541 simError();
542 }
543 (void)sscanf( foo, "%lf", &jz );
544
545 dAtom = ( DirectionalAtom* )atoms[i];
546
547 // check that the quaternion vector is normalized
548
549 qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]);
550
551 qLength = sqrt( qSqr );
552 q[0] = q[0] / qLength;
553 q[1] = q[1] / qLength;
554 q[2] = q[2] / qLength;
555 q[3] = q[3] / qLength;
556
557 dAtom->setQ( q );
558
559 // add the angular velocities
560
561 dAtom->setJx( jx );
562 dAtom->setJy( jy );
563 dAtom->setJz( jz );
564 }
565
566 // add the positions and velocities to the atom
567
568 atoms[i]->setX( rx );
569 atoms[i]->setY( ry );
570 atoms[i]->setZ( rz );
571
572 atoms[i]->set_vx( vx );
573 atoms[i]->set_vy( vy );
574 atoms[i]->set_vz( vz );
575
576 }
577 #endif
578 }
579
580 char* IntitializeFromFile::parseDumpLine(char* readLine);