1 |
#include <cstdlib> |
2 |
#include <cstring> |
3 |
#include <cmath> |
4 |
|
5 |
#include "simError.h" |
6 |
#include "SimInfo.hpp" |
7 |
#include "ReadWrite.hpp" |
8 |
|
9 |
#include "MoLocator.hpp" |
10 |
#include "sysBuild.hpp" |
11 |
#include "bilayerSys.hpp" |
12 |
|
13 |
|
14 |
int buildRandomBilayer( void ); |
15 |
|
16 |
void getRandomRot( double rot[3][3] ); |
17 |
|
18 |
int buildBilayer( int isRandom ){ |
19 |
|
20 |
if( isRandom ){ |
21 |
return buildRandomBilayer(); |
22 |
} |
23 |
else{ |
24 |
sprintf( painCave.errMsg, |
25 |
"Cannot currently create a non-random bilayer.\n" ); |
26 |
painCave.isFatal = 1; |
27 |
simError(); |
28 |
return 0; |
29 |
} |
30 |
} |
31 |
|
32 |
|
33 |
|
34 |
int buildRandomBilayer( void ){ |
35 |
|
36 |
int i,j,k; |
37 |
int nAtoms, atomIndex, molIndex, molID; |
38 |
int* molSeq; |
39 |
int* molMap; |
40 |
int* cardDeck; |
41 |
int deckSize; |
42 |
int rSite, rCard; |
43 |
double cell; |
44 |
int nCells, nSites, siteIndex; |
45 |
double rot[3][3]; |
46 |
double pos[3]; |
47 |
|
48 |
Atom** atoms; |
49 |
SimInfo* simnfo; |
50 |
DumpWriter* writer; |
51 |
MoLocator** locate; |
52 |
|
53 |
// initialize functions and variables |
54 |
|
55 |
srand48( RAND_SEED ); |
56 |
molSeq = NULL; |
57 |
molMap = NULL; |
58 |
cardDeck = NULL; |
59 |
atoms = NULL; |
60 |
locate = NULL; |
61 |
simnfo = NULL; |
62 |
writer = NULL; |
63 |
|
64 |
// calculate the number of cells in the fcc box |
65 |
|
66 |
nCells = 0; |
67 |
nSites = 0; |
68 |
while( nSites < bsInfo.totNmol ){ |
69 |
nCells++; |
70 |
nSites = 4.0 * pow( (double)nCells, 3.0 ); |
71 |
} |
72 |
|
73 |
|
74 |
// create the molMap and cardDeck arrays |
75 |
|
76 |
molMap = new int[nSites]; |
77 |
cardDeck = new int[nSites]; |
78 |
|
79 |
for(i=0; i<nSites; i++){ |
80 |
molMap[i] = -1; |
81 |
cardDeck[i] = i; |
82 |
} |
83 |
|
84 |
// randomly place the molecules on the sites |
85 |
|
86 |
deckSize = nSites; |
87 |
for(i=0; i<bsInfo.totNmol; i++){ |
88 |
rCard = (int)( deckSize * drand48() ); |
89 |
rSite = cardDeck[rCard]; |
90 |
molMap[rSite] = i; |
91 |
|
92 |
// book keep the card deck; |
93 |
|
94 |
deckSize--; |
95 |
cardDeck[rCard] = cardDeck[deckSize]; |
96 |
} |
97 |
|
98 |
|
99 |
// create the MoLocator and Atom arrays |
100 |
|
101 |
nAtoms = 0; |
102 |
molIndex = 0; |
103 |
locate = new MoLocator*[bsInfo.nComponents]; |
104 |
molSeq = new int[bsInfo.totNmol]; |
105 |
for(i=0; i<bsInfo.nComponents; i++){ |
106 |
locate[i] = new MoLocator( bsInfo.compStamps[i] ); |
107 |
for(j=0; j<bsInfo.componentsNmol[i]; j++){ |
108 |
molSeq[molIndex] = i; |
109 |
molIndex++; |
110 |
nAtoms += bsInfo.compStamps[i]->getNAtoms(); |
111 |
} |
112 |
} |
113 |
|
114 |
Atom::createArrays( nAtoms ); |
115 |
atoms = new Atom*[nAtoms]; |
116 |
|
117 |
|
118 |
// place the molecules at each FCC site |
119 |
|
120 |
cell = 5.0; |
121 |
for(i=0; i<bsInfo.nComponents; i++){ |
122 |
if(cell < locate[i]->getMaxLength() ) cell = locate[i]->getMaxLength(); |
123 |
} |
124 |
cell *= M_SQRT2; |
125 |
|
126 |
siteIndex = 0; |
127 |
atomIndex = 0; |
128 |
for(i=0; i<nCells; i++){ |
129 |
for(j=0; j<nCells; j++){ |
130 |
for(k=0; k<nCells; k++){ |
131 |
|
132 |
if( molMap[siteIndex] >= 0 ){ |
133 |
pos[0] = i * cell; |
134 |
pos[1] = j * cell; |
135 |
pos[2] = k * cell; |
136 |
|
137 |
getRandomRot( rot ); |
138 |
molID = molSeq[molMap[siteIndex]]; |
139 |
locate[molID]->placeMol( pos, rot, &atoms[atomIndex] ); |
140 |
|
141 |
atomIndex += bsInfo.compStamps[molID]->getNAtoms(); |
142 |
} |
143 |
siteIndex++; |
144 |
|
145 |
if( molMap[siteIndex] >= 0 ){ |
146 |
pos[0] = i * cell + (0.5 * cell); |
147 |
pos[1] = j * cell; |
148 |
pos[2] = k * cell + (0.5 * cell); |
149 |
|
150 |
getRandomRot( rot ); |
151 |
molID = molSeq[molMap[siteIndex]]; |
152 |
locate[molID]->placeMol( pos, rot, &atoms[atomIndex] ); |
153 |
|
154 |
atomIndex += bsInfo.compStamps[molID]->getNAtoms(); |
155 |
} |
156 |
siteIndex++; |
157 |
|
158 |
if( molMap[siteIndex] >= 0 ){ |
159 |
pos[0] = i * cell + (0.5 * cell); |
160 |
pos[1] = j * cell + (0.5 * cell); |
161 |
pos[2] = k * cell; |
162 |
|
163 |
getRandomRot( rot ); |
164 |
molID = molSeq[molMap[siteIndex]]; |
165 |
locate[molID]->placeMol( pos, rot, &atoms[atomIndex] ); |
166 |
|
167 |
atomIndex += bsInfo.compStamps[molID]->getNAtoms(); |
168 |
} |
169 |
siteIndex++; |
170 |
|
171 |
if( molMap[siteIndex] >= 0 ){ |
172 |
pos[0] = i * cell; |
173 |
pos[1] = j * cell + (0.5 * cell); |
174 |
pos[2] = k * cell + (0.5 * cell); |
175 |
|
176 |
getRandomRot( rot ); |
177 |
molID = molSeq[molMap[siteIndex]]; |
178 |
locate[molID]->placeMol( pos, rot, &atoms[atomIndex] ); |
179 |
|
180 |
atomIndex += bsInfo.compStamps[molID]->getNAtoms(); |
181 |
} |
182 |
siteIndex++; |
183 |
} |
184 |
} |
185 |
} |
186 |
|
187 |
// set up the SimInfo object |
188 |
|
189 |
bsInfo.boxX = nCells * cell; |
190 |
bsInfo.boxY = nCells * cell; |
191 |
bsInfo.boxZ = nCells * cell; |
192 |
|
193 |
simnfo = new SimInfo(); |
194 |
simnfo->n_atoms = nAtoms; |
195 |
simnfo->box_x = bsInfo.boxX; |
196 |
simnfo->box_y = bsInfo.boxY; |
197 |
simnfo->box_z = bsInfo.boxZ; |
198 |
|
199 |
sprintf( simnfo->statusName, "%s.dump", bsInfo.outPrefix ); |
200 |
sprintf( simnfo->finalName, "%s.init", bsInfo.outPrefix ); |
201 |
|
202 |
simnfo->atoms = atoms; |
203 |
|
204 |
// set up the writer and write out |
205 |
|
206 |
writer = new DumpWriter( simnfo ); |
207 |
writer->writeFinal(); |
208 |
|
209 |
// clean up the memory |
210 |
|
211 |
if( molMap != NULL ) delete[] molMap; |
212 |
if( cardDeck != NULL ) delete[] cardDeck; |
213 |
if( locate != NULL ){ |
214 |
for(i=0; i<bsInfo.nComponents; i++){ |
215 |
delete locate[i]; |
216 |
} |
217 |
delete[] locate; |
218 |
} |
219 |
if( atoms != NULL ){ |
220 |
for(i=0; i<nAtoms; i++){ |
221 |
delete atoms[i]; |
222 |
} |
223 |
Atom::destroyArrays(); |
224 |
delete[] atoms; |
225 |
} |
226 |
if( molSeq != NULL ) delete[] molSeq; |
227 |
if( simnfo != NULL ) delete simnfo; |
228 |
if( writer != NULL ) delete writer; |
229 |
|
230 |
return 1; |
231 |
} |
232 |
|
233 |
|
234 |
void getRandomRot( double rot[3][3] ){ |
235 |
|
236 |
double theta, phi, psi; |
237 |
double cosTheta; |
238 |
|
239 |
// select random phi, psi, and cosTheta |
240 |
|
241 |
phi = 2.0 * M_PI * drand48(); |
242 |
psi = 2.0 * M_PI * drand48(); |
243 |
cosTheta = (2.0 * drand48()) - 1.0; // sample cos -1 to 1 |
244 |
|
245 |
theta = acos( cosTheta ); |
246 |
|
247 |
rot[0][0] = (cos(phi) * cos(psi)) - (sin(phi) * cos(theta) * sin(psi)); |
248 |
rot[0][1] = (sin(phi) * cos(psi)) + (cos(phi) * cos(theta) * sin(psi)); |
249 |
rot[0][2] = sin(theta) * sin(psi); |
250 |
|
251 |
rot[1][0] = -(cos(phi) * sin(psi)) - (sin(phi) * cos(theta) * cos(psi)); |
252 |
rot[1][1] = -(sin(phi) * sin(psi)) + (cos(phi) * cos(theta) * cos(psi)); |
253 |
rot[1][2] = sin(theta) * cos(psi); |
254 |
|
255 |
rot[2][0] = sin(phi) * sin(theta); |
256 |
rot[2][1] = -cos(phi) * sin(theta); |
257 |
rot[2][2] = cos(theta); |
258 |
} |
259 |
|