ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/InitializeFromFile.cpp
Revision: 212
Committed: Fri Dec 13 21:17:00 2002 UTC (21 years, 7 months ago) by chuckv
File size: 13280 byte(s)
Log Message:
Added mpi to InitializeFromFile read in.

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 const int BUFFERSIZE = 2000; // size of the read buffer
63 int n_atoms; // the number of atoms
64 char read_buffer[BUFFERSIZE]; //the line buffer for reading
65 #ifdef IS_MPI
66 char send_buffer[BUFFERSIZE];
67 #endif
68
69 char *eof_test; // ptr to see when we reach the end of the file
70 char *parseErr;
71
72
73 entry_plug = the_entry_plug
74
75
76 #ifndef IS_MPI
77 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
78 if( eof_test == NULL ){
79 sprintf( painCave.errMsg,
80 "InitializeFromFile error: error reading 1st line of \"%s\"\n",
81 c_in_name );
82 painCave.isFatal = 1;
83 simError();
84 }
85
86 n_atoms = atoi( read_buffer );
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
124 parseErr = parseDumpLine( read_buffer, i );
125 if( parseErr != NULL ){
126 strcpy( painCave.errMsg, parseErr );
127 painCave.isFatal = 1;
128 simError();
129 }
130 }
131
132
133 // MPI Section of code..........
134 #else //IS_MPI
135
136 int masterIndex;
137 int nodeAtomsStart;
138 int nodeAtomsEnd;
139 int mpiErr;
140 int sendError;
141
142 MPI_Status istatus[MPI_STATUS_SIZE];
143
144 if (worldRank == 0) {
145 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
146 if( eof_test == NULL ){
147 sprintf( painCave.errMsg,
148 "Error reading 1st line of %d \n ",c_in_name);
149 painCave.isFatal = 1;
150 simError();
151 }
152
153 n_atoms = atoi( read_buffer );
154
155 Atom **atoms = entry_plug->atoms;
156 DirectionalAtom* dAtom;
157
158 // Check to see that the number of atoms in the intial configuration file is the
159 // same as declared in simBass.
160
161 if( n_atoms != entry_plug->mpiSim->getTotAtoms() ){
162 sprintf( painCave.errMsg,
163 "Initialize from File error. %s n_atoms, %d, "
164 "does not match the BASS file's n_atoms, %d.\n",
165 c_in_name, n_atoms, entry_plug->n_atoms );
166 painCave.isFatal = 1;
167 simError();
168 }
169
170 //read and toss the comment line
171
172 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
173 if(eof_test == NULL){
174 sprintf( painCave.errMsg,
175 "error in reading commment in %s\n", c_in_name);
176 painCave.isFatal = 1;
177 simError();
178 }
179
180 // Read Proc 0 share of the xyz file...
181 masterIndex = 0;
182 for( i=0; i <= entry_plug->mpiSim->getMyAtomEnd(); i++){
183
184 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
185 if(eof_test == NULL){
186 sprintf(painCave.errMsg,
187 "error in reading file %s\n"
188 "natoms = %d; index = %d\n"
189 "error reading the line from the file.\n",
190 c_in_name, n_atoms, i );
191 painCave.isFatal = 1;
192 simError();
193 }
194
195 parseErr = parseDumpLine( read_buffer, i );
196 if( parseErr != NULL ){
197 strcpy( painCave.errMsg, parseErr );
198 painCave.isFatal = 1;
199 simError();
200 }
201 masterIndex++;
202 }
203 }
204
205 sprintf(checkPointMsg,
206 "Node 0 has successfully read positions from input file.");
207 mpiCheckPoint();
208
209 for (procIndex = 1; procIndex < entryPlug->mpiSim->getNumberProcessors();
210 procIndex++){
211 if (worldRank == 0) {
212
213 mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD,
214 istatus);
215
216 mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD,
217 istatus);
218 // Make sure where node 0 is reading from, matches where the receiving node
219 // expects it to be.
220
221 if (masterIndex != nodeAtomsStart){
222 sendError = 1;
223 mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD);
224 sprintf(painCave.errMsg,
225 "Initialize from file error: atoms start index (%d) for "
226 "node %d not equal to master index (%d)",nodeAtomsStart,procIndex,masterIndex );
227 painCave.isFatal = 1;
228 simError();
229 }
230 sendError = 0;
231 mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD);
232
233 for ( i = nodeAtomStart; i <= nodeAtomEnd, i++){
234 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
235 if(eof_test == NULL){
236
237 sprintf(read_buffer,"ERROR");
238 mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD);
239
240 sprintf(painCave.errMsg,
241 "error in reading file %s\n"
242 "natoms = %d; index = %d\n"
243 "error reading the line from the file.\n",
244 c_in_name, n_atoms, i );
245 painCave.isFatal = 1;
246 simError();
247 }
248
249 mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD);
250 mpiErr = MPI_Recv(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD,
251 istatus);
252 if (sendError) mpiCheckpoint();
253
254 masterIndex++;
255 }
256 }
257
258
259 else if(worldRank == procIndex){
260 nodeAtomStart = entry_plug->mpiSim->getMyAtomStart();
261 nodeAtomEnd = entry_plug->mpiSim->getMyAtomEnd();
262 mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD);
263 mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD);
264
265 mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD,
266 istatus);
267 if (sendError) mpiCheckpoint();
268
269 for ( i = 0; i < entry_plug->n_atoms, i++){
270
271 mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,0,MPI_ANY_TAG,MPI_COMM_WORLD,
272 istatus);
273
274 if(!strcmp(read_buffer, "ERROR")) mpiCheckPoint();
275
276 parseErr = parseDumpLine( read_buffer, i );
277 if( parseErr != NULL ){
278 sendError = 1;
279 mpiErr = MPI_Send(&sendError,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD);
280
281
282 strcpy( painCave.errMsg, parseErr );
283 painCave.isFatal = 1;
284 simError();
285 }
286 sendError = 0;
287 mpiErr = MPI_Send(&sendError,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD);
288 }
289 }
290 sprintf(checkPointMsg,"Node %d received initial configuration.",procIndex);
291 mpiCheckPoint();
292 }
293
294 #endif
295 }
296
297
298 char* IntitializeFromFile::parseDumpLine(char* readLine, int atomIndex){
299
300 char *foo; // the pointer to the current string token
301
302 double rx, ry, rz; // position place holders
303 double vx, vy, vz; // velocity placeholders
304 double q[4]; // the quaternions
305 double jx, jy, jz; // angular velocity placeholders;
306 double qSqr, qLength; // needed to normalize the quaternion vector.
307
308 Atom **atoms = entry_plug->atoms;
309 DirectionalAtom* dAtom;
310
311 int n_atoms;
312
313 #ifdef IS_MPI
314 n_atoms = entry_plug->mpiSim->getTotAtoms();
315 #else
316 n_atoms = entry_plug->n_atoms;
317 #endi // is_mpi
318
319
320 // set the string tokenizer
321
322 foo = strtok(readLine, " ,;\t");
323
324 // check the atom name to the current atom
325
326 if( strcmp( foo, atoms[atomIndex]->getType() ) ){
327 sprintf( painCave.errMsg,
328 "Initialize from file error. Atom %s at index %d "
329 "in file %s does not"
330 " match the BASS atom %s.\n",
331 foo, atomIndex, c_in_name, atoms[atomIndex]->getType() );
332 return strdup( painCave.errMsg );
333 }
334
335 // get the positions
336
337 foo = strtok(NULL, " ,;\t");
338 if(foo == NULL){
339 sprintf( painCave.errMsg,
340 "error in reading postition x from %s\n"
341 "natoms = %d, index = %d\n",
342 c_in_name, n_atoms, atomIndex );
343 return strdup( painCave.errMsg );
344 }
345 rx = atof( foo );
346
347 foo = strtok(NULL, " ,;\t");
348 if(foo == NULL){
349 sprintf( painCave.errMsg,
350 "error in reading postition y from %s\n"
351 "natoms = %d, index = %d\n",
352 c_in_name, n_atoms, atomIndex );
353 return strdup( painCave.errMsg );
354 }
355 ry = atof( foo );
356
357 foo = strtok(NULL, " ,;\t");
358 if(foo == NULL){
359 sprintf( painCave.errMsg,
360 "error in reading postition z from %s\n"
361 "natoms = %d, index = %d\n",
362 c_in_name, n_atoms, atomIndex );
363 return strdup( painCave.errMsg );
364 }
365 rz = atof( foo );
366
367
368 // get the velocities
369
370 foo = strtok(NULL, " ,;\t");
371 if(foo == NULL){
372 sprintf( painCave.errMsg,
373 "error in reading velocity x from %s\n"
374 "natoms = %d, index = %d\n",
375 c_in_name, n_atoms, atomIndex );
376 return strdup( painCave.errMsg );
377 }
378 vx = atof( foo );
379
380 foo = strtok(NULL, " ,;\t");
381 if(foo == NULL){
382 sprintf( painCave.errMsg,
383 "error in reading velocity y from %s\n"
384 "natoms = %d, index = %d\n",
385 c_in_name, n_atoms, atomIndex );
386 return strdup( painCave.errMsg );
387 }
388 vy = atof( foo );
389
390 foo = strtok(NULL, " ,;\t");
391 if(foo == NULL){
392 sprintf( painCave.errMsg,
393 "error in reading velocity z from %s\n"
394 "natoms = %d, index = %d\n",
395 c_in_name, n_atoms, atomIndex );
396 return strdup( painCave.errMsg );
397 }
398 vz = atof( foo );
399
400
401 // get the quaternions
402
403 if( atoms[atomIndex]->isDirectional() ){
404
405 foo = strtok(NULL, " ,;\t");
406 if(foo == NULL){
407 sprintf(painCave.errMsg,
408 "error in reading quaternion 0 from %s\n"
409 "natoms = %d, index = %d\n",
410 c_in_name, n_atoms, atomIndex );
411 return strdup( painCave.errMsg );
412 }
413 q[0] = atof( foo );
414
415 foo = strtok(NULL, " ,;\t");
416 if(foo == NULL){
417 sprintf( painCave.errMsg,
418 "error in reading quaternion 1 from %s\n"
419 "natoms = %d, index = %d\n",
420 c_in_name, n_atoms, atomIndex );
421 return strdup( painCave.errMsg );
422 }
423 q[1] = atof( foo );
424
425 foo = strtok(NULL, " ,;\t");
426 if(foo == NULL){
427 sprintf( painCave.errMsg,
428 "error in reading quaternion 2 from %s\n"
429 "natoms = %d, index = %d\n",
430 c_in_name, n_atoms, atomIndex );
431 return strdup( painCave.errMsg );
432 }
433 q[2] = atof( foo );
434
435 foo = strtok(NULL, " ,;\t");
436 if(foo == NULL){
437 sprintf( painCave.errMsg,
438 "error in reading quaternion 3 from %s\n"
439 "natoms = %d, index = %d\n",
440 c_in_name, n_atoms, atomIndex );
441 return strdup( painCave.errMsg );
442 }
443 q[3] = atof( foo );
444
445 // get the angular velocities
446
447 foo = strtok(NULL, " ,;\t");
448 if(foo == NULL){
449 sprintf( painCave.errMsg,
450 "error in reading angular momentum jx from %s\n"
451 "natoms = %d, index = %d\n",
452 c_in_name, n_atoms, atomIndex );
453 return strdup( painCave.errMsg );
454 }
455 jx = atof( foo );
456
457 foo = strtok(NULL, " ,;\t");
458 if(foo == NULL){
459 sprintf( painCave.errMsg,
460 "error in reading angular momentum jy from %s\n"
461 "natoms = %d, index = %d\n",
462 c_in_name, n_atoms, atomIndex );
463 return strdup( painCave.errMsg );
464 }
465 jy = atof(foo );
466
467 foo = strtok(NULL, " ,;\t");
468 if(foo == NULL){
469 sprintf( painCave.errMsg,
470 "error in reading angular momentum jz from %s\n"
471 "natoms = %d, index = %d\n",
472 c_in_name, n_atoms, atomIndex );
473 return strdup( painCave.errMsg );
474 }
475 jz = atof( foo );
476
477 dAtom = ( DirectionalAtom* )atoms[atomIndex];
478
479 // check that the quaternion vector is normalized
480
481 qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]);
482
483 qLength = sqrt( qSqr );
484 q[0] = q[0] / qLength;
485 q[1] = q[1] / qLength;
486 q[2] = q[2] / qLength;
487 q[3] = q[3] / qLength;
488
489 dAtom->setQ( q );
490
491 // add the angular velocities
492
493 dAtom->setJx( jx );
494 dAtom->setJy( jy );
495 dAtom->setJz( jz );
496 }
497
498 // add the positions and velocities to the atom
499
500 atoms[atomIndex]->setX( rx );
501 atoms[atomIndex]->setY( ry );
502 atoms[atomIndex]->setZ( rz );
503
504 atoms[atomIndex]->set_vx( vx );
505 atoms[atomIndex]->set_vy( vy );
506 atoms[atomIndex]->set_vz( vz );
507
508 return NULL;
509 }