ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/InitializeFromFile.cpp
Revision: 254
Committed: Thu Jan 30 20:03:37 2003 UTC (21 years, 5 months ago) by chuckv
File size: 13290 byte(s)
Log Message:
Bug fixes for mpi version of code

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