ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/hydrodynamics/ApproximationModel.cpp
Revision: 1668
Committed: Fri Jan 6 19:03:05 2012 UTC (13 years, 3 months ago) by gezelter
File size: 15787 byte(s)
Log Message:
Some fixes for CMake and single precision builds

File Contents

# User Rev Content
1 tim 906 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9 gezelter 1390 * 1. Redistributions of source code must retain the above copyright
10 tim 906 * notice, this list of conditions and the following disclaimer.
11     *
12 gezelter 1390 * 2. Redistributions in binary form must reproduce the above copyright
13 tim 906 * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the
15     * distribution.
16     *
17     * This software is provided "AS IS," without a warranty of any
18     * kind. All express or implied conditions, representations and
19     * warranties, including any implied warranty of merchantability,
20     * fitness for a particular purpose or non-infringement, are hereby
21     * excluded. The University of Notre Dame and its licensors shall not
22     * be liable for any damages suffered by licensee as a result of
23     * using, modifying or distributing the software or its
24     * derivatives. In no event will the University of Notre Dame or its
25     * licensors be liable for any lost revenue, profit or data, or for
26     * direct, indirect, special, consequential, incidental or punitive
27     * damages, however caused and regardless of the theory of liability,
28     * arising out of the use of or inability to use software, even if the
29     * University of Notre Dame has been advised of the possibility of
30     * such damages.
31 gezelter 1390 *
32     * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
33     * research, please cite the appropriate papers when you publish your
34     * work. Good starting points are:
35     *
36     * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
37     * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
38     * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).
39 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 tim 906 */
42    
43     #include "applications/hydrodynamics/ApproximationModel.hpp"
44     #include "math/LU.hpp"
45     #include "math/DynamicRectMatrix.hpp"
46     #include "math/SquareMatrix3.hpp"
47 gezelter 1390 #include "utils/PhysicalConstants.hpp"
48 gezelter 972 #include "hydrodynamics/Sphere.hpp"
49     #include "hydrodynamics/Ellipsoid.hpp"
50 tim 906 #include "applications/hydrodynamics/CompositeShape.hpp"
51     #include "math/LU.hpp"
52 tim 921 #include "utils/simError.h"
53 gezelter 1390 namespace OpenMD {
54 tim 906 /**
55     * Reference:
56     * Beatriz Carrasco and Jose Gracia de la Torre, Hydrodynamic Properties of Rigid Particles:
57     * Comparison of Different Modeling and Computational Procedures.
58     * Biophysical Journal, 75(6), 3044, 1999
59     */
60    
61 gezelter 972 ApproximationModel::ApproximationModel(StuntDouble* sd, SimInfo* info): HydrodynamicsModel(sd, info){
62     }
63    
64     void ApproximationModel::init() {
65 tim 906 if (!createBeads(beads_)) {
66 tim 921 sprintf(painCave.errMsg, "ApproximationModel::init() : Can not create beads\n");
67     painCave.isFatal = 1;
68     simError();
69 tim 906 }
70 gezelter 972
71     }
72    
73 tim 977 bool ApproximationModel::calcHydroProps(Shape* shape, RealType viscosity, RealType temperature) {
74 gezelter 972
75 tim 906 bool ret = true;
76 xsun 1177 HydroProp* cr = new HydroProp();
77     HydroProp* cd = new HydroProp();
78 tim 906 calcHydroPropsAtCR(beads_, viscosity, temperature, cr);
79 xsun 1177 calcHydroPropsAtCD(beads_, viscosity, temperature, cd);
80 tim 906 setCR(cr);
81     setCD(cd);
82     return true;
83 gezelter 972 }
84    
85 gezelter 981 bool ApproximationModel::calcHydroPropsAtCR(std::vector<BeadParam>& beads, RealType viscosity, RealType temperature, HydroProp* cr) {
86 gezelter 972
87 tim 906 int nbeads = beads.size();
88 tim 977 DynamicRectMatrix<RealType> B(3*nbeads, 3*nbeads);
89     DynamicRectMatrix<RealType> C(3*nbeads, 3*nbeads);
90 tim 906 Mat3x3d I;
91     I(0, 0) = 1.0;
92     I(1, 1) = 1.0;
93     I(2, 2) = 1.0;
94    
95     for (std::size_t i = 0; i < nbeads; ++i) {
96 gezelter 972 for (std::size_t j = 0; j < nbeads; ++j) {
97     Mat3x3d Tij;
98 tim 906 if (i != j ) {
99 gezelter 972 Vector3d Rij = beads[i].pos - beads[j].pos;
100 tim 977 RealType rij = Rij.length();
101     RealType rij2 = rij * rij;
102     RealType sumSigma2OverRij2 = ((beads[i].radius*beads[i].radius) + (beads[j].radius*beads[j].radius)) / rij2;
103 gezelter 972 Mat3x3d tmpMat;
104     tmpMat = outProduct(Rij, Rij) / rij2;
105 tim 977 RealType constant = 8.0 * NumericConstant::PI * viscosity * rij;
106 tim 978 RealType tmp1 = 1.0 + sumSigma2OverRij2/3.0;
107     RealType tmp2 = 1.0 - sumSigma2OverRij2;
108     Tij = (tmp1 * I + tmp2 * tmpMat ) / constant;
109 tim 906 }else {
110 tim 977 RealType constant = 1.0 / (6.0 * NumericConstant::PI * viscosity * beads[i].radius);
111 gezelter 972 Tij(0, 0) = constant;
112     Tij(1, 1) = constant;
113     Tij(2, 2) = constant;
114 tim 906 }
115     B.setSubMatrix(i*3, j*3, Tij);
116 gezelter 972 }
117 tim 906 }
118 gezelter 972
119 tim 906 //invert B Matrix
120     invertMatrix(B, C);
121 tim 910
122 tim 906 //prepare U Matrix relative to arbitrary origin O(0.0, 0.0, 0.0)
123     std::vector<Mat3x3d> U;
124     for (int i = 0; i < nbeads; ++i) {
125 gezelter 972 Mat3x3d currU;
126     currU.setupSkewMat(beads[i].pos);
127     U.push_back(currU);
128 tim 906 }
129    
130     //calculate Xi matrix at arbitrary origin O
131     Mat3x3d Xiott;
132     Mat3x3d Xiorr;
133     Mat3x3d Xiotr;
134 gezelter 972
135 tim 906 //calculate the total volume
136 gezelter 972
137 tim 977 RealType volume = 0.0;
138 tim 906 for (std::vector<BeadParam>::iterator iter = beads.begin(); iter != beads.end(); ++iter) {
139 gezelter 972 volume += 4.0/3.0 * NumericConstant::PI * pow((*iter).radius,3);
140 tim 906 }
141 gezelter 972
142     for (std::size_t i = 0; i < nbeads; ++i) {
143     for (std::size_t j = 0; j < nbeads; ++j) {
144     Mat3x3d Cij;
145     C.getSubMatrix(i*3, j*3, Cij);
146 tim 906
147 gezelter 972 Xiott += Cij;
148     Xiotr += U[i] * Cij;
149 xsun 1208 // uncorrected here. Volume correction is added after we assemble Xiorr
150 gezelter 972 Xiorr += -U[i] * Cij * U[j];
151     }
152 tim 906 }
153 xsun 1208
154     // add the volume correction
155 gezelter 1668 Xiorr += (RealType(6.0) * viscosity * volume) * I;
156 gezelter 972
157 gezelter 1390 Xiott *= PhysicalConstants::viscoConvert;
158     Xiotr *= PhysicalConstants::viscoConvert;
159     Xiorr *= PhysicalConstants::viscoConvert;
160 tim 906
161     Mat3x3d tmp;
162     Mat3x3d tmpInv;
163     Vector3d tmpVec;
164     tmp(0, 0) = Xiott(1, 1) + Xiott(2, 2);
165     tmp(0, 1) = - Xiott(0, 1);
166     tmp(0, 2) = -Xiott(0, 2);
167     tmp(1, 0) = -Xiott(0, 1);
168     tmp(1, 1) = Xiott(0, 0) + Xiott(2, 2);
169     tmp(1, 2) = -Xiott(1, 2);
170     tmp(2, 0) = -Xiott(0, 2);
171     tmp(2, 1) = -Xiott(1, 2);
172     tmp(2, 2) = Xiott(1, 1) + Xiott(0, 0);
173     tmpVec[0] = Xiotr(2, 1) - Xiotr(1, 2);
174     tmpVec[1] = Xiotr(0, 2) - Xiotr(2, 0);
175     tmpVec[2] = Xiotr(1, 0) - Xiotr(0, 1);
176     tmpInv = tmp.inverse();
177     Vector3d ror = tmpInv * tmpVec; //center of resistance
178     Mat3x3d Uor;
179     Uor.setupSkewMat(ror);
180    
181     Mat3x3d Xirtt;
182     Mat3x3d Xirrr;
183     Mat3x3d Xirtr;
184    
185     Xirtt = Xiott;
186     Xirtr = (Xiotr - Uor * Xiott);
187     Xirrr = Xiorr - Uor * Xiott * Uor + Xiotr * Uor - Uor * Xiotr.transpose();
188    
189    
190 tim 977 SquareMatrix<RealType,6> Xir6x6;
191     SquareMatrix<RealType,6> Dr6x6;
192 tim 906
193     Xir6x6.setSubMatrix(0, 0, Xirtt);
194     Xir6x6.setSubMatrix(0, 3, Xirtr.transpose());
195     Xir6x6.setSubMatrix(3, 0, Xirtr);
196     Xir6x6.setSubMatrix(3, 3, Xirrr);
197    
198     invertMatrix(Xir6x6, Dr6x6);
199     Mat3x3d Drtt;
200     Mat3x3d Drtr;
201     Mat3x3d Drrt;
202     Mat3x3d Drrr;
203     Dr6x6.getSubMatrix(0, 0, Drtt);
204     Dr6x6.getSubMatrix(0, 3, Drrt);
205     Dr6x6.getSubMatrix(3, 0, Drtr);
206     Dr6x6.getSubMatrix(3, 3, Drrr);
207 gezelter 1390 RealType kt = PhysicalConstants::kb * temperature ; // in kcal mol^-1
208 tim 906 Drtt *= kt;
209     Drrt *= kt;
210     Drtr *= kt;
211     Drrr *= kt;
212 gezelter 1390 //Xirtt *= PhysicalConstants::kb * temperature;
213     //Xirtr *= PhysicalConstants::kb * temperature;
214     //Xirrr *= PhysicalConstants::kb * temperature;
215 tim 906
216 gezelter 981 Mat6x6d Xi, D;
217 tim 906
218 gezelter 981 cr->setCOR(ror);
219    
220     Xi.setSubMatrix(0, 0, Xirtt);
221     Xi.setSubMatrix(0, 3, Xirtr);
222     Xi.setSubMatrix(3, 0, Xirtr);
223     Xi.setSubMatrix(3, 3, Xirrr);
224    
225     cr->setXi(Xi);
226    
227     D.setSubMatrix(0, 0, Drtt);
228     D.setSubMatrix(0, 3, Drrt);
229     D.setSubMatrix(3, 0, Drtr);
230     D.setSubMatrix(3, 3, Drrr);
231    
232     cr->setD(D);
233 tim 906
234     std::cout << "-----------------------------------------\n";
235     std::cout << "center of resistance :" << std::endl;
236     std::cout << ror << std::endl;
237     std::cout << "resistant tensor at center of resistance" << std::endl;
238     std::cout << "translation:" << std::endl;
239     std::cout << Xirtt << std::endl;
240     std::cout << "translation-rotation:" << std::endl;
241     std::cout << Xirtr << std::endl;
242     std::cout << "rotation:" << std::endl;
243     std::cout << Xirrr << std::endl;
244     std::cout << "diffusion tensor at center of resistance" << std::endl;
245     std::cout << "translation:" << std::endl;
246     std::cout << Drtt << std::endl;
247     std::cout << "rotation-translation:" << std::endl;
248     std::cout << Drrt << std::endl;
249     std::cout << "translation-rotation:" << std::endl;
250     std::cout << Drtr << std::endl;
251     std::cout << "rotation:" << std::endl;
252     std::cout << Drrr << std::endl;
253     std::cout << "-----------------------------------------\n";
254    
255     return true;
256     }
257 gezelter 972
258 xsun 1177 bool ApproximationModel::calcHydroPropsAtCD(std::vector<BeadParam>& beads, RealType viscosity, RealType temperature, HydroProp* cd) {
259 gezelter 972
260 tim 906 int nbeads = beads.size();
261 tim 977 DynamicRectMatrix<RealType> B(3*nbeads, 3*nbeads);
262     DynamicRectMatrix<RealType> C(3*nbeads, 3*nbeads);
263 tim 906 Mat3x3d I;
264     I(0, 0) = 1.0;
265     I(1, 1) = 1.0;
266     I(2, 2) = 1.0;
267    
268     for (std::size_t i = 0; i < nbeads; ++i) {
269 gezelter 972 for (std::size_t j = 0; j < nbeads; ++j) {
270     Mat3x3d Tij;
271     if (i != j ) {
272     Vector3d Rij = beads[i].pos - beads[j].pos;
273 tim 977 RealType rij = Rij.length();
274     RealType rij2 = rij * rij;
275     RealType sumSigma2OverRij2 = ((beads[i].radius*beads[i].radius) + (beads[j].radius*beads[j].radius)) / rij2;
276 gezelter 972 Mat3x3d tmpMat;
277     tmpMat = outProduct(Rij, Rij) / rij2;
278 tim 977 RealType constant = 8.0 * NumericConstant::PI * viscosity * rij;
279 tim 978 RealType tmp1 = 1.0 + sumSigma2OverRij2/3.0;
280     RealType tmp2 = 1.0 - sumSigma2OverRij2;
281     Tij = (tmp1 * I + tmp2 * tmpMat ) / constant;
282 gezelter 972 }else {
283 tim 977 RealType constant = 1.0 / (6.0 * NumericConstant::PI * viscosity * beads[i].radius);
284 gezelter 972 Tij(0, 0) = constant;
285     Tij(1, 1) = constant;
286     Tij(2, 2) = constant;
287 tim 906 }
288 gezelter 972 B.setSubMatrix(i*3, j*3, Tij);
289     }
290 tim 906 }
291 gezelter 972
292 tim 906 //invert B Matrix
293     invertMatrix(B, C);
294 gezelter 972
295 tim 906 //prepare U Matrix relative to arbitrary origin O(0.0, 0.0, 0.0)
296     std::vector<Mat3x3d> U;
297     for (int i = 0; i < nbeads; ++i) {
298 gezelter 972 Mat3x3d currU;
299     currU.setupSkewMat(beads[i].pos);
300     U.push_back(currU);
301 tim 906 }
302    
303     //calculate Xi matrix at arbitrary origin O
304     Mat3x3d Xitt;
305     Mat3x3d Xirr;
306     Mat3x3d Xitr;
307    
308     //calculate the total volume
309    
310 tim 977 RealType volume = 0.0;
311 tim 906 for (std::vector<BeadParam>::iterator iter = beads.begin(); iter != beads.end(); ++iter) {
312 gezelter 972 volume += 4.0/3.0 * NumericConstant::PI * pow((*iter).radius,3);
313 tim 906 }
314 gezelter 972
315 tim 906 for (std::size_t i = 0; i < nbeads; ++i) {
316 gezelter 972 for (std::size_t j = 0; j < nbeads; ++j) {
317     Mat3x3d Cij;
318     C.getSubMatrix(i*3, j*3, Cij);
319 tim 906
320 gezelter 972 Xitt += Cij;
321     Xitr += U[i] * Cij;
322 xsun 1208 // uncorrected here. Volume correction is added after we assemble Xiorr
323 gezelter 972 Xirr += -U[i] * Cij * U[j];
324     }
325 tim 906 }
326 xsun 1208 // add the volume correction here:
327 gezelter 1668 Xirr += (RealType(6.0) * viscosity * volume) * I;
328 gezelter 972
329 gezelter 1390 Xitt *= PhysicalConstants::viscoConvert;
330     Xitr *= PhysicalConstants::viscoConvert;
331     Xirr *= PhysicalConstants::viscoConvert;
332 gezelter 972
333 gezelter 1390 RealType kt = PhysicalConstants::kb * temperature; // in kcal mol^-1
334 gezelter 972
335 tim 906 Mat3x3d Dott; //translational diffusion tensor at arbitrary origin O
336     Mat3x3d Dorr; //rotational diffusion tensor at arbitrary origin O
337     Mat3x3d Dotr; //translation-rotation couplingl diffusion tensor at arbitrary origin O
338 gezelter 972
339 tim 906 const static Mat3x3d zeroMat(0.0);
340    
341     Mat3x3d XittInv(0.0);
342     XittInv = Xitt.inverse();
343    
344     Mat3x3d XirrInv;
345     XirrInv = Xirr.inverse();
346    
347     Mat3x3d tmp;
348     Mat3x3d tmpInv;
349     tmp = Xitt - Xitr.transpose() * XirrInv * Xitr;
350     tmpInv = tmp.inverse();
351    
352     Dott = tmpInv;
353     Dotr = -XirrInv * Xitr * tmpInv;
354    
355     tmp = Xirr - Xitr * XittInv * Xitr.transpose();
356     tmpInv = tmp.inverse();
357    
358     Dorr = tmpInv;
359    
360     //calculate center of diffusion
361     tmp(0, 0) = Dorr(1, 1) + Dorr(2, 2);
362     tmp(0, 1) = - Dorr(0, 1);
363     tmp(0, 2) = -Dorr(0, 2);
364     tmp(1, 0) = -Dorr(0, 1);
365     tmp(1, 1) = Dorr(0, 0) + Dorr(2, 2);
366     tmp(1, 2) = -Dorr(1, 2);
367     tmp(2, 0) = -Dorr(0, 2);
368     tmp(2, 1) = -Dorr(1, 2);
369     tmp(2, 2) = Dorr(1, 1) + Dorr(0, 0);
370    
371     Vector3d tmpVec;
372     tmpVec[0] = Dotr(1, 2) - Dotr(2, 1);
373     tmpVec[1] = Dotr(2, 0) - Dotr(0, 2);
374     tmpVec[2] = Dotr(0, 1) - Dotr(1, 0);
375    
376     tmpInv = tmp.inverse();
377    
378     Vector3d rod = tmpInv * tmpVec;
379    
380     //calculate Diffusion Tensor at center of diffusion
381     Mat3x3d Uod;
382     Uod.setupSkewMat(rod);
383    
384     Mat3x3d Ddtt; //translational diffusion tensor at diffusion center
385     Mat3x3d Ddtr; //rotational diffusion tensor at diffusion center
386     Mat3x3d Ddrr; //translation-rotation couplingl diffusion tensor at diffusion tensor
387    
388     Ddtt = Dott - Uod * Dorr * Uod + Dotr.transpose() * Uod - Uod * Dotr;
389     Ddrr = Dorr;
390     Ddtr = Dotr + Dorr * Uod;
391    
392 tim 977 SquareMatrix<RealType, 6> Dd;
393 tim 906 Dd.setSubMatrix(0, 0, Ddtt);
394     Dd.setSubMatrix(0, 3, Ddtr.transpose());
395     Dd.setSubMatrix(3, 0, Ddtr);
396     Dd.setSubMatrix(3, 3, Ddrr);
397 tim 977 SquareMatrix<RealType, 6> Xid;
398 tim 906 Ddtt *= kt;
399     Ddtr *=kt;
400     Ddrr *= kt;
401     invertMatrix(Dd, Xid);
402    
403    
404    
405     //Xidtt in units of kcal*fs*mol^-1*Ang^-2
406 gezelter 1390 //Xid /= PhysicalConstants::energyConvert;
407     Xid *= PhysicalConstants::kb * temperature;
408 tim 906
409 gezelter 981 Mat6x6d Xi, D;
410 tim 906
411 xsun 1177 cd->setCOR(rod);
412 gezelter 981
413 xsun 1177 cd->setXi(Xid);
414 gezelter 981
415     D.setSubMatrix(0, 0, Ddtt);
416     D.setSubMatrix(0, 3, Ddtr);
417     D.setSubMatrix(3, 0, Ddtr);
418     D.setSubMatrix(3, 3, Ddrr);
419    
420 xsun 1177 cd->setD(D);
421 gezelter 981
422 tim 906 std::cout << "viscosity = " << viscosity << std::endl;
423     std::cout << "temperature = " << temperature << std::endl;
424     std::cout << "center of diffusion :" << std::endl;
425     std::cout << rod << std::endl;
426     std::cout << "diffusion tensor at center of diffusion " << std::endl;
427 xsun 1208 std::cout << "translation(A^2 / fs) :" << std::endl;
428 tim 906 std::cout << Ddtt << std::endl;
429 xsun 1208 std::cout << "translation-rotation(A / fs):" << std::endl;
430 tim 906 std::cout << Ddtr << std::endl;
431 xsun 1208 std::cout << "rotation(fs^-1):" << std::endl;
432 tim 906 std::cout << Ddrr << std::endl;
433    
434     std::cout << "resistance tensor at center of diffusion " << std::endl;
435     std::cout << "translation(kcal*fs*mol^-1*Ang^-2) :" << std::endl;
436    
437     Mat3x3d Xidtt;
438     Mat3x3d Xidrt;
439     Mat3x3d Xidtr;
440     Mat3x3d Xidrr;
441     Xid.getSubMatrix(0, 0, Xidtt);
442     Xid.getSubMatrix(0, 3, Xidrt);
443     Xid.getSubMatrix(3, 0, Xidtr);
444     Xid.getSubMatrix(3, 3, Xidrr);
445    
446     std::cout << Xidtt << std::endl;
447 xsun 1208 std::cout << "rotation-translation (kcal*fs*mol^-1*Ang^-1):" << std::endl;
448 tim 906 std::cout << Xidrt << std::endl;
449 xsun 1208 std::cout << "translation-rotation(kcal*fs*mol^-1*Ang^-1):" << std::endl;
450 tim 906 std::cout << Xidtr << std::endl;
451 xsun 1208 std::cout << "rotation(kcal*fs*mol^-1):" << std::endl;
452 tim 906 std::cout << Xidrr << std::endl;
453    
454     return true;
455 gezelter 972
456     }
457 tim 906
458 gezelter 972 void ApproximationModel::writeBeads(std::ostream& os) {
459 tim 906 std::vector<BeadParam>::iterator iter;
460     os << beads_.size() << std::endl;
461     os << "Generated by Hydro" << std::endl;
462     for (iter = beads_.begin(); iter != beads_.end(); ++iter) {
463 gezelter 972 os << iter->atomName << "\t" << iter->pos[0] << "\t" << iter->pos[1] << "\t" << iter->pos[2] << std::endl;
464 tim 906 }
465 gezelter 972
466     }
467 tim 906 }

Properties

Name Value
svn:keywords Author Id Revision Date