ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/InitializeFromFile.cpp
Revision: 209
Committed: Fri Dec 13 17:59:52 2002 UTC (21 years, 7 months ago) by mmeineke
File size: 10086 byte(s)
Log Message:
*** empty log message ***

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
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 "Error reading 1st line of %d \n ",c_in_name);
144 painCave.isFatal = 1;
145 simError();
146 }
147
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 "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
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 commment in %s\n", c_in_name);
168 painCave.isFatal = 1;
169 simError();
170 }
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 }
185
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 quaternion 1 from %s\n"
318 "natoms = %d, index = %d\n",
319 c_in_name, n_atoms, atomIndex );
320 return strdup( painCave.errMsg );
321 }
322 q[1] = atof( foo );
323
324 foo = strtok(NULL, " ,;\t");
325 if(foo == NULL){
326 sprintf( painCave.errMsg,
327 "error in reading quaternion 2 from %s\n"
328 "natoms = %d, index = %d\n",
329 c_in_name, n_atoms, atomIndex );
330 return strdup( painCave.errMsg );
331 }
332 q[2] = atof( foo );
333
334 foo = strtok(NULL, " ,;\t");
335 if(foo == NULL){
336 sprintf( painCave.errMsg,
337 "error in reading quaternion 3 from %s\n"
338 "natoms = %d, index = %d\n",
339 c_in_name, n_atoms, atomIndex );
340 return strdup( painCave.errMsg );
341 }
342 q[3] = atof( foo );
343
344 // get the angular velocities
345
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 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 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 dAtom = ( DirectionalAtom* )atoms[atomIndex];
377
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]);
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;
387
388 dAtom->setQ( q );
389
390 // add the angular velocities
391
392 dAtom->setJx( jx );
393 dAtom->setJy( jy );
394 dAtom->setJz( jz );
395 }
396
397 // add the positions and velocities to the atom
398
399 atoms[atomIndex]->setX( rx );
400 atoms[atomIndex]->setY( ry );
401 atoms[atomIndex]->setZ( rz );
402
403 atoms[atomIndex]->set_vx( vx );
404 atoms[atomIndex]->set_vy( vy );
405 atoms[atomIndex]->set_vz( vz );
406
407 return NULL;
408 }