ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/interface_implementation/LJ_FF.cpp
Revision: 234
Committed: Fri Jan 10 21:56:06 2003 UTC (21 years, 5 months ago) by mmeineke
File size: 10462 byte(s)
Log Message:
started some additions to wrap LJ down to fortran. INCOMPLETE!

File Contents

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