ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE_old/src/mdtools/libmdCode/LJ_FF.cpp
Revision: 299
Committed: Fri Mar 7 19:31:02 2003 UTC (21 years, 6 months ago) by mmeineke
File size: 12497 byte(s)
Log Message:
implemented the new fortran force interface

File Contents

# User Rev Content
1 mmeineke 270 #include <cstdlib>
2     #include <cstdio>
3     #include <cstring>
4    
5     #include <iostream>
6     using namespace std;
7    
8 mmeineke 275 #ifdef IS_MPI
9     #include <mpi.h>
10     #include <mpi++.h>
11     #endif //is_mpi
12    
13 mmeineke 270 #include "ForceFields.hpp"
14     #include "SRI.hpp"
15     #include "simError.h"
16    
17 mmeineke 294 #include <fortranWrappers.hpp>
18    
19 mmeineke 291 #ifdef IS_MPI
20     #include "mpiForceField.h"
21     #endif // is_mpi
22 mmeineke 275
23    
24 mmeineke 270
25 mmeineke 291 namespace {
26 mmeineke 270
27 mmeineke 291 // Declare the structures that will be passed by the parser and MPI
28    
29     typedef struct{
30     char name[15];
31     double mass;
32     double epslon;
33     double sigma;
34     int ident;
35     int last; // 0 -> default
36     // 1 -> in MPI: tells nodes to stop listening
37     } atomStruct;
38 mmeineke 270
39 mmeineke 291 int parseAtom( char *lineBuffer, int lineNum, atomStruct &info );
40    
41 mmeineke 270 #ifdef IS_MPI
42 mmeineke 291
43     MPI_Datatype mpiAtomStructType;
44    
45 mmeineke 270 #endif
46 mmeineke 291 }
47 mmeineke 270
48    
49     //****************************************************************
50     // begins the actual forcefield stuff.
51     //****************************************************************
52    
53    
54     LJ_FF::LJ_FF(){
55    
56     char fileName[200];
57     char* ffPath_env = "FORCE_PARAM_PATH";
58     char* ffPath;
59     char temp[200];
60     char errMsg[1000];
61    
62     // do the funtion wrapping
63 mmeineke 294 wrapMeFF( this );
64 mmeineke 270
65     #ifdef IS_MPI
66     int i;
67    
68     // **********************************************************************
69     // Init the atomStruct mpi type
70    
71     atomStruct atomProto; // mpiPrototype
72     int atomBC[3] = {15,3,2}; // block counts
73     MPI_Aint atomDspls[3]; // displacements
74     MPI_Datatype atomMbrTypes[3]; // member mpi types
75    
76     MPI_Address(&atomProto.name, &atomDspls[0]);
77     MPI_Address(&atomProto.mass, &atomDspls[1]);
78     MPI_Address(&atomProto.ident, &atomDspls[2]);
79    
80     atomMbrTypes[0] = MPI_CHAR;
81     atomMbrTypes[1] = MPI_DOUBLE;
82     atomMbrTypes[2] = MPI_INT;
83    
84     for (i=2; i >= 0; i--) atomDspls[i] -= atomDspls[0];
85    
86     MPI_Type_struct(3, atomBC, atomDspls, atomMbrTypes, &mpiAtomStructType);
87     MPI_Type_commit(&mpiAtomStructType);
88    
89     // ***********************************************************************
90    
91     if( worldRank == 0 ){
92     #endif
93    
94     // generate the force file name
95    
96     strcpy( fileName, "LJ_FF.frc" );
97     // fprintf( stderr,"Trying to open %s\n", fileName );
98    
99     // attempt to open the file in the current directory first.
100    
101     frcFile = fopen( fileName, "r" );
102    
103     if( frcFile == NULL ){
104    
105     // next see if the force path enviorment variable is set
106    
107     ffPath = getenv( ffPath_env );
108     if( ffPath == NULL ) {
109     sprintf( painCave.errMsg,
110     "Error opening the force field parameter file: %s\n"
111     "Have you tried setting the FORCE_PARAM_PATH environment "
112     "vairable?\n",
113     fileName );
114     painCave.isFatal = 1;
115     simError();
116     }
117    
118    
119     strcpy( temp, ffPath );
120     strcat( temp, "/" );
121     strcat( temp, fileName );
122     strcpy( fileName, temp );
123    
124     frcFile = fopen( fileName, "r" );
125    
126     if( frcFile == NULL ){
127    
128     sprintf( painCave.errMsg,
129     "Error opening the force field parameter file: %s\n"
130     "Have you tried setting the FORCE_PARAM_PATH environment "
131     "vairable?\n",
132     fileName );
133     painCave.isFatal = 1;
134     simError();
135     }
136     }
137    
138     #ifdef IS_MPI
139     }
140    
141     sprintf( checkPointMsg, "LJ_FF file opened sucessfully." );
142     MPIcheckPoint();
143    
144     #endif // is_mpi
145     }
146    
147    
148     LJ_FF::~LJ_FF(){
149    
150     #ifdef IS_MPI
151     if( worldRank == 0 ){
152     #endif // is_mpi
153    
154     fclose( frcFile );
155    
156     #ifdef IS_MPI
157     }
158     #endif // is_mpi
159     }
160    
161    
162    
163     void LJ_FF::initializeAtoms( void ){
164    
165     class LinkedType {
166     public:
167     LinkedType(){
168     next = NULL;
169     name[0] = '\0';
170     }
171     ~LinkedType(){ if( next != NULL ) delete next; }
172    
173     LinkedType* find(char* key){
174     if( !strcmp(name, key) ) return this;
175     if( next != NULL ) return next->find(key);
176     return NULL;
177     }
178    
179    
180     void add( atomStruct &info ){
181    
182     // check for duplicates
183    
184     if( !strcmp( info.name, name ) ){
185     sprintf( painCave.errMsg,
186     "Duplicate LJ atom type \"%s\" found in "
187     "the LJ_FF param file./n",
188     name );
189     painCave.isFatal = 1;
190     simError();
191     }
192    
193     if( next != NULL ) next->add(info);
194     else{
195     next = new LinkedType();
196     strcpy(next->name, info.name);
197     next->mass = info.mass;
198     next->epslon = info.epslon;
199     next->sigma = info.sigma;
200     next->ident = info.ident;
201     }
202     }
203    
204    
205     #ifdef IS_MPI
206    
207     void duplicate( atomStruct &info ){
208     strcpy(info.name, name);
209     info.mass = mass;
210     info.epslon = epslon;
211     info.sigma = sigma;
212     info.ident = ident;
213     info.last = 0;
214     }
215    
216    
217     #endif
218    
219     char name[15];
220     double mass;
221     double epslon;
222     double sigma;
223     int ident;
224     LinkedType* next;
225     };
226    
227     LinkedType* headAtomType;
228     LinkedType* currentAtomType;
229     atomStruct info;
230     info.last = 1; // initialize last to have the last set.
231     // if things go well, last will be set to 0
232    
233     int i;
234     int identNum;
235    
236     Atom** the_atoms;
237     int nAtoms;
238     the_atoms = entry_plug->atoms;
239     nAtoms = entry_plug->n_atoms;
240    
241    
242     #ifdef IS_MPI
243     if( worldRank == 0 ){
244     #endif
245    
246     // read in the atom types.
247    
248     headAtomType = new LinkedType;
249    
250     fastForward( "AtomTypes", "initializeAtoms" );
251    
252     // we are now at the AtomTypes section.
253    
254     eof_test = fgets( readLine, sizeof(readLine), frcFile );
255     lineNum++;
256    
257    
258     // read a line, and start parseing out the atom types
259    
260     if( eof_test == NULL ){
261     sprintf( painCave.errMsg,
262     "Error in reading Atoms from force file at line %d.\n",
263     lineNum );
264     painCave.isFatal = 1;
265     simError();
266     }
267    
268     identNum = 1;
269     // stop reading at end of file, or at next section
270     while( readLine[0] != '#' && eof_test != NULL ){
271    
272     // toss comment lines
273     if( readLine[0] != '!' ){
274    
275     // the parser returns 0 if the line was blank
276     if( parseAtomLJ( readLine, lineNum, info ) ){
277     info.ident = identNum;
278     headAtomType->add( info );;
279     identNum++;
280     }
281     }
282     eof_test = fgets( readLine, sizeof(readLine), frcFile );
283     lineNum++;
284     }
285    
286     #ifdef IS_MPI
287    
288     // send out the linked list to all the other processes
289    
290     sprintf( checkPointMsg,
291     "LJ_FF atom structures read successfully." );
292     MPIcheckPoint();
293    
294     currentAtomType = headAtomType->next; //skip the first element who is a place holder.
295     while( currentAtomType != NULL ){
296     currentAtomType->duplicate( info );
297    
298    
299    
300     sendFrcStruct( &info, mpiAtomStructType );
301    
302     sprintf( checkPointMsg,
303     "successfully sent lJ force type: \"%s\"\n",
304     info.name );
305     MPIcheckPoint();
306    
307     currentAtomType = currentAtomType->next;
308     }
309     info.last = 1;
310     sendFrcStruct( &info, mpiAtomStructType );
311    
312     }
313    
314     else{
315    
316     // listen for node 0 to send out the force params
317    
318     MPIcheckPoint();
319    
320     headAtomType = new LinkedType;
321     recieveFrcStruct( &info, mpiAtomStructType );
322    
323     while( !info.last ){
324    
325    
326    
327     headAtomType->add( info );
328    
329     MPIcheckPoint();
330    
331     recieveFrcStruct( &info, mpiAtomStructType );
332     }
333     }
334     #endif // is_mpi
335    
336     // call new A_types in fortran
337    
338     int isError;
339 mmeineke 296
340     // dummy variables
341     int isLJ = 1;
342     int isDipole = 0;
343     int isSSD = 0;
344     int isGB = 0;
345     double w0 = 0.0;
346     double v0 = 0.0;
347     double dipole = 0.0;
348    
349    
350 mmeineke 270 currentAtomType = headAtomType;
351     while( currentAtomType != NULL ){
352    
353     if( currentAtomType->name[0] != '\0' ){
354     isError = 0;
355 mmeineke 296 newAtype( &(currentAtomType->ident),
356     &(currentAtomType->mass),
357     &(currentAtomType->epslon),
358     &(currentAtomType->sigma),
359     &isLJ, &isSSD, &isDipole, &isGB, &w0, &v0, &dipole,
360     &isError );
361 mmeineke 270 if( isError ){
362     sprintf( painCave.errMsg,
363     "Error initializing the \"%s\" atom type in fortran\n",
364     currentAtomType->name );
365     painCave.isFatal = 1;
366     simError();
367     }
368     }
369     currentAtomType = currentAtomType->next;
370     }
371    
372     #ifdef IS_MPI
373     sprintf( checkPointMsg,
374     "LJ_FF atom structures successfully sent to fortran\n" );
375     MPIcheckPoint();
376     #endif // is_mpi
377    
378    
379    
380     // initialize the atoms
381    
382     double bigSigma = 0.0;
383     Atom* thisAtom;
384    
385     for( i=0; i<nAtoms; i++ ){
386    
387     currentAtomType = headAtomType->find( the_atoms[i]->getType() );
388     if( currentAtomType == NULL ){
389     sprintf( painCave.errMsg,
390     "AtomType error, %s not found in force file.\n",
391     the_atoms[i]->getType() );
392     painCave.isFatal = 1;
393     simError();
394     }
395    
396     the_atoms[i]->setMass( currentAtomType->mass );
397     the_atoms[i]->setEpslon( currentAtomType->epslon );
398     the_atoms[i]->setSigma( currentAtomType->sigma );
399     the_atoms[i]->setIdent( currentAtomType->ident );
400     the_atoms[i]->setLJ();
401    
402     if( bigSigma < currentAtomType->sigma ) bigSigma = currentAtomType->sigma;
403     }
404    
405    
406     #ifdef IS_MPI
407     double tempBig = bigSigma;
408     MPI::COMM_WORLD.Allreduce( &tempBig, &bigSigma, 1, MPI_DOUBLE, MPI_MAX );
409     #endif //is_mpi
410    
411     //calc rCut and rList
412    
413     entry_plug->rCut = 2.5 * bigSigma;
414     if(entry_plug->rCut > (entry_plug->box_x / 2.0)) entry_plug->rCut = entry_plug->box_x / 2.0;
415     if(entry_plug->rCut > (entry_plug->box_y / 2.0)) entry_plug->rCut = entry_plug->box_y / 2.0;
416     if(entry_plug->rCut > (entry_plug->box_z / 2.0)) entry_plug->rCut = entry_plug->box_z / 2.0;
417    
418     entry_plug->rList = entry_plug->rCut + 1.0;
419    
420     // clean up the memory
421    
422     delete headAtomType;
423    
424     #ifdef IS_MPI
425     sprintf( checkPointMsg, "LJ_FF atoms initialized succesfully" );
426     MPIcheckPoint();
427     #endif // is_mpi
428    
429 mmeineke 299 this->initFortran();
430 mmeineke 270 entry_plug->refreshSim();
431 mmeineke 296
432 mmeineke 270 }
433    
434     void LJ_FF::initializeBonds( bond_pair* the_bonds ){
435    
436     if( entry_plug->n_bonds ){
437     sprintf( painCave.errMsg,
438     "LJ_FF does not support bonds.\n" );
439     painCave.isFatal = 1;
440     simError();
441     }
442     #ifdef IS_MPI
443     MPIcheckPoint();
444     #endif // is_mpi
445    
446     }
447    
448     void LJ_FF::initializeBends( bend_set* the_bends ){
449    
450     if( entry_plug->n_bends ){
451     sprintf( painCave.errMsg,
452     "LJ_FF does not support bends.\n" );
453     painCave.isFatal = 1;
454     simError();
455     }
456     #ifdef IS_MPI
457     MPIcheckPoint();
458     #endif // is_mpi
459    
460     }
461    
462     void LJ_FF::initializeTorsions( torsion_set* the_torsions ){
463    
464     if( entry_plug->n_torsions ){
465     sprintf( painCave.errMsg,
466     "LJ_FF does not support torsions.\n" );
467     painCave.isFatal = 1;
468     simError();
469     }
470     #ifdef IS_MPI
471     MPIcheckPoint();
472     #endif // is_mpi
473    
474     }
475    
476     void LJ_FF::fastForward( char* stopText, char* searchOwner ){
477    
478     int foundText = 0;
479     char* the_token;
480    
481     rewind( frcFile );
482     lineNum = 0;
483    
484     eof_test = fgets( readLine, sizeof(readLine), frcFile );
485     lineNum++;
486     if( eof_test == NULL ){
487     sprintf( painCave.errMsg, "Error fast forwarding force file for %s: "
488     " file is empty.\n",
489     searchOwner );
490     painCave.isFatal = 1;
491     simError();
492     }
493    
494    
495     while( !foundText ){
496     while( eof_test != NULL && readLine[0] != '#' ){
497     eof_test = fgets( readLine, sizeof(readLine), frcFile );
498     lineNum++;
499     }
500     if( eof_test == NULL ){
501     sprintf( painCave.errMsg,
502     "Error fast forwarding force file for %s at "
503     "line %d: file ended unexpectedly.\n",
504     searchOwner,
505     lineNum );
506     painCave.isFatal = 1;
507     simError();
508     }
509    
510     the_token = strtok( readLine, " ,;\t#\n" );
511     foundText = !strcmp( stopText, the_token );
512    
513     if( !foundText ){
514     eof_test = fgets( readLine, sizeof(readLine), frcFile );
515     lineNum++;
516    
517     if( eof_test == NULL ){
518     sprintf( painCave.errMsg,
519     "Error fast forwarding force file for %s at "
520     "line %d: file ended unexpectedly.\n",
521     searchOwner,
522     lineNum );
523     painCave.isFatal = 1;
524     simError();
525     }
526     }
527     }
528     }
529    
530    
531    
532 mmeineke 291 int parseAtom( char *lineBuffer, int lineNum, atomStruct &info ){
533 mmeineke 270
534     char* the_token;
535    
536     the_token = strtok( lineBuffer, " \n\t,;" );
537     if( the_token != NULL ){
538    
539     strcpy( info.name, the_token );
540    
541     if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){
542     sprintf( painCave.errMsg,
543     "Error parseing AtomTypes: line %d\n", lineNum );
544     painCave.isFatal = 1;
545     simError();
546     }
547    
548     info.mass = atof( the_token );
549    
550     if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){
551     sprintf( painCave.errMsg,
552     "Error parseing AtomTypes: line %d\n", lineNum );
553     painCave.isFatal = 1;
554     simError();
555     }
556    
557     info.epslon = atof( the_token );
558    
559     if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){
560     sprintf( painCave.errMsg,
561     "Error parseing AtomTypes: line %d\n", lineNum );
562     painCave.isFatal = 1;
563     simError();
564     }
565    
566     info.sigma = atof( the_token );
567    
568     return 1;
569     }
570     else return 0;
571     }