ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/visitors/ZconsVisitor.cpp
(Generate patch)

Comparing trunk/src/visitors/ZconsVisitor.cpp (file contents):
Revision 2 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
Revision 2071 by gezelter, Sat Mar 7 21:41:51 2015 UTC

# Line 1 | Line 1
1 < #include "ZconsVisitor.hpp"
1 > /*
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 > * 1. Redistributions of source code must retain the above copyright
10 > *    notice, this list of conditions and the following disclaimer.
11 > *
12 > * 2. Redistributions in binary form must reproduce the above copyright
13 > *    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 > *
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, 234107 (2008).          
39 > * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 > * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 > */
42 >
43   #include <cmath>
44 + #include "visitors/ZconsVisitor.hpp"
45 + #include "primitives/Molecule.hpp"
46 + #include "utils/StringUtils.hpp"
47 + #include "types/ZconsStamp.hpp"
48 + namespace OpenMD {
49  
50 < ZConsVisitor::ZConsVisitor(SimInfo* info) : BaseVisitor(), zconsReader(NULL){
51 <  GenericData* data;
52 <  DoubleData* tolerance;  
53 <  ZConsParaData* zConsParaData;
54 <  StringData* filename;
55 <  DoubleData* sampleTime;
10 <  vector<ZConsParaItem>* parameters;
50 >  ZConsVisitor::ZConsVisitor(SimInfo* info) : BaseVisitor(),
51 >                                              zconsReader_(NULL), info_(info){
52 >    
53 >    visitorName = "ZConsVisitor";
54 >    currSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
55 >    Globals* simParam = info_->getSimParams();
56  
57 <  this->info = info;
58 <  visitorName = "ZConsVisitor";
14 <  
15 <  //retrieve tolerance for z-constraint molecuels
16 <  data = info->getProperty(ZCONSTOL_ID);
17 <
18 <  if (!data){
19 <    cerr << "Can not get zconstraint tolerance from SimInfo" << endl;
20 <    haveZcons = false;  
21 <    return;
22 <  }
23 <  else{
24 <    tolerance = dynamic_cast<DoubleData*>(data);
25 <
26 <    if (!tolerance){
27 <      cerr << "Can not get zconstraint tolerance  from SimInfo" << endl;
28 <      haveZcons = false;    
29 <      return;
57 >    if (simParam->haveZconsTime()){
58 >      zconsTime_ = simParam->getZconsTime();
59      }
60      else{
61 <      zconsTol = tolerance->getData();
61 >      sprintf(painCave.errMsg,
62 >              "ZConstraint error: If you use a ZConstraint,\n"
63 >              "\tyou must set zconsTime.\n");
64 >      painCave.isFatal = 1;
65 >      simError();
66      }
34  }
67  
68 <  //retrieve sample time of z-contraint
69 <  data = info->getProperty(ZCONSTIME_ID);
38 <
39 <  if (!data){
40 <    cerr << "Can not get zcons time  from SimInfo" << endl;
41 <    haveZcons = false;    
42 <    return;
43 <  }
44 <  else{
45 <    sampleTime = dynamic_cast<DoubleData*>(data);
46 <
47 <    if (!sampleTime){
48 <      cerr << "Can not get zcons time  from SimInfo" << endl;
49 <      haveZcons = false;    
50 <      return;
68 >    if (simParam->haveZconsTol()){
69 >      zconsTol_ = simParam->getZconsTol();
70      }
71      else{
72 <     zconsTime = sampleTime->getData();
72 >      zconsTol_ = 0.01;
73 >      sprintf(painCave.errMsg,
74 >              "ZConstraint Warning: Tolerance for z-constraint method is not specified.\n"
75 >              "\tOpenMD will use a default value of %f.\n"
76 >              "\tTo set the tolerance, use the zconsTol variable.\n",
77 >              zconsTol_);
78 >      painCave.isFatal = 0;
79 >      simError();      
80 >    }    
81 >        
82 >    int nZconstraints = simParam->getNZconsStamps();
83 >    std::vector<ZConsStamp*> stamp = simParam->getZconsStamps();
84 >    for (int i = 0; i < nZconstraints; i++){
85 >      int zmolIndex = stamp[i]->getMolIndex();
86 >      zmolStates_.insert(std::make_pair(zmolIndex, zsMoving));
87      }
55  }
88  
57  //retrieve index of z-constraint molecules
58  data = info->getProperty(ZCONSPARADATA_ID);
59  if (!data){
60    cerr << "Can not get index of z-constraint molecules from SimInfo" << endl;
61    haveZcons = false;
62    return;
63  }
64  else{
65    zConsParaData = dynamic_cast<ZConsParaData*>(data);
89  
90 <    if (!zConsParaData){
91 <      cerr << "Can not get index of z-constraint molecules from SimInfo" << endl;
92 <      haveZcons = false;
93 <      return;
90 >    //fill zatomToZmol_ array
91 >    /** @todo only works for single version now*/
92 >    std::map<int, ZConsState>::iterator j;
93 >    for (j = zmolStates_.begin(); j != zmolStates_.end(); ++j) {
94 >      Molecule* mol = info_->getMoleculeByGlobalIndex(j->first);
95 >      assert(mol != NULL);
96 >      Molecule::AtomIterator ai;
97 >      Atom* at;
98 >      for (at = mol->beginAtom(ai); at != NULL; at = mol->nextAtom(ai)) {
99 >        zatomToZmol_.insert(std::make_pair(at->getGlobalIndex(), mol->getGlobalIndex()));
100 >      }
101      }
72    else{
73      vector<ZConsParaItem>::iterator i;
74      Molecule* mol;
102  
103 <      parameters = zConsParaData->getData();
104 <      for(i = parameters->begin(); i != parameters->end(); ++i){
103 >    zconsFilename_ = getPrefix(info_->getFinalConfigFileName()) + ".fz";
104 >    
105 >    zconsReader_ = new ZConsReader(info);
106  
107 <        mol = findZconsMol(i->zconsIndex);
108 <        if(mol != NULL)
109 <          zconsMol.push_back(mol);
107 >    if (zconsReader_->hasNextFrame())
108 >      zconsReader_->readNextFrame();
109 >  
110 >  }
111  
112 <      }
113 <
114 <      if(zconsMol.size() < 1){
86 <        cerr << "number of zconstraint molecules is less than one" << endl;
87 <        haveZcons = false;
88 <        return;
89 <      }
90 <      
91 <    }//end if (!zConsParaData)
92 <
93 <  }//end  if (!data  
112 >  ZConsVisitor::~ZConsVisitor(){
113 >    if(!zconsReader_)
114 >      delete zconsReader_;
115    
95  //retrieve output filename of z force
96  data = info->getProperty(ZCONSFILENAME_ID);
97  if (!data){
98    cerr << "Can not get filename of z-constraint output from SimInfo" << endl;
99    haveZcons = false;
100    return;
116    }
102  else{
103    filename = dynamic_cast<StringData*>(data);
117  
118 <    if (!filename){
119 <      cerr << "Can not get filename of z-constraint output from SimInfo" << endl;
120 <      haveZcons = false;
121 <      return;
109 <    }
110 <    else{
111 <      zconsFilename = filename->getData();
112 <    }
118 >  void ZConsVisitor::visit(Atom* atom){
119 >    std::string prefix;
120 >    if(isZconstraint(atom->getGlobalIndex(), prefix))
121 >      internalVisit(atom, prefix);
122    }
123  
124 <  zconsReader = new ZConsReader(info);
124 >  void ZConsVisitor::visit(DirectionalAtom* datom){
125 >    std::string prefix;
126  
127 <  if (zconsReader->hasNextFrame())
128 <  zconsReader->readNextFrame();
129 <  
120 <  haveZcons = true;
121 < }
127 >    if(isZconstraint(datom->getGlobalIndex(), prefix))
128 >      internalVisit(datom, prefix);
129 >  }
130  
131 < ZConsVisitor::~ZConsVisitor(){
132 <  if(!zconsReader)
133 <    delete zconsReader;
126 <  
127 < }
131 >  void ZConsVisitor::visit(RigidBody* rb){
132 >    std::string prefix;
133 >    std::vector<Atom*> atoms;
134  
135 < void ZConsVisitor::visit(Atom* atom){
130 <  string prefix;
131 <  if(isZconstraint(atom->getIndex(), prefix))
132 <    internalVisit(atom, prefix);
133 < }
135 >    atoms = rb->getAtoms();
136  
137 < void ZConsVisitor::visit(DirectionalAtom* datom){
138 <  string prefix;
139 <  
138 <  if(isZconstraint(datom->getIndex(), prefix))
139 <    internalVisit(datom, prefix);
140 < }
141 <
142 < void ZConsVisitor::visit(RigidBody* rb){
143 <  string prefix;
144 <  vector<Atom*> atoms;
145 <  
146 <  atoms = rb->getAtoms();
147 <    
148 <  if(isZconstraint(atoms[0]->getIndex(), prefix))
149 <    internalVisit(rb, prefix);
150 < }
137 >    if(isZconstraint(atoms[0]->getGlobalIndex(), prefix))
138 >      internalVisit(rb, prefix);
139 >  }
140  
141 < Molecule* ZConsVisitor::findZconsMol(int index){
142 <  Molecule* mols;
143 <  mols = info->molecules;
144 <  for(int i =0; i < info->n_mol; i++)
145 <    if(index == mols[i].getMyIndex())
146 <      return &mols[i];
141 >  void ZConsVisitor::update(){
142 >    Vector3d com;
143 >    std::map<int, ZConsState>::iterator i;
144 >    for ( i = zmolStates_.begin(); i != zmolStates_.end(); ++i) {
145 >      i->second = zsMoving;
146 >    }
147 >    
148 >    readZconsFile(currSnapshot_->getTime());
149  
150 <  return NULL;
151 < }
150 >    const std::vector<ZconsData>& fixedZmolData = zconsReader_->getFixedZMolData();
151 >    std::vector<ZconsData>::const_iterator j;
152 >    for (j = fixedZmolData.begin(); j != fixedZmolData.end(); ++j) {
153 >      std::map<int, ZConsState>::iterator k = zmolStates_.find(j->zmolIndex);
154 >      assert(k != zmolStates_.end());
155 >      k->second = zsFixed;
156 >    }
157 >    
158 >  }
159  
160 < void ZConsVisitor::update(){
161 <  Molecule* mol;
162 <  double com[3];
163 <  ZConsState state;
164 <  Atom** atoms;
165 <  int natoms;
160 >  void ZConsVisitor::readZconsFile(RealType time) {
161 >    RealType tempTime;
162 >    while(zconsReader_->hasNextFrame()){
163 >      tempTime = zconsReader_->getCurTime();
164 >      if(tempTime >= time) {
165 >        return;
166 >      }
167 >        
168 >      zconsReader_->readNextFrame();
169 >    }
170 >  }
171  
172 <  getZconsPos(info->currentTime);
173 <  
174 <  for(size_t i = 0; i < zconsMol.size(); i++){
175 <    zconsMol[i]->getCOM(com);
172 >  void ZConsVisitor::internalVisit(StuntDouble* sd, const std::string& prefix){
173 >    GenericData* data;
174 >    AtomData* atomData;
175 >    AtomInfo* atomInfo;
176 >    std::vector<AtomInfo*>::iterator iter;
177  
178 <    if(fabs(com[2] - zconsPos[i]) < zconsTol)
179 <      state = zsFixed;
178 >    //if there is not atom data, just skip it
179 >    data = sd->getPropertyByName("ATOMDATA");
180 >    if(data != NULL){
181 >      atomData = dynamic_cast<AtomData*>(data);  
182 >      if(atomData == NULL)
183 >        return;
184 >    }
185      else
186 <      state = zsMoving;
186 >      return;
187  
188 <    //update the state of zconstraint atom
189 <    natoms = zconsMol[i]->getNAtoms();
181 <    atoms = zconsMol[i]->getMyAtoms();    
182 <    for(int j =0; j < natoms; j++)
183 <      zconsState[atoms[j]->getIndex()] = state;
188 >    for(atomInfo  = atomData->beginAtomInfo(iter); atomInfo; atomInfo = atomData->nextAtomInfo(iter))
189 >      (atomInfo->atomTypeName).insert(0, prefix);
190    }
185  
186 }
191  
188 void ZConsVisitor::getZconsPos(double time){
192  
193 <  vector<double> tempPos;
194 <  vector<double> prevPos;
195 <  double tempTime;
193 >  bool ZConsVisitor::isZconstraint(int atomIndex, std::string& prefix){
194 >    std::string prefixString[] = {"ZF", "ZM"};
195 >    std::map<int, int>::iterator i = zatomToZmol_.find(atomIndex);
196 >    if (i ==  zatomToZmol_.end() ){
197 >      prefix = "";
198 >      return false;
199 >    } else {
200  
201 <  while(true){
202 <    tempTime = zconsReader->getCurTime();
203 <
204 <    zconsPos = zconsReader->getZConsPos();
205 <    
199 <    if(tempTime >= time)
200 <      return;
201 <    
202 <    zconsReader->readNextFrame();
203 <
204 <  }
205 <
206 < }
207 <
208 < void ZConsVisitor::internalVisit(StuntDouble* sd, const string& prefix){
209 <  GenericData* data;
210 <  AtomData* atomData;
211 <  AtomInfo* atomInfo;
212 <  vector<AtomInfo*>::iterator iter;
213 <
214 <  
215 <  //if there is not atom data, just skip it
216 <  data = sd->getProperty("ATOMDATA");
217 <  if(data != NULL){
218 <    atomData = dynamic_cast<AtomData*>(data);  
219 <    if(atomData == NULL)
220 <      return;
201 >      std::map<int, ZConsState>::iterator j = zmolStates_.find(i->second);
202 >      assert(j !=zmolStates_.end());
203 >      prefix = prefixString[j->second];
204 >      return true;
205 >    }
206    }
222  else
223    return;
207  
208 <  for(atomInfo  = atomData->beginAtomInfo(iter); atomInfo; atomInfo = atomData->nextAtomInfo(iter))
209 <    (atomInfo->AtomType).insert(0, prefix);
210 < }
208 >  const std::string ZConsVisitor::toString(){
209 >    char buffer[65535];
210 >    std::string result;
211  
212 +    sprintf(buffer ,"------------------------------------------------------------------\n");
213 +    result += buffer;
214  
215 < bool ZConsVisitor::isZconstraint(int index, string& prefix){
216 <  map<int, ZConsState>::iterator i;
232 <  string prefixString[] = {"ZF", "ZM"};
233 <  
234 <  i = zconsState.find(index);
235 <  if(i !=zconsState.end()){
236 <    prefix = prefixString[(*i).second];
237 <    return true;
238 <  }
239 <  else{
240 <    prefix = "";
241 <    return false;
242 <  }
243 < }
215 >    sprintf(buffer ,"Visitor name: %s\n", visitorName.c_str());
216 >    result += buffer;
217  
218 < const string ZConsVisitor::toString(){
219 <  char buffer[65535];
247 <  string result;
248 <  
249 <  sprintf(buffer ,"------------------------------------------------------------------\n");
250 <  result += buffer;
218 >    sprintf(buffer , "number of zconstraint molecule: %d\n", (int) zmolStates_.size());
219 >    result += buffer;
220  
221 <  sprintf(buffer ,"Visitor name: %s\n", visitorName.c_str());
222 <  result += buffer;
254 <  
255 <  sprintf(buffer , "number of zconstraint molecule: %d\n", zconsMol.size());
256 <  result += buffer;
221 >    sprintf(buffer , "zconstraint tolerance = %lf\n", zconsTol_);
222 >    result += buffer;
223  
224 <  sprintf(buffer , "zconstraint tolerance = %lf\n", zconsTol);
225 <  result += buffer;
224 >    sprintf(buffer , "zconstraint sample time = %lf\n", zconsTime_);
225 >    result += buffer;
226  
227 <  sprintf(buffer , "zconstraint sample time = %lf\n", zconsTime);
228 <  result += buffer;
227 >    sprintf(buffer , "zconstraint output filename = %s\n", zconsFilename_.c_str());
228 >    result += buffer;
229  
230 <  sprintf(buffer , "zconstraint output filename = %s\n", zconsFilename.c_str());
231 <  result += buffer;
230 >    std::map<int, ZConsState>::iterator i;
231 >    int j = 0;
232 >    for ( i = zmolStates_.begin(); i != zmolStates_.end(); ++i) {
233 >      sprintf(buffer ,"zconstraint molecule[%d] = %d\n", j++, i->first);
234 >      result += buffer;
235 >    }
236    
237 <  for(size_t i = 0; i < zconsMol.size(); ++i){
268 <    sprintf(buffer ,"zconstraint molecule[%d] = %d\n", i, zconsMol[i]->getMyIndex());
237 >    sprintf(buffer ,"------------------------------------------------------------------\n");
238      result += buffer;
239  
240 +    return result;
241    }
242  
273  
274  sprintf(buffer ,"------------------------------------------------------------------\n");
275  result += buffer;
243  
244 <  return result;
278 < }
244 > }//namespace OpenMD

Comparing trunk/src/visitors/ZconsVisitor.cpp (property svn:keywords):
Revision 2 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
Revision 2071 by gezelter, Sat Mar 7 21:41:51 2015 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines