ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/InitializeFromFile.cpp
(Generate patch)

Comparing trunk/mdtools/md_code/InitializeFromFile.cpp (file contents):
Revision 162 by mmeineke, Thu Oct 31 21:20:49 2002 UTC vs.
Revision 209 by mmeineke, Fri Dec 13 17:59:52 2002 UTC

# Line 13 | Line 13 | InitializeFromFile :: InitializeFromFile( char *in_nam
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){
# Line 23 | Line 26 | InitializeFromFile :: InitializeFromFile( char *in_nam
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 <  
38 > #ifdef IS_MPI
39 >  if (worldRank == 0) {
40 > #endif
41    int error;
42    error = fclose( c_in_file );
43    if( error ){
# Line 35 | Line 45 | InitializeFromFile :: ~InitializeFromFile( ){
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* entry_plug ){
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[2000]; //the line buffer for reading
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 *foo; // the pointer to the current string token
70 >  char *parseErr;
71  
51  double rx, ry, rz; // position place holders
52  double vx, vy, vz; // velocity placeholders
53  double q[4]; // the quaternions
54  double jx, jy, jz; // angular velocity placeholders;
55  double qSqr, qLength; // needed to normalize the quaternion vector.
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 <    std::cerr << "error reading 1st line of" << c_in_name << "\n";
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 <  (void)sscanf(read_buffer, "%d", &n_atoms);
86 >  n_atoms = atoi( read_buffer );
87  
88    Atom **atoms = entry_plug->atoms;
89    DirectionalAtom* dAtom;
# Line 96 | Line 120 | void InitializeFromFile :: read_xyz( SimInfo* entry_pl
120        simError();
121      }
122  
99    foo = strtok(read_buffer, " ,;\t");
123      
124 <    // check the atom name to the current atom
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 <    if( strcmp( foo, atoms[i]->getType() ) ){
132 >
133 >  // MPI Section of code..........
134 > #else //IS_MPI
135 >
136 >
137 >
138 >
139 >  if (worldRank == 0) {
140 >    eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
141 >    if( eof_test == NULL ){
142        sprintf( painCave.errMsg,
143 <               "Initialize from file error. Atom %s at index %d "
106 <               "in file %s does not"
107 <               " match the BASS atom %s.\n",
108 <               foo, i, c_in_name, atoms[i]->getType() );
143 >               "Error reading 1st line of %d \n ",c_in_name);
144        painCave.isFatal = 1;
145        simError();
146      }
147      
148 <    // get the positions
149 <
150 <    foo = strtok(NULL, " ,;\t");
151 <    if(foo == NULL){
148 >    n_atoms = atoi( read_buffer );
149 >    
150 >    Atom **atoms = entry_plug->atoms;
151 >    DirectionalAtom* dAtom;
152 >    
153 >    if( n_atoms != entry_plug->mpiSim->getTotAtoms() ){
154        sprintf( painCave.errMsg,
155 <               "error in reading postition x from %s\n"
156 <               "natoms  = %d, index = %d\n",
157 <               c_in_name, n_atoms, i );
155 >               "Initialize from File error. %s n_atoms, %d, "
156 >               "does not match the BASS file's n_atoms, %d.\n",
157 >               c_in_name, n_atoms, entry_plug->n_atoms );
158        painCave.isFatal = 1;
159        simError();
160      }
161 <    (void)sscanf( foo, "%lf", &rx );
162 <  
163 <    foo = strtok(NULL, " ,;\t");
164 <    if(foo == NULL){
161 >    
162 >    //read and toss the comment line
163 >    
164 >    eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
165 >    if(eof_test == NULL){
166        sprintf( painCave.errMsg,
167 <               "error in reading postition y from %s\n"
130 <               "natoms  = %d, index = %d\n",
131 <               c_in_name, n_atoms, i );
167 >               "error in reading commment in %s\n", c_in_name);
168        painCave.isFatal = 1;
169        simError();
170      }
171 <    (void)sscanf( foo, "%lf", &ry );
172 <    
173 <    foo = strtok(NULL, " ,;\t");
174 <    if(foo == NULL){
175 <      sprintf( painCave.errMsg,
176 <               "error in reading postition z from %s\n"
177 <               "natoms  = %d, index = %d\n",
178 <               c_in_name, n_atoms, i );
171 >  }
172 >
173 >  for( i=0; i < n_atoms; i++){
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 file %s\n"
179 >              "natoms  = %d; index = %d\n"
180 >              "error reading the line from the file.\n",
181 >              c_in_name, n_atoms, i );
182        painCave.isFatal = 1;
183        simError();
184      }
146    (void)sscanf( foo, "%lf", &rz );
185      
186 <    // get the velocities
186 >    parseErr = parseDumpLine( read_buffer, i );
187 >    if( parseErr != NULL ){
188 >      strcpy( painCave.errMsg, parseErr );
189 >      painCave.isFatal = 1;
190 >      simError();
191 >    }    
192 >  }
193 > #endif
194 > }
195  
196 +
197 + char* IntitializeFromFile::parseDumpLine(char* readLine, int atomIndex){
198 +
199 +  char *foo; // the pointer to the current string token
200 +  
201 +  double rx, ry, rz; // position place holders
202 +  double vx, vy, vz; // velocity placeholders
203 +  double q[4]; // the quaternions
204 +  double jx, jy, jz; // angular velocity placeholders;
205 +  double qSqr, qLength; // needed to normalize the quaternion vector.
206 +  
207 +  Atom **atoms = entry_plug->atoms;
208 +  DirectionalAtom* dAtom;
209 +  
210 +  int n_atoms;
211 +
212 + #ifdef IS_MPI
213 +  n_atoms = entry_plug->mpiSim->getTotAtoms();
214 + #else
215 +  n_atoms = entry_plug->n_atoms;
216 + #endi // is_mpi
217 +
218 +
219 +  // set the string tokenizer
220 +  
221 +  foo = strtok(readLine, " ,;\t");
222 +  
223 +  // check the atom name to the current atom
224 +  
225 +  if( strcmp( foo, atoms[atomIndex]->getType() ) ){
226 +    sprintf( painCave.errMsg,
227 +             "Initialize from file error. Atom %s at index %d "
228 +             "in file %s does not"
229 +             " match the BASS atom %s.\n",
230 +             foo, atomIndex, c_in_name, atoms[atomIndex]->getType() );
231 +    return strdup( painCave.errMsg );
232 +  }
233 +    
234 +  // get the positions
235 +
236 +  foo = strtok(NULL, " ,;\t");
237 +  if(foo == NULL){
238 +    sprintf( painCave.errMsg,
239 +             "error in reading postition x from %s\n"
240 +             "natoms  = %d, index = %d\n",
241 +             c_in_name, n_atoms, atomIndex );
242 +    return strdup( painCave.errMsg );
243 +  }
244 +  rx = atof( foo );
245 +  
246 +  foo = strtok(NULL, " ,;\t");
247 +  if(foo == NULL){
248 +    sprintf( painCave.errMsg,
249 +             "error in reading postition y from %s\n"
250 +             "natoms  = %d, index = %d\n",
251 +             c_in_name, n_atoms, atomIndex );
252 +    return strdup( painCave.errMsg );
253 +  }
254 +  ry = atof( foo );
255 +    
256 +  foo = strtok(NULL, " ,;\t");
257 +  if(foo == NULL){
258 +    sprintf( painCave.errMsg,
259 +             "error in reading postition z from %s\n"
260 +             "natoms  = %d, index = %d\n",
261 +             c_in_name, n_atoms, atomIndex );
262 +    return strdup( painCave.errMsg );
263 +  }
264 +  rz = atof( foo );    
265 +
266 +
267 +  // get the velocities
268 +
269 +  foo = strtok(NULL, " ,;\t");
270 +  if(foo == NULL){
271 +    sprintf( painCave.errMsg,
272 +             "error in reading velocity x from %s\n"
273 +             "natoms  = %d, index = %d\n",
274 +             c_in_name, n_atoms, atomIndex );
275 +    return strdup( painCave.errMsg );
276 +  }
277 +  vx = atof( foo );
278 +    
279 +  foo = strtok(NULL, " ,;\t");
280 +  if(foo == NULL){
281 +    sprintf( painCave.errMsg,
282 +             "error in reading velocity y from %s\n"
283 +             "natoms  = %d, index = %d\n",
284 +             c_in_name, n_atoms, atomIndex );
285 +    return strdup( painCave.errMsg );
286 +  }
287 +  vy = atof( foo );
288 +    
289 +  foo = strtok(NULL, " ,;\t");
290 +  if(foo == NULL){
291 +    sprintf( painCave.errMsg,
292 +             "error in reading velocity z from %s\n"
293 +             "natoms  = %d, index = %d\n",
294 +             c_in_name, n_atoms, atomIndex );
295 +    return strdup( painCave.errMsg );
296 +  }
297 +  vz = atof( foo );
298 +    
299 +    
300 +  // get the quaternions
301 +    
302 +  if( atoms[atomIndex]->isDirectional() ){
303 +      
304      foo = strtok(NULL, " ,;\t");
305      if(foo == NULL){
306 +      sprintf(painCave.errMsg,
307 +              "error in reading quaternion 0 from %s\n"
308 +              "natoms  = %d, index = %d\n",
309 +              c_in_name, n_atoms, atomIndex );
310 +      return strdup( painCave.errMsg );
311 +    }
312 +    q[0] = atof( foo );
313 +      
314 +    foo = strtok(NULL, " ,;\t");
315 +    if(foo == NULL){
316        sprintf( painCave.errMsg,
317 <               "error in reading velocity x from %s\n"
317 >               "error in reading quaternion 1 from %s\n"
318                 "natoms  = %d, index = %d\n",
319 <               c_in_name, n_atoms, i );
320 <      painCave.isFatal = 1;
157 <      simError();
319 >               c_in_name, n_atoms, atomIndex );
320 >      return strdup( painCave.errMsg );
321      }
322 <    (void)sscanf( foo, "%lf", &vx );
323 <    
322 >    q[1] = atof( foo );
323 >      
324      foo = strtok(NULL, " ,;\t");
325      if(foo == NULL){
326        sprintf( painCave.errMsg,
327 <               "error in reading velocity y from %s\n"
327 >               "error in reading quaternion 2 from %s\n"
328                 "natoms  = %d, index = %d\n",
329 <               c_in_name, n_atoms, i );
330 <      painCave.isFatal = 1;
168 <      simError();
329 >               c_in_name, n_atoms, atomIndex );
330 >      return strdup( painCave.errMsg );
331      }
332 <    (void)sscanf( foo, "%lf", &vy );
333 <    
332 >    q[2] = atof( foo );
333 >      
334      foo = strtok(NULL, " ,;\t");
335      if(foo == NULL){
336        sprintf( painCave.errMsg,
337 <               "error in reading velocity z from %s\n"
337 >               "error in reading quaternion 3 from %s\n"
338                 "natoms  = %d, index = %d\n",
339 <               c_in_name, n_atoms, i );
340 <      painCave.isFatal = 1;
179 <      simError();
339 >               c_in_name, n_atoms, atomIndex );
340 >      return strdup( painCave.errMsg );
341      }
342 <    (void)sscanf( foo, "%lf", &vz );
182 <    
183 <    
184 <    // get the quaternions
185 <    
186 <    if( atoms[i]->isDirectional() ){
342 >    q[3] = atof( foo );
343        
344 <      foo = strtok(NULL, " ,;\t");
189 <      if(foo == NULL){
190 <        sprintf(painCave.errMsg,
191 <                "error in reading quaternion 0 from %s\n"
192 <                "natoms  = %d, index = %d\n",
193 <                c_in_name, n_atoms, i );
194 <        painCave.isFatal = 1;
195 <        simError();
196 <      }
197 <      (void)sscanf( foo, "%lf", &q[0] );
344 >    // get the angular velocities
345        
346 <      foo = strtok(NULL, " ,;\t");
347 <      if(foo == NULL){
348 <        sprintf( painCave.errMsg,
349 <                 "error in reading quaternion 1 from %s\n"
350 <                 "natoms  = %d, index = %d\n",
351 <                 c_in_name, n_atoms, i );
352 <        painCave.isFatal = 1;
353 <        simError();
354 <      }
208 <      (void)sscanf( foo, "%lf", &q[1] );
346 >    foo = strtok(NULL, " ,;\t");
347 >    if(foo == NULL){
348 >      sprintf( painCave.errMsg,
349 >               "error in reading angular momentum jx from %s\n"
350 >               "natoms  = %d, index = %d\n",
351 >               c_in_name, n_atoms, atomIndex );
352 >      return strdup( painCave.errMsg );
353 >    }
354 >    jx = atof( foo );
355        
356 <      foo = strtok(NULL, " ,;\t");
357 <      if(foo == NULL){
358 <        sprintf( painCave.errMsg,
359 <                 "error in reading quaternion 2 from %s\n"
360 <                 "natoms  = %d, index = %d\n",
361 <                 c_in_name, n_atoms, i );
362 <        painCave.isFatal = 1;
363 <        simError();
364 <      }
219 <      (void)sscanf( foo, "%lf", &q[2] );
356 >    foo = strtok(NULL, " ,;\t");
357 >    if(foo == NULL){
358 >      sprintf( painCave.errMsg,
359 >               "error in reading angular momentum jy from %s\n"
360 >               "natoms  = %d, index = %d\n",
361 >               c_in_name, n_atoms, atomIndex );
362 >      return strdup( painCave.errMsg );
363 >    }
364 >    jy = atof(foo );
365        
366 <      foo = strtok(NULL, " ,;\t");
367 <      if(foo == NULL){
368 <        sprintf( painCave.errMsg,
369 <                 "error in reading quaternion 3 from %s\n"
370 <                 "natoms  = %d, index = %d\n",
371 <                 c_in_name, n_atoms, i );
372 <        painCave.isFatal = 1;
373 <        simError();
374 <      }
230 <      (void)sscanf( foo, "%lf", &q[3] );
366 >    foo = strtok(NULL, " ,;\t");
367 >    if(foo == NULL){
368 >      sprintf( painCave.errMsg,
369 >               "error in reading angular momentum jz from %s\n"
370 >               "natoms  = %d, index = %d\n",
371 >               c_in_name, n_atoms, atomIndex );
372 >      return strdup( painCave.errMsg );
373 >    }
374 >    jz = atof( foo );
375        
376 <      // get the angular velocities
233 <      
234 <      foo = strtok(NULL, " ,;\t");
235 <      if(foo == NULL){
236 <        sprintf( painCave.errMsg,
237 <                 "error in reading angular momentum jx 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", &jx );
244 <      
245 <      foo = strtok(NULL, " ,;\t");
246 <      if(foo == NULL){
247 <        sprintf( painCave.errMsg,
248 <                 "error in reading angular momentum jy 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", &jy );
255 <      
256 <      foo = strtok(NULL, " ,;\t");
257 <      if(foo == NULL){
258 <        sprintf( painCave.errMsg,
259 <                 "error in reading angular momentum jz from %s\n"
260 <                 "natoms  = %d, index = %d\n",
261 <                 c_in_name, n_atoms, i );
262 <        painCave.isFatal = 1;
263 <        simError();
264 <      }
265 <      (void)sscanf( foo, "%lf", &jz );
266 <      
267 <      dAtom = ( DirectionalAtom* )atoms[i];
376 >    dAtom = ( DirectionalAtom* )atoms[atomIndex];
377  
378 <      // check that the quaternion vector is normalized
378 >    // check that the quaternion vector is normalized
379  
380 <      qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]);
380 >    qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]);
381        
382 <      qLength = sqrt( qSqr );
383 <      q[0] = q[0] / qLength;
384 <      q[1] = q[1] / qLength;
385 <      q[2] = q[2] / qLength;
386 <      q[3] = q[3] / qLength;
382 >    qLength = sqrt( qSqr );
383 >    q[0] = q[0] / qLength;
384 >    q[1] = q[1] / qLength;
385 >    q[2] = q[2] / qLength;
386 >    q[3] = q[3] / qLength;
387        
388 <      dAtom->setQ( q );
388 >    dAtom->setQ( q );
389        
390 <      // add the angular velocities
390 >    // add the angular velocities
391  
392 <      dAtom->setJx( jx );
393 <      dAtom->setJy( jy );
394 <      dAtom->setJz( jz );
395 <    }
392 >    dAtom->setJx( jx );
393 >    dAtom->setJy( jy );
394 >    dAtom->setJz( jz );
395 >  }
396      
397 <    // add the positions and velocities to the atom
397 >  // add the positions and velocities to the atom
398      
399 <    atoms[i]->setX( rx );
400 <    atoms[i]->setY( ry );
401 <    atoms[i]->setZ( rz );
399 >  atoms[atomIndex]->setX( rx );
400 >  atoms[atomIndex]->setY( ry );
401 >  atoms[atomIndex]->setZ( rz );
402      
403 <    atoms[i]->set_vx( vx );
404 <    atoms[i]->set_vy( vy );
405 <    atoms[i]->set_vz( vz );
406 <    
407 <  }
403 >  atoms[atomIndex]->set_vx( vx );
404 >  atoms[atomIndex]->set_vy( vy );
405 >  atoms[atomIndex]->set_vz( vz );
406 >
407 >  return NULL;
408   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines