ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/InitializeFromFile.cpp
Revision: 419
Committed: Thu Mar 27 15:07:29 2003 UTC (21 years, 3 months ago) by gezelter
File size: 12083 byte(s)
Log Message:
fixes for local->global atom numbering in MPI

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