1 |
tim |
1703 |
/* |
2 |
|
|
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
3 |
|
|
* |
4 |
|
|
* Contact: oopse@oopse.org |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2.1 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* All we ask is that proper credit is given for our work, which includes |
11 |
|
|
* - but is not limited to - adding the above copyright notice to the beginning |
12 |
|
|
* of your source code files, and to any copyright notice that you may distribute |
13 |
|
|
* with programs based on this work. |
14 |
|
|
* |
15 |
|
|
* This program is distributed in the hope that it will be useful, |
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
|
* GNU Lesser General Public License for more details. |
19 |
|
|
* |
20 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
21 |
|
|
* along with this program; if not, write to the Free Software |
22 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
|
|
* |
24 |
|
|
*/ |
25 |
|
|
|
26 |
tim |
1725 |
/** |
27 |
|
|
* @file SimCreator.cpp |
28 |
|
|
* @author tlin |
29 |
|
|
* @date 11/03/2004 |
30 |
|
|
* @time 13:51am |
31 |
|
|
* @version 1.0 |
32 |
|
|
*/ |
33 |
|
|
|
34 |
tim |
1807 |
#include <sprng.h> |
35 |
|
|
|
36 |
|
|
#include "brains/MoleculeCreator.hpp" |
37 |
tim |
1725 |
#include "brains/SimCreator.hpp" |
38 |
tim |
1807 |
#include "brains/SimSnapshotManager.hpp" |
39 |
|
|
#include "io/DumpReader.hpp" |
40 |
|
|
#include "io/parse_me.h" |
41 |
|
|
#include "UseTheForce/ForceFieldFactory.hpp" |
42 |
|
|
#include "utils/simError.h" |
43 |
|
|
#include "utils/StringUtils.hpp" |
44 |
|
|
#ifdef IS_MPI |
45 |
|
|
#include "io/mpiBASS.h" |
46 |
|
|
#endif |
47 |
tim |
1733 |
|
48 |
tim |
1703 |
namespace oopse { |
49 |
|
|
|
50 |
tim |
1841 |
void SimCreator::parseFile(const std::string mdFileName, MakeStamps* stamps, Globals* simParams){ |
51 |
tim |
1733 |
|
52 |
tim |
1712 |
#ifdef IS_MPI |
53 |
tim |
1733 |
|
54 |
|
|
if (worldRank == 0) { |
55 |
tim |
1712 |
#endif // is_mpi |
56 |
|
|
|
57 |
tim |
1841 |
simParams->initalize(); |
58 |
|
|
set_interface_stamps(stamps, simParams); |
59 |
tim |
1712 |
|
60 |
tim |
1733 |
#ifdef IS_MPI |
61 |
tim |
1712 |
|
62 |
tim |
1733 |
mpiEventInit(); |
63 |
|
|
|
64 |
tim |
1712 |
#endif |
65 |
|
|
|
66 |
tim |
1733 |
yacc_BASS(mdFileName.c_str()); |
67 |
tim |
1712 |
|
68 |
|
|
#ifdef IS_MPI |
69 |
tim |
1733 |
|
70 |
|
|
throwMPIEvent(NULL); |
71 |
|
|
} else { |
72 |
tim |
1841 |
set_interface_stamps(stamps, simParams); |
73 |
tim |
1733 |
mpiEventInit(); |
74 |
|
|
MPIcheckPoint(); |
75 |
|
|
mpiEventLoop(); |
76 |
|
|
} |
77 |
|
|
|
78 |
tim |
1712 |
#endif |
79 |
|
|
|
80 |
tim |
1703 |
} |
81 |
|
|
|
82 |
tim |
1733 |
SimInfo* SimCreator::createSim(const std::string & mdFileName) { |
83 |
tim |
1712 |
|
84 |
tim |
1733 |
MakeStamps * stamps = new MakeStamps(); |
85 |
tim |
1712 |
|
86 |
tim |
1841 |
Globals * simParams = new Globals(); |
87 |
tim |
1733 |
|
88 |
tim |
1712 |
//parse meta-data file |
89 |
tim |
1841 |
parseFile(mdFileName, stamps, simParams); |
90 |
tim |
1712 |
|
91 |
|
|
//create the force field |
92 |
tim |
1807 |
ForceField * ff = ForceFieldFactory::getInstance()->createForceField( |
93 |
tim |
1841 |
simParams->getForceField()); |
94 |
tim |
1740 |
|
95 |
tim |
1733 |
if (ff == NULL) { |
96 |
tim |
1807 |
sprintf(painCave.errMsg, "ForceField Factory can not create %s force field\n", |
97 |
tim |
1841 |
simParams->getForceField()); |
98 |
tim |
1733 |
painCave.isFatal = 1; |
99 |
|
|
simError(); |
100 |
|
|
} |
101 |
|
|
|
102 |
tim |
1807 |
std::string forcefieldFileName; |
103 |
|
|
forcefieldFileName = ff->getForceFieldFileName(); |
104 |
|
|
|
105 |
tim |
1841 |
if (simParams->haveForceFieldVariant()) { |
106 |
tim |
1807 |
//If the force field has variant, the variant force field name will be |
107 |
|
|
//Base.variant.frc. For exampel EAM.u6.frc |
108 |
|
|
|
109 |
tim |
1841 |
std::string variant = simParams->getForceFieldVariant(); |
110 |
tim |
1807 |
|
111 |
|
|
std::string::size_type pos = forcefieldFileName.rfind(".frc"); |
112 |
|
|
variant = "." + variant; |
113 |
|
|
if (pos != std::string::npos) { |
114 |
|
|
forcefieldFileName.insert(pos, variant); |
115 |
|
|
} else { |
116 |
|
|
//If the default force field file name does not containt .frc suffix, just append the .variant |
117 |
|
|
forcefieldFileName.append(variant); |
118 |
|
|
} |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
ff->parse(forcefieldFileName); |
122 |
|
|
|
123 |
tim |
1733 |
//extract the molecule stamps |
124 |
|
|
std::vector < std::pair<MoleculeStamp *, int> > moleculeStampPairs; |
125 |
tim |
1841 |
compList(stamps, simParams, moleculeStampPairs); |
126 |
tim |
1733 |
|
127 |
tim |
1719 |
//create SimInfo |
128 |
tim |
1841 |
SimInfo * info = new SimInfo(moleculeStampPairs, ff, simParams); |
129 |
tim |
1719 |
|
130 |
tim |
1733 |
//gather parameters (SimCreator only retrieves part of the parameters) |
131 |
tim |
1807 |
gatherParameters(info, mdFileName); |
132 |
tim |
1733 |
|
133 |
tim |
1712 |
//divide the molecules and determine the global index of molecules |
134 |
tim |
1807 |
#ifdef IS_MPI |
135 |
tim |
1733 |
divideMolecules(info); |
136 |
tim |
1807 |
#endif |
137 |
tim |
1733 |
|
138 |
tim |
1712 |
//create the molecules |
139 |
tim |
1719 |
createMolecules(info); |
140 |
tim |
1712 |
|
141 |
tim |
1807 |
|
142 |
|
|
//allocate memory for DataStorage(circular reference, need to break it) |
143 |
|
|
info->setSnapshotManager(new SimSnapshotManager(info)); |
144 |
|
|
|
145 |
tim |
1725 |
//set the global index of atoms, rigidbodies and cutoffgroups (only need to be set once, the |
146 |
|
|
//global index will never change again). Local indices of atoms and rigidbodies are already set by |
147 |
tim |
1733 |
//MoleculeCreator class which actually delegates the responsibility to LocalIndexManager. |
148 |
tim |
1807 |
setGlobalIndex(info); |
149 |
tim |
1733 |
|
150 |
tim |
1807 |
|
151 |
tim |
1733 |
|
152 |
tim |
1725 |
//load initial coordinates, some extra information are pushed into SimInfo's property map ( such as |
153 |
|
|
//eta, chi for NPT integrator) |
154 |
tim |
1733 |
loadCoordinates(info); |
155 |
tim |
1719 |
return info; |
156 |
tim |
1733 |
} |
157 |
tim |
1712 |
|
158 |
tim |
1807 |
void SimCreator::gatherParameters(SimInfo *info, const std::string& mdfile) { |
159 |
tim |
1725 |
|
160 |
|
|
//setup seed for random number generator |
161 |
|
|
int seedValue; |
162 |
tim |
1841 |
Globals * simParams = info->getSimParams(); |
163 |
tim |
1733 |
|
164 |
tim |
1841 |
if (simParams->haveSeed()) { |
165 |
|
|
seedValue = simParams->getSeed(); |
166 |
tim |
1725 |
|
167 |
tim |
1733 |
if (seedValue / 1000000000 == 0) { |
168 |
tim |
1725 |
sprintf(painCave.errMsg, |
169 |
tim |
1733 |
"Seed for sprng library should contain at least 9 digits\n" |
170 |
|
|
"OOPSE will generate a seed for user\n"); |
171 |
|
|
|
172 |
tim |
1725 |
painCave.isFatal = 0; |
173 |
|
|
simError(); |
174 |
|
|
|
175 |
tim |
1733 |
//using seed generated by system instead of invalid seed set by user |
176 |
|
|
|
177 |
tim |
1725 |
#ifndef IS_MPI |
178 |
tim |
1733 |
|
179 |
tim |
1725 |
seedValue = make_sprng_seed(); |
180 |
tim |
1733 |
|
181 |
|
|
#else |
182 |
|
|
|
183 |
|
|
if (worldRank == 0) { |
184 |
tim |
1725 |
seedValue = make_sprng_seed(); |
185 |
|
|
} |
186 |
tim |
1733 |
|
187 |
|
|
MPI_Bcast(&seedValue, 1, MPI_INT, 0, MPI_COMM_WORLD); |
188 |
|
|
|
189 |
|
|
#endif |
190 |
|
|
|
191 |
tim |
1725 |
} //end if (seedValue /1000000000 == 0) |
192 |
tim |
1733 |
} else { |
193 |
|
|
|
194 |
tim |
1725 |
#ifndef IS_MPI |
195 |
tim |
1733 |
|
196 |
tim |
1725 |
seedValue = make_sprng_seed(); |
197 |
tim |
1733 |
|
198 |
|
|
#else |
199 |
|
|
|
200 |
|
|
if (worldRank == 0) { |
201 |
tim |
1725 |
seedValue = make_sprng_seed(); |
202 |
|
|
} |
203 |
tim |
1733 |
|
204 |
|
|
MPI_Bcast(&seedValue, 1, MPI_INT, 0, MPI_COMM_WORLD); |
205 |
|
|
|
206 |
tim |
1725 |
#endif |
207 |
tim |
1733 |
|
208 |
tim |
1841 |
} //end of simParams->haveSeed() |
209 |
tim |
1733 |
|
210 |
tim |
1725 |
info->setSeed(seedValue); |
211 |
|
|
|
212 |
tim |
1733 |
|
213 |
|
|
//figure out the ouput file names |
214 |
tim |
1725 |
std::string prefix; |
215 |
tim |
1733 |
|
216 |
tim |
1725 |
#ifdef IS_MPI |
217 |
tim |
1733 |
|
218 |
|
|
if (worldRank == 0) { |
219 |
tim |
1725 |
#endif // is_mpi |
220 |
tim |
1733 |
|
221 |
tim |
1841 |
if (simParams->haveFinalConfig()) { |
222 |
|
|
prefix = getPrefix(simParams->getFinalConfig()); |
223 |
tim |
1725 |
} else { |
224 |
tim |
1807 |
prefix = getPrefix(mdfile); |
225 |
tim |
1725 |
} |
226 |
tim |
1733 |
|
227 |
|
|
info->setFinalConfigFileName(prefix + ".eor"); |
228 |
tim |
1725 |
info->setDumpFileName(prefix + ".dump"); |
229 |
|
|
info->setStatFileName(prefix + ".stat"); |
230 |
|
|
|
231 |
|
|
#ifdef IS_MPI |
232 |
tim |
1733 |
|
233 |
tim |
1725 |
} |
234 |
|
|
|
235 |
tim |
1733 |
#endif |
236 |
tim |
1725 |
|
237 |
tim |
1712 |
} |
238 |
|
|
|
239 |
|
|
#ifdef IS_MPI |
240 |
tim |
1733 |
void SimCreator::divideMolecules(SimInfo *info) { |
241 |
tim |
1727 |
double numerator; |
242 |
|
|
double denominator; |
243 |
|
|
double precast; |
244 |
|
|
double x; |
245 |
|
|
double y; |
246 |
|
|
double a; |
247 |
|
|
int old_atoms; |
248 |
|
|
int add_atoms; |
249 |
|
|
int new_atoms; |
250 |
|
|
int nTarget; |
251 |
|
|
int done; |
252 |
|
|
int i; |
253 |
|
|
int j; |
254 |
|
|
int loops; |
255 |
|
|
int which_proc; |
256 |
tim |
1733 |
int nProcessors; |
257 |
tim |
1842 |
std::vector<int> atomsPerProc; |
258 |
tim |
1733 |
randomSPRNG myRandom(info->getSeed()); |
259 |
tim |
1842 |
int nGlobalMols = info->getNGlobalMolecules(); |
260 |
|
|
std::vector<int> molToProcMap(nGlobalMols, -1); // default to an error condition: |
261 |
|
|
|
262 |
tim |
1733 |
MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); |
263 |
tim |
1727 |
|
264 |
tim |
1733 |
if (nProcessors > nGlobalMols) { |
265 |
tim |
1727 |
sprintf(painCave.errMsg, |
266 |
|
|
"nProcessors (%d) > nMol (%d)\n" |
267 |
|
|
"\tThe number of processors is larger than\n" |
268 |
|
|
"\tthe number of molecules. This will not result in a \n" |
269 |
|
|
"\tusable division of atoms for force decomposition.\n" |
270 |
|
|
"\tEither try a smaller number of processors, or run the\n" |
271 |
tim |
1733 |
"\tsingle-processor version of OOPSE.\n", nProcessors, nGlobalMols); |
272 |
tim |
1727 |
|
273 |
|
|
painCave.isFatal = 1; |
274 |
|
|
simError(); |
275 |
|
|
} |
276 |
|
|
|
277 |
tim |
1733 |
a = 3.0 * nGlobalMols / info->getNGlobalAtoms(); |
278 |
tim |
1727 |
|
279 |
tim |
1733 |
//initialize atomsPerProc |
280 |
|
|
atomsPerProc.insert(atomsPerProc.end(), nProcessors, 0); |
281 |
tim |
1727 |
|
282 |
tim |
1733 |
if (worldRank == 0) { |
283 |
|
|
numerator = info->getNGlobalAtoms(); |
284 |
|
|
denominator = nProcessors; |
285 |
tim |
1727 |
precast = numerator / denominator; |
286 |
|
|
nTarget = (int)(precast + 0.5); |
287 |
|
|
|
288 |
tim |
1733 |
for(i = 0; i < nGlobalMols; i++) { |
289 |
tim |
1727 |
done = 0; |
290 |
|
|
loops = 0; |
291 |
|
|
|
292 |
|
|
while (!done) { |
293 |
|
|
loops++; |
294 |
|
|
|
295 |
|
|
// Pick a processor at random |
296 |
|
|
|
297 |
tim |
1733 |
which_proc = (int) (myRandom.getRandom() * nProcessors); |
298 |
tim |
1727 |
|
299 |
tim |
1733 |
//get the molecule stamp first |
300 |
|
|
int stampId = info->getMoleculeStampId(i); |
301 |
|
|
MoleculeStamp * moleculeStamp = info->getMoleculeStamp(stampId); |
302 |
tim |
1727 |
|
303 |
tim |
1733 |
// How many atoms does this processor have so far? |
304 |
|
|
old_atoms = atomsPerProc[which_proc]; |
305 |
|
|
add_atoms = moleculeStamp->getNAtoms(); |
306 |
tim |
1727 |
new_atoms = old_atoms + add_atoms; |
307 |
|
|
|
308 |
|
|
// If we've been through this loop too many times, we need |
309 |
|
|
// to just give up and assign the molecule to this processor |
310 |
|
|
// and be done with it. |
311 |
|
|
|
312 |
|
|
if (loops > 100) { |
313 |
tim |
1733 |
sprintf(painCave.errMsg, |
314 |
|
|
"I've tried 100 times to assign molecule %d to a " |
315 |
|
|
" processor, but can't find a good spot.\n" |
316 |
|
|
"I'm assigning it at random to processor %d.\n", |
317 |
|
|
i, which_proc); |
318 |
tim |
1727 |
|
319 |
|
|
painCave.isFatal = 0; |
320 |
|
|
simError(); |
321 |
|
|
|
322 |
tim |
1733 |
molToProcMap[i] = which_proc; |
323 |
|
|
atomsPerProc[which_proc] += add_atoms; |
324 |
tim |
1727 |
|
325 |
|
|
done = 1; |
326 |
|
|
continue; |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
// If we can add this molecule to this processor without sending |
330 |
|
|
// it above nTarget, then go ahead and do it: |
331 |
|
|
|
332 |
|
|
if (new_atoms <= nTarget) { |
333 |
tim |
1733 |
molToProcMap[i] = which_proc; |
334 |
|
|
atomsPerProc[which_proc] += add_atoms; |
335 |
tim |
1727 |
|
336 |
|
|
done = 1; |
337 |
|
|
continue; |
338 |
|
|
} |
339 |
|
|
|
340 |
|
|
// The only situation left is when new_atoms > nTarget. We |
341 |
|
|
// want to accept this with some probability that dies off the |
342 |
|
|
// farther we are from nTarget |
343 |
|
|
|
344 |
|
|
// roughly: x = new_atoms - nTarget |
345 |
|
|
// Pacc(x) = exp(- a * x) |
346 |
|
|
// where a = penalty / (average atoms per molecule) |
347 |
|
|
|
348 |
|
|
x = (double)(new_atoms - nTarget); |
349 |
tim |
1733 |
y = myRandom.getRandom(); |
350 |
tim |
1727 |
|
351 |
|
|
if (y < exp(- a * x)) { |
352 |
tim |
1733 |
molToProcMap[i] = which_proc; |
353 |
|
|
atomsPerProc[which_proc] += add_atoms; |
354 |
tim |
1727 |
|
355 |
|
|
done = 1; |
356 |
|
|
continue; |
357 |
tim |
1733 |
} else { |
358 |
|
|
continue; |
359 |
|
|
} |
360 |
tim |
1727 |
} |
361 |
|
|
} |
362 |
|
|
|
363 |
|
|
// Spray out this nonsense to all other processors: |
364 |
|
|
|
365 |
tim |
1842 |
MPI_Bcast(&molToProcMap[0], nGlobalMols, MPI_INT, 0, MPI_COMM_WORLD); |
366 |
tim |
1727 |
} else { |
367 |
|
|
|
368 |
tim |
1733 |
// Listen to your marching orders from processor 0: |
369 |
tim |
1727 |
|
370 |
tim |
1842 |
MPI_Bcast(&molToProcMap[0], nGlobalMols, MPI_INT, 0, MPI_COMM_WORLD); |
371 |
tim |
1727 |
} |
372 |
|
|
|
373 |
tim |
1842 |
info->setMolToProcMap(molToProcMap); |
374 |
tim |
1727 |
sprintf(checkPointMsg, |
375 |
|
|
"Successfully divided the molecules among the processors.\n"); |
376 |
|
|
MPIcheckPoint(); |
377 |
tim |
1712 |
} |
378 |
tim |
1733 |
|
379 |
tim |
1713 |
#endif |
380 |
tim |
1712 |
|
381 |
tim |
1733 |
void SimCreator::createMolecules(SimInfo *info) { |
382 |
tim |
1725 |
MoleculeCreator molCreator; |
383 |
|
|
int stampId; |
384 |
tim |
1713 |
|
385 |
tim |
1733 |
for(int i = 0; i < info->getNGlobalMolecules(); i++) { |
386 |
tim |
1713 |
|
387 |
tim |
1733 |
#ifdef IS_MPI |
388 |
|
|
|
389 |
|
|
if (info->getMolToProc(i) == worldRank) { |
390 |
|
|
#endif |
391 |
|
|
|
392 |
tim |
1725 |
stampId = info->getMoleculeStampId(i); |
393 |
tim |
1807 |
Molecule * mol = molCreator.createMolecule(info->getForceField(), info->getMoleculeStamp(stampId), |
394 |
|
|
stampId, i, info->getLocalIndexManager()); |
395 |
tim |
1733 |
|
396 |
tim |
1719 |
info->addMolecule(mol); |
397 |
tim |
1733 |
|
398 |
|
|
#ifdef IS_MPI |
399 |
|
|
|
400 |
tim |
1719 |
} |
401 |
tim |
1733 |
|
402 |
|
|
#endif |
403 |
|
|
|
404 |
|
|
} //end for(int i=0) |
405 |
tim |
1713 |
} |
406 |
|
|
|
407 |
tim |
1841 |
void SimCreator::compList(MakeStamps *stamps, Globals* simParams, |
408 |
tim |
1733 |
std::vector < std::pair<MoleculeStamp *, int> > &moleculeStampPairs) { |
409 |
tim |
1719 |
int i; |
410 |
tim |
1733 |
char * id; |
411 |
|
|
MoleculeStamp * currentStamp; |
412 |
tim |
1841 |
Component** the_components = simParams->getComponents(); |
413 |
|
|
int n_components = simParams->getNComponents(); |
414 |
tim |
1719 |
|
415 |
tim |
1841 |
if (!simParams->haveNMol()) { |
416 |
tim |
1719 |
// we don't have the total number of molecules, so we assume it is |
417 |
|
|
// given in each component |
418 |
|
|
|
419 |
tim |
1733 |
for(i = 0; i < n_components; i++) { |
420 |
|
|
if (!the_components[i]->haveNMol()) { |
421 |
tim |
1719 |
// we have a problem |
422 |
|
|
sprintf(painCave.errMsg, |
423 |
tim |
1807 |
"SimCreator Error. No global NMol or component NMol given.\n" |
424 |
tim |
1733 |
"\tCannot calculate the number of atoms.\n"); |
425 |
|
|
|
426 |
tim |
1719 |
painCave.isFatal = 1; |
427 |
|
|
simError(); |
428 |
|
|
} |
429 |
|
|
|
430 |
|
|
id = the_components[i]->getType(); |
431 |
tim |
1807 |
currentStamp = (stamps->extractMolStamp(id))->getStamp(); |
432 |
tim |
1733 |
|
433 |
|
|
if (currentStamp == NULL) { |
434 |
tim |
1719 |
sprintf(painCave.errMsg, |
435 |
tim |
1807 |
"SimCreator error: Component \"%s\" was not found in the " |
436 |
tim |
1733 |
"list of declared molecules\n", id); |
437 |
|
|
|
438 |
tim |
1719 |
painCave.isFatal = 1; |
439 |
|
|
simError(); |
440 |
tim |
1733 |
} |
441 |
tim |
1719 |
|
442 |
tim |
1733 |
moleculeStampPairs.push_back( |
443 |
tim |
1832 |
std::make_pair(currentStamp, the_components[i]->getNMol())); |
444 |
tim |
1719 |
} //end for (i = 0; i < n_components; i++) |
445 |
tim |
1733 |
} else { |
446 |
|
|
sprintf(painCave.errMsg, "SimSetup error.\n" |
447 |
|
|
"\tSorry, the ability to specify total" |
448 |
|
|
" nMols and then give molfractions in the components\n" |
449 |
|
|
"\tis not currently supported." |
450 |
|
|
" Please give nMol in the components.\n"); |
451 |
|
|
|
452 |
tim |
1719 |
painCave.isFatal = 1; |
453 |
|
|
simError(); |
454 |
|
|
} |
455 |
tim |
1733 |
|
456 |
tim |
1719 |
#ifdef IS_MPI |
457 |
tim |
1733 |
|
458 |
|
|
strcpy(checkPointMsg, "Component stamps successfully extracted\n"); |
459 |
|
|
MPIcheckPoint(); |
460 |
|
|
|
461 |
tim |
1719 |
#endif // is_mpi |
462 |
tim |
1733 |
|
463 |
tim |
1719 |
} |
464 |
|
|
|
465 |
tim |
1733 |
void SimCreator::setGlobalIndex(SimInfo *info) { |
466 |
tim |
1807 |
SimInfo::MoleculeIterator mi; |
467 |
|
|
Molecule::AtomIterator ai; |
468 |
|
|
Molecule::RigidBodyIterator ri; |
469 |
|
|
Molecule::CutoffGroupIterator ci; |
470 |
tim |
1733 |
Molecule * mol; |
471 |
|
|
Atom * atom; |
472 |
|
|
RigidBody * rb; |
473 |
|
|
CutoffGroup * cg; |
474 |
tim |
1725 |
int beginAtomIndex; |
475 |
|
|
int beginRigidBodyIndex; |
476 |
|
|
int beginCutoffGroupIndex; |
477 |
tim |
1842 |
int nGlobalAtoms = info->getNGlobalAtoms(); |
478 |
|
|
|
479 |
tim |
1733 |
#ifndef IS_MPI |
480 |
|
|
|
481 |
tim |
1725 |
beginAtomIndex = 0; |
482 |
|
|
beginRigidBodyIndex = 0; |
483 |
|
|
beginCutoffGroupIndex = 0; |
484 |
tim |
1733 |
|
485 |
tim |
1725 |
#else |
486 |
tim |
1733 |
|
487 |
tim |
1725 |
int nproc; |
488 |
|
|
int myNode; |
489 |
|
|
|
490 |
|
|
myNode = worldRank; |
491 |
|
|
MPI_Comm_size(MPI_COMM_WORLD, &nproc); |
492 |
|
|
|
493 |
tim |
1733 |
std::vector < int > tmpAtomsInProc(nproc, 0); |
494 |
|
|
std::vector < int > tmpRigidBodiesInProc(nproc, 0); |
495 |
|
|
std::vector < int > tmpCutoffGroupsInProc(nproc, 0); |
496 |
|
|
std::vector < int > NumAtomsInProc(nproc, 0); |
497 |
|
|
std::vector < int > NumRigidBodiesInProc(nproc, 0); |
498 |
|
|
std::vector < int > NumCutoffGroupsInProc(nproc, 0); |
499 |
|
|
|
500 |
|
|
tmpAtomsInProc[myNode] = info->getNAtoms(); |
501 |
|
|
tmpRigidBodiesInProc[myNode] = info->getNRigidBodies(); |
502 |
|
|
tmpCutoffGroupsInProc[myNode] = info->getNCutoffGroups(); |
503 |
|
|
|
504 |
tim |
1725 |
//do MPI_ALLREDUCE to exchange the total number of atoms, rigidbodies and cutoff groups |
505 |
tim |
1733 |
MPI_Allreduce(&tmpAtomsInProc[0], &NumAtomsInProc[0], nproc, MPI_INT, |
506 |
|
|
MPI_SUM, MPI_COMM_WORLD); |
507 |
|
|
MPI_Allreduce(&tmpRigidBodiesInProc[0], &NumRigidBodiesInProc[0], nproc, |
508 |
|
|
MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
509 |
|
|
MPI_Allreduce(&tmpCutoffGroupsInProc[0], &NumCutoffGroupsInProc[0], nproc, |
510 |
|
|
MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
511 |
tim |
1725 |
|
512 |
|
|
beginAtomIndex = 0; |
513 |
|
|
beginRigidBodyIndex = 0; |
514 |
|
|
beginCutoffGroupIndex = 0; |
515 |
|
|
|
516 |
tim |
1733 |
for(int i = 0; i < nproc; i++) { |
517 |
tim |
1725 |
beginAtomIndex += NumAtomsInProc[i]; |
518 |
|
|
beginRigidBodyIndex += NumRigidBodiesInProc[i]; |
519 |
tim |
1733 |
beginCutoffGroupIndex += NumCutoffGroupsInProc[i]; |
520 |
tim |
1725 |
} |
521 |
tim |
1733 |
|
522 |
tim |
1725 |
#endif |
523 |
|
|
|
524 |
tim |
1733 |
for(mol = info->beginMolecule(mi); mol != NULL; |
525 |
|
|
mol = info->nextMolecule(mi)) { |
526 |
|
|
|
527 |
tim |
1725 |
//local index(index in DataStorge) of atom is important |
528 |
tim |
1733 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
529 |
tim |
1725 |
atom->setGlobalIndex(beginAtomIndex++); |
530 |
|
|
} |
531 |
|
|
|
532 |
tim |
1733 |
for(rb = mol->beginRigidBody(ri); rb != NULL; |
533 |
|
|
rb = mol->nextRigidBody(ri)) { |
534 |
tim |
1725 |
rb->setGlobalIndex(beginRigidBodyIndex++); |
535 |
|
|
} |
536 |
tim |
1733 |
|
537 |
tim |
1725 |
//local index of cutoff group is trivial, it only depends on the order of travesing |
538 |
tim |
1733 |
for(cg = mol->beginCutoffGroup(ci); cg != NULL; |
539 |
|
|
cg = mol->nextCutoffGroup(ci)) { |
540 |
|
|
cg->setGlobalIndex(beginCutoffGroupIndex++); |
541 |
|
|
} |
542 |
|
|
} |
543 |
|
|
|
544 |
tim |
1735 |
//fill globalGroupMembership |
545 |
tim |
1733 |
std::vector<int> globalGroupMembership(info->getNGlobalAtoms(), 0); |
546 |
tim |
1807 |
for(mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { |
547 |
tim |
1725 |
for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) { |
548 |
tim |
1733 |
|
549 |
|
|
for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) { |
550 |
|
|
globalGroupMembership[atom->getGlobalIndex()] = cg->getGlobalIndex(); |
551 |
|
|
} |
552 |
|
|
|
553 |
|
|
} |
554 |
|
|
} |
555 |
tim |
1735 |
|
556 |
|
|
#ifdef IS_MPI |
557 |
tim |
1733 |
// Since the globalGroupMembership has been zero filled and we've only |
558 |
|
|
// poked values into the atoms we know, we can do an Allreduce |
559 |
|
|
// to get the full globalGroupMembership array (We think). |
560 |
|
|
// This would be prettier if we could use MPI_IN_PLACE like the MPI-2 |
561 |
|
|
// docs said we could. |
562 |
tim |
1842 |
std::vector<int> tmpGroupMembership(nGlobalAtoms, 0); |
563 |
|
|
MPI_Allreduce(&globalGroupMembership[0], &tmpGroupMembership[0], nGlobalAtoms, |
564 |
tim |
1733 |
MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
565 |
tim |
1735 |
#else |
566 |
tim |
1842 |
info->setGlobalGroupMembership(globalGroupMembership); |
567 |
tim |
1735 |
#endif |
568 |
tim |
1733 |
|
569 |
|
|
//fill molMembership |
570 |
|
|
std::vector<int> globalMolMembership(info->getNGlobalAtoms(), 0); |
571 |
|
|
|
572 |
|
|
for(mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { |
573 |
|
|
|
574 |
|
|
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
575 |
|
|
globalMolMembership[atom->getGlobalIndex()] = mol->getGlobalIndex(); |
576 |
|
|
} |
577 |
|
|
|
578 |
tim |
1735 |
#ifdef IS_MPI |
579 |
tim |
1842 |
std::vector<int> tmpMolMembership(nGlobalAtoms, 0); |
580 |
|
|
|
581 |
|
|
MPI_Allreduce(&globalMolMembership[0], &tmpMolMembership[0], nGlobalAtoms, |
582 |
tim |
1733 |
MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
583 |
tim |
1842 |
|
584 |
|
|
info->setGlobalMolMembership(tmpMolMembership); |
585 |
tim |
1735 |
#else |
586 |
tim |
1842 |
info->setGlobalMolMembership(globalMolMembership); |
587 |
tim |
1735 |
#endif |
588 |
|
|
|
589 |
tim |
1807 |
} |
590 |
tim |
1733 |
} |
591 |
|
|
|
592 |
|
|
void SimCreator::loadCoordinates(SimInfo* info) { |
593 |
tim |
1841 |
Globals* simParams; |
594 |
|
|
simParams = info->getSimParams(); |
595 |
tim |
1733 |
|
596 |
tim |
1841 |
if (!simParams->haveInitialConfig()) { |
597 |
tim |
1733 |
sprintf(painCave.errMsg, |
598 |
|
|
"Cannot intialize a simulation without an initial configuration file.\n"); |
599 |
|
|
painCave.isFatal = 1;; |
600 |
|
|
simError(); |
601 |
|
|
} |
602 |
tim |
1725 |
|
603 |
tim |
1841 |
DumpReader reader(info, simParams->getInitialConfig()); |
604 |
tim |
1807 |
int nframes = reader.getNFrames(); |
605 |
tim |
1733 |
|
606 |
|
|
if (nframes > 0) { |
607 |
tim |
1807 |
reader.readFrame(nframes - 1); |
608 |
tim |
1733 |
} else { |
609 |
tim |
1735 |
//invalid initial coordinate file |
610 |
tim |
1733 |
sprintf(painCave.errMsg, "Initial configuration file %s should at least contain one frame\n", |
611 |
tim |
1841 |
simParams->getInitialConfig()); |
612 |
tim |
1733 |
painCave.isFatal = 1; |
613 |
|
|
simError(); |
614 |
tim |
1725 |
} |
615 |
tim |
1733 |
|
616 |
tim |
1725 |
} |
617 |
|
|
|
618 |
tim |
1703 |
} //end namespace oopse |
619 |
tim |
1842 |
|
620 |
|
|
|