ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/brains/Thermo.cpp
Revision: 1820
Committed: Thu Dec 2 00:09:35 2004 UTC (19 years, 7 months ago) by tim
File size: 5345 byte(s)
Log Message:
NVE get built

File Contents

# User Rev Content
1 tim 1725 #include <math.h>
2     #include <iostream>
3    
4     using namespace std;
5    
6     #ifdef IS_MPI
7     #include <mpi.h>
8     #endif //is_mpi
9    
10     #include "brains/Thermo.hpp"
11 tim 1804 #include "primitives/Molecule.hpp"
12 tim 1725 #include "utils/simError.h"
13 tim 1804 #include "utils/OOPSEConstant.hpp"
14 tim 1725
15     namespace oopse {
16    
17     double Thermo::getKinetic() {
18 tim 1804 SimInfo::MoleculeIterator miter;
19     std::vector<StuntDouble*>::iterator iiter;
20     Molecule* mol;
21     StuntDouble* integrableObject;
22     double mass;
23     Vector3d vel;
24     Vector3d angMom;
25     Mat3x3d I;
26     int i;
27     int j;
28     int k;
29     double kinetic = 0.0;
30     double kinetic_global = 0.0;
31    
32     for (mol = info_->beginMolecule(miter); mol != NULL; mol = info_->nextMolecule(miter)) {
33     for (integrableObject = mol->beginIntegrableObject(iiter); integrableObject != NULL;
34     integrableObject = mol->nextIntegrableObject(iiter)) {
35 tim 1725
36 tim 1804 double mass = integrableObject->getMass();
37     Vector3d vel = integrableObject->getVel();
38 tim 1725
39 tim 1804 kinetic = mass * (vel[0]*vel[0] + vel[1]*vel[1] + vel[2]*vel[2]);
40 tim 1725
41 tim 1804 if (integrableObject->isDirectional()) {
42     angMom = integrableObject->getJ();
43     I = integrableObject->getI();
44 tim 1725
45 tim 1804 if (integrableObject->isLinear()) {
46     i = integrableObject->linearAxis();
47     j = (i + 1) % 3;
48     k = (i + 2) % 3;
49     kinetic += angMom[j] * angMom[j] / I(j, j) + angMom[k] * angMom[k] / I(k, k);
50     } else {
51     kinetic += angMom[0]*angMom[0]/I(0, 0) + angMom[1]*angMom[1]/I(1, 1)
52     + angMom[2]*angMom[2]/I(2, 2);
53     }
54 tim 1725 }
55 tim 1804
56 tim 1725 }
57     }
58 tim 1804
59 tim 1725 #ifdef IS_MPI
60    
61     MPI_Allreduce(&kinetic, &kinetic_global, 1, MPI_DOUBLE, MPI_SUM,
62     MPI_COMM_WORLD);
63     kinetic = kinetic_global;
64    
65     #endif //is_mpi
66    
67 tim 1804 kinetic = kinetic * 0.5 / OOPSEConstant::energyConvert;
68 tim 1725
69     return kinetic;
70     }
71    
72     double Thermo::getPotential() {
73 tim 1804 double potential = 0.0;
74     Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
75     double potential_local = curSnapshot->statData[Stats::LONG_RANGE_POTENTIAL];
76 tim 1725
77 tim 1804 SimInfo::MoleculeIterator i;
78     Molecule* mol;
79     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
80     potential_local += mol->getPotential();
81 tim 1725 }
82    
83     // Get total potential for entire system from MPI.
84    
85     #ifdef IS_MPI
86    
87     MPI_Allreduce(&potential_local, &potential, 1, MPI_DOUBLE, MPI_SUM,
88     MPI_COMM_WORLD);
89    
90     #else
91    
92     potential = potential_local;
93    
94     #endif // is_mpi
95    
96     return potential;
97     }
98    
99     double Thermo::getTotalE() {
100     double total;
101    
102     total = this->getKinetic() + this->getPotential();
103     return total;
104     }
105    
106     double Thermo::getTemperature() {
107 tim 1804
108     double temperature = ( 2.0 * this->getKinetic() ) / (info_->getNdf()* OOPSEConstant::kb );
109 tim 1725 return temperature;
110     }
111    
112 tim 1804 double Thermo::getVolume() {
113     Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
114     return curSnapshot->getVolume();
115     }
116 tim 1725
117     double Thermo::getPressure() {
118    
119     // Relies on the calculation of the full molecular pressure tensor
120    
121    
122 tim 1804 Mat3x3d tensor;
123     double pressure;
124 tim 1725
125 tim 1804 this->getPressureTensor(tensor);
126 tim 1725
127 tim 1804 pressure = OOPSEConstant::pressureConvert * (tensor(0, 0) + tensor(1, 1) + tensor(2, 2)) / 3.0;
128    
129 tim 1725 return pressure;
130     }
131    
132 tim 1804 void Thermo::getPressureTensor(Mat3x3d pressureTensor) {
133 tim 1725 // returns pressure tensor in units amu*fs^-2*Ang^-1
134     // routine derived via viral theorem description in:
135     // Paci, E. and Marchi, M. J.Phys.Chem. 1996, 100, 4314-4322
136    
137 tim 1804 Mat3x3d p_local(0.0);
138     Mat3x3d p_global(0.0);
139 tim 1725
140 tim 1804 SimInfo::MoleculeIterator i;
141     std::vector<StuntDouble*>::iterator j;
142     Molecule* mol;
143     StuntDouble* integrableObject;
144     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
145     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
146     integrableObject = mol->nextIntegrableObject(j)) {
147 tim 1725
148 tim 1804 double mass = integrableObject->getMass();
149     Vector3d vcom = integrableObject->getVel();
150     p_local += mass * outProduct(vcom, vcom);
151     }
152 tim 1725 }
153 tim 1804
154 tim 1725 #ifdef IS_MPI
155 tim 1804 MPI_Allreduce(p_local.getArrayPointer(), p_global.getArrayPointer(), 9, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
156 tim 1725 #else
157 tim 1804 p_global = p_local;
158 tim 1725 #endif // is_mpi
159    
160 tim 1804 double volume = this->getVolume();
161     Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
162     Mat3x3d tau = curSnapshot->statData.getTau();
163 tim 1725
164 tim 1804 pressureTensor = p_global + OOPSEConstant::energyConvert/volume * tau;
165     }
166 tim 1725
167 tim 1820 void Thermo::saveStat(){
168     Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
169     Stats& stat = currSnapshot->statData;
170    
171     stat[Stats::KINETIC_ENERGY] = getKinetic();
172     stat[Stats::POTENTIAL_ENERGY] = getPotential();
173     stat[Stats::TOTAL_ENERGY] = stat[Stats::KINETIC_ENERGY] + stat[Stats::POTENTIAL_ENERGY] ;
174     stat[Stats::TEMPERATURE] = getTemperature();
175     stat[Stats::PRESSURE] = getPressure();
176     stat[Stats::VOLUME] = currSnapshot->getVolume();
177    
178     /**@todo need refactorying*/
179     //Conserved Quantity is set by integrator and time is set by setTime
180    
181     }
182    
183 tim 1725 } //end namespace oopse