ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/utils/bilayerSys.cpp
Revision: 504
Committed: Thu Apr 17 21:54:18 2003 UTC (22 years ago) by mmeineke
File size: 5685 byte(s)
Log Message:
fixed up sysBuild to where it should now build our systems

File Contents

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