Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/molecule_geometry.cpp

    r9291d04 r833b15  
    5858/************************************* Functions for class molecule *********************************/
    5959
     60/** Returns vector pointing to center of the domain.
     61 * \return pointer to center of the domain
     62 */
     63#ifdef HAVE_INLINE
     64inline
     65#else
     66static
     67#endif
     68const Vector DetermineCenterOfBox()
     69{
     70  Vector a(0.5,0.5,0.5);
     71  const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
     72  a *= M;
     73  return a;
     74}
    6075
    6176/** Centers the molecule in the box whose lengths are defined by vector \a *BoxLengths.
     
    6580{
    6681  bool status = true;
    67   const Vector *Center = DetermineCenterOfAll();
    68   const Vector *CenterBox = DetermineCenterOfBox();
     82  const Vector Center = DetermineCenterOfAll();
     83  const Vector CenterBox = DetermineCenterOfBox();
    6984  Box &domain = World::getInstance().getDomain();
    7085
    7186  // go through all atoms
    72   for (iterator iter = begin(); iter != end(); ++iter) {
    73     if (DoLog(4) && (*Center != *CenterBox))
    74       LOG(4, "INFO: atom before is at " << **iter);
    75     **iter -= *Center;
    76     **iter += *CenterBox;
    77     if (DoLog(4) && (*Center != *CenterBox))
    78       LOG(4, "INFO: atom after is at " << **iter);
    79   }
     87  Translate(CenterBox - Center);
    8088  getAtomSet().transformNodes(boost::bind(&Box::enforceBoundaryConditions,domain,_1));
    8189
    82   delete(Center);
    83   delete(CenterBox);
    8490  return status;
    85 };
     91}
    8692
    8793
     
    98104
    99105  return status;
    100 };
     106}
    101107
    102108/** Centers the edge of the atoms at (0,0,0).
    103  * \param *out output stream for debugging
    104  * \param *max coordinates of other edge, specifying box dimensions.
    105  */
    106 void molecule::CenterEdge(Vector *max)
    107 {
    108 //  Info info(__func__);
    109   Vector *min = new Vector;
    110 
    111   const_iterator iter = begin();  // start at first in list
    112   if (iter != end()) { //list not empty?
    113     for (int i=NDIM;i--;) {
    114       max->at(i) = (*iter)->at(i);
    115       min->at(i) = (*iter)->at(i);
    116     }
    117     for (; iter != end(); ++iter) {// continue with second if present
    118       //(*iter)->Output(1,1,out);
    119       for (int i=NDIM;i--;) {
    120         max->at(i) = (max->at(i) < (*iter)->at(i)) ? (*iter)->at(i) : max->at(i);
    121         min->at(i) = (min->at(i) > (*iter)->at(i)) ? (*iter)->at(i) : min->at(i);
    122       }
    123     }
    124     LOG(1, "INFO: Maximum is " << *max << ", Minimum is " << *min << ".");
    125     min->Scale(-1.);
    126     (*max) += (*min);
    127     Translate(min);
    128   }
    129   delete(min);
    130 };
     109 */
     110void molecule::CenterEdge()
     111{
     112  const_iterator iter = begin();
     113  if (iter != end()) {   //list not empty?
     114    Vector min = (*begin())->getPosition();
     115    for (;iter != end(); ++iter) { // continue with second if present
     116      const Vector &currentPos = (*iter)->getPosition();
     117      for (size_t i=0;i<NDIM;++i)
     118        if (min[i] > currentPos[i])
     119          min[i] = currentPos[i];
     120    }
     121    Translate(-1.*min);
     122  }
     123}
    131124
    132125/** Centers the center of the atoms at (0,0,0).
     
    147140    }
    148141    Center.Scale(-1./(double)Num); // divide through total number (and sign for direction)
    149     Translate(&Center);
    150   }
    151 };
     142    Translate(Center);
     143  }
     144}
    152145
    153146/** Returns vector pointing to center of all atoms.
    154147 * \return pointer to center of all vector
    155148 */
    156 Vector * molecule::DetermineCenterOfAll() const
     149const Vector molecule::DetermineCenterOfAll() const
    157150{
    158151  const_iterator iter = begin();  // start at first in list
    159   Vector *a = new Vector();
     152  Vector a;
    160153  double Num = 0;
    161154
    162   a->Zero();
     155  a.Zero();
    163156
    164157  if (iter != end()) {   //list not empty?
    165158    for (; iter != end(); ++iter) {  // continue with second if present
    166159      Num++;
    167       (*a) += (*iter)->getPosition();
    168     }
    169     a->Scale(1./(double)Num); // divide through total mass (and sign for direction)
     160      a += (*iter)->getPosition();
     161    }
     162    a.Scale(1./(double)Num); // divide through total mass (and sign for direction)
    170163  }
    171164  return a;
    172 };
    173 
    174 /** Returns vector pointing to center of the domain.
    175  * \return pointer to center of the domain
    176  */
    177 Vector * molecule::DetermineCenterOfBox() const
    178 {
    179   Vector *a = new Vector(0.5,0.5,0.5);
    180   const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
    181   (*a) *= M;
    182   return a;
    183 };
     165}
     166
    184167
    185168/** Returns vector pointing to center of gravity.
     
    187170 * \return pointer to center of gravity vector
    188171 */
    189 Vector * molecule::DetermineCenterOfGravity() const
     172const Vector molecule::DetermineCenterOfGravity() const
    190173{
    191174  const_iterator iter = begin();  // start at first in list
    192   Vector *a = new Vector();
     175  Vector a;
    193176  Vector tmp;
    194177  double Num = 0;
    195178
    196   a->Zero();
     179  a.Zero();
    197180
    198181  if (iter != end()) {   //list not empty?
     
    200183      Num += (*iter)->getType()->getMass();
    201184      tmp = (*iter)->getType()->getMass() * (*iter)->getPosition();
    202       (*a) += tmp;
    203     }
    204     a->Scale(1./Num); // divide through total mass
    205   }
    206   LOG(1, "INFO: Resulting center of gravity: " << *a << ".");
     185      a += tmp;
     186    }
     187    a.Scale(1./Num); // divide through total mass
     188  }
     189  LOG(1, "INFO: Resulting center of gravity: " << a << ".");
    207190  return a;
    208 };
     191}
    209192
    210193/** Centers the center of gravity of the atoms at (0,0,0).
     
    216199  Vector NewCenter;
    217200  DeterminePeriodicCenter(NewCenter);
    218   // go through all atoms
    219   for (iterator iter = begin(); iter != end(); ++iter) {
    220     **iter -= NewCenter;
    221   }
    222 };
     201  Translate(-1.*NewCenter);
     202}
    223203
    224204
     
    227207 * \param *center return vector for translation vector
    228208 */
    229 void molecule::CenterAtVector(Vector *newcenter)
    230 {
    231   // go through all atoms
    232   for (iterator iter = begin(); iter != end(); ++iter) {
    233     **iter -= *newcenter;
    234   }
    235 };
     209void molecule::CenterAtVector(const Vector &newcenter)
     210{
     211  Translate(-1.*newcenter);
     212}
    236213
    237214/** Calculate the inertia tensor of a the molecule.
     
    242219{
    243220  RealSpaceMatrix InertiaTensor;
    244   Vector *CenterOfGravity = DetermineCenterOfGravity();
     221  const Vector CenterOfGravity = DetermineCenterOfGravity();
    245222
    246223  // reset inertia tensor
     
    250227  for (const_iterator iter = begin(); iter != end(); ++iter) {
    251228    Vector x = (*iter)->getPosition();
    252     x -= *CenterOfGravity;
     229    x -= CenterOfGravity;
    253230    const double mass = (*iter)->getType()->getMass();
    254231    InertiaTensor.at(0,0) += mass*(x[1]*x[1] + x[2]*x[2]);
     
    265242  LOG(1, "INFO: The inertia tensor of molecule " << getName() <<  " is:" << InertiaTensor);
    266243
    267   delete CenterOfGravity;
    268244  return InertiaTensor;
    269245}
     
    276252void molecule::RotateToPrincipalAxisSystem(const Vector &Axis)
    277253{
    278   Vector *CenterOfGravity = DetermineCenterOfGravity();
     254  const Vector CenterOfGravity = DetermineCenterOfGravity();
    279255  RealSpaceMatrix InertiaTensor = getInertiaTensor();
    280256
     
    288264
    289265  // obtain first column, eigenvector to biggest eigenvalue
    290   Vector BiggestEigenvector(InertiaTensor.column(Eigenvalues.SmallestComponent()));
     266  const Vector BiggestEigenvector(InertiaTensor.column(Eigenvalues.SmallestComponent()));
    291267  Vector DesiredAxis(Axis.getNormalized());
    292268
     
    302278  // and rotate
    303279  for (iterator iter = begin(); iter != end(); ++iter) {
    304     *(*iter) -= *CenterOfGravity;
     280    *(*iter) -= CenterOfGravity;
    305281    (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), alpha));
    306     *(*iter) += *CenterOfGravity;
     282    *(*iter) += CenterOfGravity;
    307283  }
    308284  LOG(0, "STATUS: done.");
    309 
    310   delete CenterOfGravity;
    311285}
    312286
     
    319293 * x=(**factor) * x (as suggested by comment)
    320294 */
    321 void molecule::Scale(const double ** const factor)
     295void molecule::Scale(const double *factor)
    322296{
    323297  for (iterator iter = begin(); iter != end(); ++iter) {
    324298    for (size_t j=0;j<(*iter)->getTrajectorySize();j++) {
    325299      Vector temp = (*iter)->getPositionAtStep(j);
    326       temp.ScaleAll(*factor);
     300      temp.ScaleAll(factor);
    327301      (*iter)->setPositionAtStep(j,temp);
    328302    }
     
    333307 * \param trans[] translation vector.
    334308 */
    335 void molecule::Translate(const Vector *trans)
    336 {
    337   for (iterator iter = begin(); iter != end(); ++iter) {
    338     for (size_t j=0;j<(*iter)->getTrajectorySize();j++) {
    339       (*iter)->setPositionAtStep(j, (*iter)->getPositionAtStep(j) + (*trans));
    340     }
    341   }
     309void molecule::Translate(const Vector &trans)
     310{
     311  getAtomSet().translate(trans);
    342312};
    343313
     
    346316 * TODO treatment of trajectories missing
    347317 */
    348 void molecule::TranslatePeriodically(const Vector *trans)
    349 {
     318void molecule::TranslatePeriodically(const Vector &trans)
     319{
     320  Translate(trans);
    350321  Box &domain = World::getInstance().getDomain();
    351 
    352   // go through all atoms
    353   for (iterator iter = begin(); iter != end(); ++iter) {
    354     **iter += *trans;
    355   }
    356322  getAtomSet().transformNodes(boost::bind(&Box::enforceBoundaryConditions,domain,_1));
    357 
    358323};
    359324
     
    362327 * \param n[] normal vector of mirror plane.
    363328 */
    364 void molecule::Mirror(const Vector *n)
    365 {
    366   OBSERVE;
    367   Plane p(*n,0);
     329void molecule::Mirror(const Vector &n)
     330{
     331  Plane p(n,0);
    368332  getAtomSet().transformNodes(boost::bind(&Plane::mirrorVector,p,_1));
    369333};
     
    381345  Vector Testvector, Translationvector;
    382346  Vector Center;
    383   BondGraph *BG = World::getInstance().getBondGraph();
     347  const BondGraph * const BG = World::getInstance().getBondGraph();
    384348
    385349  do {
     
    432396
    433397  Center.Scale(1./static_cast<double>(getAtomCount()));
    434   CenterAtVector(&Center);
     398  CenterAtVector(Center);
    435399};
    436400
     
    438402 * \param n[] alignment vector.
    439403 */
    440 void molecule::Align(Vector *n)
     404void molecule::Align(const Vector &n)
    441405{
    442406  double alpha, tmp;
    443407  Vector z_axis;
     408  Vector alignment(n);
    444409  z_axis[0] = 0.;
    445410  z_axis[1] = 0.;
     
    448413  // rotate on z-x plane
    449414  LOG(0, "Begin of Aligning all atoms.");
    450   alpha = atan(-n->at(0)/n->at(2));
     415  alpha = atan(-alignment.at(0)/alignment.at(2));
    451416  LOG(1, "INFO: Z-X-angle: " << alpha << " ... ");
    452417  for (iterator iter = begin(); iter != end(); ++iter) {
     
    462427  }
    463428  // rotate n vector
    464   tmp = n->at(0);
    465   n->at(0) =  cos(alpha) * tmp +  sin(alpha) * n->at(2);
    466   n->at(2) = -sin(alpha) * tmp +  cos(alpha) * n->at(2);
    467   LOG(1, "alignment vector after first rotation: " << n);
     429  tmp = alignment.at(0);
     430  alignment.at(0) =  cos(alpha) * tmp +  sin(alpha) * alignment.at(2);
     431  alignment.at(2) = -sin(alpha) * tmp +  cos(alpha) * alignment.at(2);
     432  LOG(1, "alignment vector after first rotation: " << alignment);
    468433
    469434  // rotate on z-y plane
    470   alpha = atan(-n->at(1)/n->at(2));
     435  alpha = atan(-alignment.at(1)/alignment.at(2));
    471436  LOG(1, "INFO: Z-Y-angle: " << alpha << " ... ");
    472437  for (iterator iter = begin(); iter != end(); ++iter) {
     
    482447  }
    483448  // rotate n vector (for consistency check)
    484   tmp = n->at(1);
    485   n->at(1) =  cos(alpha) * tmp +  sin(alpha) * n->at(2);
    486   n->at(2) = -sin(alpha) * tmp +  cos(alpha) * n->at(2);
    487 
    488 
    489   LOG(1, "alignment vector after second rotation: " << n);
     449  tmp = alignment.at(1);
     450  alignment.at(1) =  cos(alpha) * tmp +  sin(alpha) * alignment.at(2);
     451  alignment.at(2) = -sin(alpha) * tmp +  cos(alpha) * alignment.at(2);
     452
     453  LOG(1, "alignment vector after second rotation: " << alignment);
    490454  LOG(0, "End of Aligning all atoms.");
    491455};
Note: See TracChangeset for help on using the changeset viewer.