1 |
#include <cstdlib> |
2 |
#include <cstdio> |
3 |
#include <cstring> |
4 |
#include <cmath> |
5 |
#include <iostream> |
6 |
#include <string> |
7 |
#include <map> |
8 |
#include <fstream> |
9 |
|
10 |
#include "applications/simpleBuilder/simpleBuilderCmd.h" |
11 |
#include "applications/simpleBuilder/LatticeFactory.hpp" |
12 |
#include "applications/simpleBuilder/MoLocator.hpp" |
13 |
#include "applications/simpleBuilder/Lattice.hpp" |
14 |
#include "brains/Register.hpp" |
15 |
#include "brains/SimInfo.hpp" |
16 |
#include "brains/SimCreator.hpp" |
17 |
#include "io/DumpWriter.hpp" |
18 |
#include "math/Vector3.hpp" |
19 |
#include "math/SquareMatrix3.hpp" |
20 |
#include "utils/StringUtils.hpp" |
21 |
|
22 |
using namespace std; |
23 |
using namespace oopse; |
24 |
void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, |
25 |
int numMol); |
26 |
|
27 |
int main(int argc, char *argv []) { |
28 |
|
29 |
//register force fields |
30 |
registerForceFields(); |
31 |
|
32 |
gengetopt_args_info args_info; |
33 |
std::string latticeType; |
34 |
std::string inputFileName; |
35 |
std::string outPrefix; |
36 |
std::string outMdFileName; |
37 |
std::string outInitFileName; |
38 |
BaseLattice *simpleLat; |
39 |
int numMol; |
40 |
double latticeConstant; |
41 |
std::vector<double> lc; |
42 |
double mass; |
43 |
const double rhoConvertConst = 1.661; |
44 |
double density; |
45 |
int nx, |
46 |
ny, |
47 |
nz; |
48 |
Mat3x3d hmat; |
49 |
MoLocator *locator; |
50 |
std::vector<Vector3d> latticePos; |
51 |
std::vector<Vector3d> latticeOrt; |
52 |
int numMolPerCell; |
53 |
int curMolIndex; |
54 |
DumpWriter *writer; |
55 |
|
56 |
// parse command line arguments |
57 |
if (cmdline_parser(argc, argv, &args_info) != 0) |
58 |
exit(1); |
59 |
|
60 |
density = args_info.density_arg; |
61 |
|
62 |
//get lattice type |
63 |
latticeType = UpperCase(args_info.latticetype_arg); |
64 |
|
65 |
if (!LatticeFactory::getInstance()->hasLatticeCreator(latticeType)) { |
66 |
std::cerr << latticeType << " is an invalid lattice type" << std::endl; |
67 |
std::cerr << LatticeFactory::getInstance()->toString() << std::endl; |
68 |
exit(1); |
69 |
} |
70 |
|
71 |
//get the number of unit cell |
72 |
nx = args_info.nx_arg; |
73 |
|
74 |
if (nx <= 0) { |
75 |
std::cerr << "The number of unit cell in h direction must be greater than 0" << std::endl; |
76 |
exit(1); |
77 |
} |
78 |
|
79 |
ny = args_info.ny_arg; |
80 |
|
81 |
if (ny <= 0) { |
82 |
std::cerr << "The number of unit cell in l direction must be greater than 0" << std::endl; |
83 |
exit(1); |
84 |
} |
85 |
|
86 |
nz = args_info.nz_arg; |
87 |
|
88 |
if (nz <= 0) { |
89 |
std::cerr << "The number of unit cell in k direction must be greater than 0" << std::endl; |
90 |
exit(1); |
91 |
} |
92 |
|
93 |
//get input file name |
94 |
if (args_info.inputs_num) |
95 |
inputFileName = args_info.inputs[0]; |
96 |
else { |
97 |
std::cerr << "You must specify a input file name.\n" << std::endl; |
98 |
cmdline_parser_print_help(); |
99 |
exit(1); |
100 |
} |
101 |
|
102 |
//parse md file and set up the system |
103 |
SimCreator oldCreator; |
104 |
SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); |
105 |
|
106 |
if (oldInfo->getNMoleculeStamp()>= 2) { |
107 |
std::cerr << "can not build the system with more than two components" |
108 |
<< std::endl; |
109 |
exit(1); |
110 |
} |
111 |
|
112 |
//get mass of molecule. |
113 |
|
114 |
mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); |
115 |
|
116 |
//creat lattice |
117 |
simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); |
118 |
|
119 |
if (simpleLat == NULL) { |
120 |
std::cerr << "Error in creating lattice" << std::endl; |
121 |
exit(1); |
122 |
} |
123 |
|
124 |
numMolPerCell = simpleLat->getNumSitesPerCell(); |
125 |
|
126 |
//calculate lattice constant (in Angstrom) |
127 |
latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density, |
128 |
1.0 / 3.0); |
129 |
|
130 |
//set lattice constant |
131 |
lc.push_back(latticeConstant); |
132 |
simpleLat->setLatticeConstant(lc); |
133 |
|
134 |
//calculate the total number of molecules |
135 |
numMol = nx * ny * nz * numMolPerCell; |
136 |
|
137 |
if (oldInfo->getNGlobalMolecules() != numMol) { |
138 |
outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType; |
139 |
outMdFileName = outPrefix + ".md"; |
140 |
|
141 |
//creat new .md file on fly which corrects the number of molecule |
142 |
createMdFile(inputFileName, outMdFileName, numMol); |
143 |
std::cerr |
144 |
<< "SimpleBuilder Error: the number of molecule and the density are not matched" |
145 |
<< std::endl; |
146 |
std::cerr << "A new .md file: " << outMdFileName |
147 |
<< " is generated, use it to rerun the simpleBuilder" << std::endl; |
148 |
exit(1); |
149 |
} |
150 |
|
151 |
//determine the output file names |
152 |
if (args_info.output_given) |
153 |
outInitFileName = args_info.output_arg; |
154 |
else |
155 |
outInitFileName = getPrefix(inputFileName.c_str()) + ".in"; |
156 |
|
157 |
//creat Molocator |
158 |
locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); |
159 |
|
160 |
//fill Hmat |
161 |
hmat(0, 0)= nx * latticeConstant; |
162 |
hmat(0, 1) = 0.0; |
163 |
hmat(0, 2) = 0.0; |
164 |
|
165 |
hmat(1, 0) = 0.0; |
166 |
hmat(1, 1) = ny * latticeConstant; |
167 |
hmat(1, 2) = 0.0; |
168 |
|
169 |
hmat(2, 0) = 0.0; |
170 |
hmat(2, 1) = 0.0; |
171 |
hmat(2, 2) = nz * latticeConstant; |
172 |
|
173 |
//set Hmat |
174 |
oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); |
175 |
|
176 |
//place the molecules |
177 |
|
178 |
curMolIndex = 0; |
179 |
|
180 |
//get the orientation of the cell sites |
181 |
//for the same type of molecule in same lattice, it will not change |
182 |
latticeOrt = simpleLat->getLatticePointsOrt(); |
183 |
|
184 |
Molecule* mol; |
185 |
SimInfo::MoleculeIterator mi; |
186 |
mol = oldInfo->beginMolecule(mi); |
187 |
for(int i = 0; i < nx; i++) { |
188 |
for(int j = 0; j < ny; j++) { |
189 |
for(int k = 0; k < nz; k++) { |
190 |
|
191 |
//get the position of the cell sites |
192 |
simpleLat->getLatticePointsPos(latticePos, i, j, k); |
193 |
|
194 |
for(int l = 0; l < numMolPerCell; l++) { |
195 |
if (mol != NULL) { |
196 |
locator->placeMol(latticePos[l], latticeOrt[l], mol); |
197 |
} else { |
198 |
std::cerr << std::endl; |
199 |
} |
200 |
mol = oldInfo->nextMolecule(mi); |
201 |
} |
202 |
} |
203 |
} |
204 |
} |
205 |
|
206 |
//create dumpwriter and write out the coordinates |
207 |
oldInfo->setFinalConfigFileName(outInitFileName); |
208 |
writer = new DumpWriter(oldInfo, oldInfo->getDumpFileName()); |
209 |
|
210 |
if (writer == NULL) { |
211 |
std::cerr << "error in creating DumpWriter" << std::endl; |
212 |
exit(1); |
213 |
} |
214 |
|
215 |
writer->writeDump(); |
216 |
std::cout << "new initial configuration file: " << outInitFileName |
217 |
<< " is generated." << std::endl; |
218 |
|
219 |
//delete objects |
220 |
|
221 |
//delete oldInfo and oldSimSetup |
222 |
if (oldInfo != NULL) |
223 |
delete oldInfo; |
224 |
|
225 |
if (writer != NULL) |
226 |
delete writer; |
227 |
|
228 |
return 0; |
229 |
} |
230 |
|
231 |
void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, |
232 |
int numMol) { |
233 |
ifstream oldMdFile; |
234 |
ofstream newMdFile; |
235 |
const int MAXLEN = 65535; |
236 |
char buffer[MAXLEN]; |
237 |
|
238 |
//create new .md file based on old .md file |
239 |
oldMdFile.open(oldMdFileName.c_str()); |
240 |
newMdFile.open(newMdFileName.c_str()); |
241 |
|
242 |
oldMdFile.getline(buffer, MAXLEN); |
243 |
|
244 |
while (!oldMdFile.eof()) { |
245 |
|
246 |
//correct molecule number |
247 |
if (strstr(buffer, "nMol") != NULL) { |
248 |
sprintf(buffer, "\t\tnMol = %d;", numMol); |
249 |
newMdFile << buffer << std::endl; |
250 |
} else |
251 |
newMdFile << buffer << std::endl; |
252 |
|
253 |
oldMdFile.getline(buffer, MAXLEN); |
254 |
} |
255 |
|
256 |
oldMdFile.close(); |
257 |
newMdFile.close(); |
258 |
} |
259 |
|