Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/molecule.cpp

    read4e6 ra67d19  
    66
    77#include <cstring>
    8 #include <boost/bind.hpp>
    9 
    10 #include "World.hpp"
     8
    119#include "atom.hpp"
    1210#include "bond.hpp"
     
    2523#include "tesselation.hpp"
    2624#include "vector.hpp"
     25#include "World.hpp"
    2726
    2827/************************************* Functions for class molecule *********************************/
     
    3130 * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
    3231 */
    33 molecule::molecule(const periodentafel * const teil) : elemente(teil), start(World::getInstance().createAtom()), end(World::getInstance().createAtom()),
     32molecule::molecule(const periodentafel * const teil) : elemente(teil), start(new atom), end(new atom),
    3433  first(new bond(start, end, 1, -1)), last(new bond(start, end, 1, -1)), MDSteps(0), AtomCount(0),
    3534  BondCount(0), ElementCount(0), NoNonHydrogen(0), NoNonBonds(0), NoCyclicBonds(0), BondDistance(0.),
    36   ActiveFlag(false), IndexNr(-1),
    37   formula(this,boost::bind(&molecule::calcFormula,this)),
    38   last_atom(0),
    39   InternalPointer(start)
     35  ActiveFlag(false), IndexNr(-1), last_atom(0), InternalPointer(start)
    4036{
    4137  // init atom chain list
     
    5046  for(int i=MAX_ELEMENTS;i--;)
    5147    ElementsInMolecule[i] = 0;
    52   cell_size[0] = cell_size[2] = cell_size[5]= 20.;
    53   cell_size[1] = cell_size[3] = cell_size[4]= 0.;
    54   strcpy(name,"none");
    55 };
    56 
    57 molecule *NewMolecule(){
    58   return new molecule(World::getInstance().getPeriode());
    59 }
     48  strcpy(name,World::get()->DefaultName);
     49};
    6050
    6151/** Destructor of class molecule.
     
    6757  delete(first);
    6858  delete(last);
    69   end->getWorld()->destroyAtom(end);
    70   start->getWorld()->destroyAtom(start);
    71 };
    72 
    73 
    74 void DeleteMolecule(molecule *mol){
    75   delete mol;
    76 }
    77 
    78 // getter and setter
    79 const std::string molecule::getName(){
    80   return std::string(name);
    81 }
    82 
    83 void molecule::setName(const std::string _name){
    84   OBSERVE;
    85   strncpy(name,_name.c_str(),MAXSTRINGSIZE);
    86 }
    87 
    88 moleculeId_t molecule::getId(){
    89   return id;
    90 }
    91 
    92 void molecule::setId(moleculeId_t _id){
    93   id =_id;
    94 }
    95 
    96 const std::string molecule::getFormula(){
    97   return *formula;
    98 }
    99 
    100 std::string molecule::calcFormula(){
    101   std::map<atomicNumber_t,unsigned int> counts;
    102   stringstream sstr;
    103   periodentafel *periode = World::getInstance().getPeriode();
    104   for(atom *Walker = start; Walker != end; Walker = Walker->next) {
    105     counts[Walker->type->getNumber()]++;
    106   }
    107   std::map<atomicNumber_t,unsigned int>::reverse_iterator iter;
    108   for(iter = counts.rbegin(); iter != counts.rend(); ++iter) {
    109     atomicNumber_t Z = (*iter).first;
    110     sstr << periode->FindElement(Z)->symbol << (*iter).second;
    111   }
    112   return sstr.str();
    113 }
     59  delete(end);
     60  delete(start);
     61};
    11462
    11563
     
    12169bool molecule::AddAtom(atom *pointer)
    12270{
    123   bool retval = false;
    124   OBSERVE;
    12571  if (pointer != NULL) {
    12672    pointer->sort = &pointer->nr;
     
    13985      }
    14086    }
    141     retval = add(pointer, end);
    142   }
    143   return retval;
     87    return add(pointer, end);
     88  } else
     89    return false;
    14490};
    14591
     
    15197atom * molecule::AddCopyAtom(atom *pointer)
    15298{
    153   atom *retval = NULL;
    154   OBSERVE;
    15599  if (pointer != NULL) {
    156     atom *walker = pointer->clone();
     100    atom *walker = new atom(pointer);
    157101    walker->Name = Malloc<char>(strlen(pointer->Name) + 1, "atom::atom: *Name");
    158102    strcpy (walker->Name, pointer->Name);
     
    162106      NoNonHydrogen++;
    163107    AtomCount++;
    164     retval=walker;
    165   }
    166   return retval;
     108    return walker;
     109  } else
     110    return NULL;
    167111};
    168112
     
    203147bool molecule::AddHydrogenReplacementAtom(bond *TopBond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem)
    204148{
    205   bool AllWentWell = true;    // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
    206   OBSERVE;
    207149  double bondlength;  // bond length of the bond to be replaced/cut
    208150  double bondangle;  // bond angle of the bond to be replaced/cut
    209151  double BondRescale;   // rescale value for the hydrogen bond length
     152  bool AllWentWell = true;    // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
    210153  bond *FirstBond = NULL, *SecondBond = NULL; // Other bonds in double bond case to determine "other" plane
    211154  atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
     
    215158  double *matrix = NULL;
    216159  bond *Binder = NULL;
     160  double * const cell_size = World::get()->cell_size;
    217161
    218162//  Log() << Verbose(3) << "Begin of AddHydrogenReplacementAtom." << endl;
     
    250194  BondRescale = TopOrigin->type->HBondDistance[TopBond->BondDegree-1];
    251195  if (BondRescale == -1) {
    252     eLog() << Verbose(1) << "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl;
     196    DoeLog(1) && (eLog()<< Verbose(1) << "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl);
    253197    return false;
    254198    BondRescale = bondlength;
     
    261205  switch(TopBond->BondDegree) {
    262206    case 1:
    263       FirstOtherAtom = World::getInstance().createAtom();    // new atom
     207      FirstOtherAtom = new atom();    // new atom
    264208      FirstOtherAtom->type = elemente->FindElement(1);  // element is Hydrogen
    265209      FirstOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
     
    293237            SecondOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
    294238          } else {
    295             eLog() << Verbose(2) << "Detected more than four bonds for atom " << TopOrigin->Name;
     239            DoeLog(2) && (eLog()<< Verbose(2) << "Detected more than four bonds for atom " << TopOrigin->Name);
    296240          }
    297241        }
     
    318262
    319263      // create the two Hydrogens ...
    320       FirstOtherAtom = World::getInstance().createAtom();
    321       SecondOtherAtom = World::getInstance().createAtom();
     264      FirstOtherAtom = new atom();
     265      SecondOtherAtom = new atom();
    322266      FirstOtherAtom->type = elemente->FindElement(1);
    323267      SecondOtherAtom->type = elemente->FindElement(1);
     
    330274      bondangle = TopOrigin->type->HBondAngle[1];
    331275      if (bondangle == -1) {
    332         eLog() << Verbose(1) << "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl;
     276        DoeLog(1) && (eLog()<< Verbose(1) << "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl);
    333277        return false;
    334278        bondangle = 0;
     
    373317    case 3:
    374318      // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
    375       FirstOtherAtom = World::getInstance().createAtom();
    376       SecondOtherAtom = World::getInstance().createAtom();
    377       ThirdOtherAtom = World::getInstance().createAtom();
     319      FirstOtherAtom = new atom();
     320      SecondOtherAtom = new atom();
     321      ThirdOtherAtom = new atom();
    378322      FirstOtherAtom->type = elemente->FindElement(1);
    379323      SecondOtherAtom->type = elemente->FindElement(1);
     
    452396      break;
    453397    default:
    454       eLog() << Verbose(1) << "BondDegree does not state single, double or triple bond!" << endl;
     398      DoeLog(1) && (eLog()<< Verbose(1) << "BondDegree does not state single, double or triple bond!" << endl);
    455399      AllWentWell = false;
    456400      break;
     
    469413bool molecule::AddXYZFile(string filename)
    470414{
    471 
    472415  istringstream *input = NULL;
    473416  int NumberOfAtoms = 0; // atom number in xyz read
     
    483426    return false;
    484427
    485   OBSERVE;
    486428  getline(xyzfile,line,'\n'); // Read numer of atoms in file
    487429  input = new istringstream(line);
    488430  *input >> NumberOfAtoms;
    489   Log() << Verbose(0) << "Parsing " << NumberOfAtoms << " atoms in file." << endl;
     431  DoLog(0) && (Log() << Verbose(0) << "Parsing " << NumberOfAtoms << " atoms in file." << endl);
    490432  getline(xyzfile,line,'\n'); // Read comment
    491   Log() << Verbose(1) << "Comment: " << line << endl;
     433  DoLog(1) && (Log() << Verbose(1) << "Comment: " << line << endl);
    492434
    493435  if (MDSteps == 0) // no atoms yet present
    494436    MDSteps++;
    495437  for(i=0;i<NumberOfAtoms;i++){
    496     Walker = World::getInstance().createAtom();
     438    Walker = new atom;
    497439    getline(xyzfile,line,'\n');
    498440    istringstream *item = new istringstream(line);
     
    505447    Walker->type = elemente->FindElement(shorthand);
    506448    if (Walker->type == NULL) {
    507       eLog() << Verbose(1) << "Could not parse the element at line: '" << line << "', setting to H.";
     449      DoeLog(1) && (eLog()<< Verbose(1) << "Could not parse the element at line: '" << line << "', setting to H.");
    508450      Walker->type = elemente->FindElement(1);
    509451    }
     
    601543    add(Binder, last);
    602544  } else {
    603     eLog() << Verbose(1) << "Could not add bond between " << atom1->Name << " and " << atom2->Name << " as one or both are not present in the molecule." << endl;
     545    DoeLog(1) && (eLog()<< Verbose(1) << "Could not add bond between " << atom1->Name << " and " << atom2->Name << " as one or both are not present in the molecule." << endl);
    604546  }
    605547  return Binder;
     
    613555bool molecule::RemoveBond(bond *pointer)
    614556{
    615   //eLog() << Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl;
     557  //DoeLog(1) && (eLog()<< Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl);
    616558  pointer->leftatom->RegisterBond(pointer);
    617559  pointer->rightatom->RegisterBond(pointer);
     
    627569bool molecule::RemoveBonds(atom *BondPartner)
    628570{
    629   //eLog() << Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl;
     571  //DoeLog(1) && (eLog()<< Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl);
    630572  BondList::const_iterator ForeRunner;
    631573  while (!BondPartner->ListOfBonds.empty()) {
     
    661603void molecule::SetBoxDimension(Vector *dim)
    662604{
     605  double * const cell_size = World::get()->cell_size;
    663606  cell_size[0] = dim->x[0];
    664607  cell_size[1] = 0.;
     
    679622    AtomCount--;
    680623  } else
    681     eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;
     624    DoeLog(1) && (eLog()<< Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl);
    682625  if (ElementsInMolecule[pointer->type->Z] == 0)  // was last atom of this element?
    683626    ElementCount--;
     
    697640    ElementsInMolecule[pointer->type->Z]--; // decrease number of atom of this element
    698641  else
    699     eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;
     642    DoeLog(1) && (eLog()<< Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl);
    700643  if (ElementsInMolecule[pointer->type->Z] == 0)  // was last atom of this element?
    701644    ElementCount--;
     
    722665    return walker;
    723666  } else {
    724     Log() << Verbose(0) << "Atom not found in list." << endl;
     667    DoLog(0) && (Log() << Verbose(0) << "Atom not found in list." << endl);
    725668    return NULL;
    726669  }
     
    738681    //mol->Output((ofstream *)&cout);
    739682    //Log() << Verbose(0) << "===============================================" << endl;
    740     Log() << Verbose(0) << text;
     683    DoLog(0) && (Log() << Verbose(0) << text);
    741684    cin >> No;
    742685    ion = this->FindAtom(No);
     
    751694bool molecule::CheckBounds(const Vector *x) const
    752695{
     696  double * const cell_size = World::get()->cell_size;
    753697  bool result = true;
    754698  int j =-1;
     
    826770void molecule::OutputListOfBonds() const
    827771{
    828   Log() << Verbose(2) << endl << "From Contents of ListOfBonds, all non-hydrogen atoms:" << endl;
     772  DoLog(2) && (Log() << Verbose(2) << endl << "From Contents of ListOfBonds, all non-hydrogen atoms:" << endl);
    829773  ActOnAllAtoms (&atom::OutputBondOfAtom );
    830   Log() << Verbose(0) << endl;
     774  DoLog(0) && (Log() << Verbose(0) << endl);
    831775};
    832776
     
    885829  }
    886830  if ((AtomCount == 0) || (i != AtomCount)) {
    887     Log() << Verbose(3) << "Mismatch in AtomCount " << AtomCount << " and recounted number " << i << ", renaming all." << endl;
     831    DoLog(3) && (Log() << Verbose(3) << "Mismatch in AtomCount " << AtomCount << " and recounted number " << i << ", renaming all." << endl);
    888832    AtomCount = i;
    889833
     
    901845        Walker->Name = Malloc<char>(6, "molecule::CountAtoms: *walker->Name");
    902846        sprintf(Walker->Name, "%2s%02d", Walker->type->symbol, Walker->nr+1);
    903         Log() << Verbose(3) << "Naming atom nr. " << Walker->nr << " " << Walker->Name << "." << endl;
     847        DoLog(3) && (Log() << Verbose(3) << "Naming atom nr. " << Walker->nr << " " << Walker->Name << "." << endl);
    904848        i++;
    905849      }
    906850    } else
    907       Log() << Verbose(3) << "AtomCount is still " << AtomCount << ", thus counting nothing." << endl;
     851      DoLog(3) && (Log() << Verbose(3) << "AtomCount is still " << AtomCount << ", thus counting nothing." << endl);
    908852  }
    909853};
     
    965909  bool result = true; // status of comparison
    966910
    967   Log() << Verbose(3) << "Begin of IsEqualToWithinThreshold." << endl;
     911  DoLog(3) && (Log() << Verbose(3) << "Begin of IsEqualToWithinThreshold." << endl);
    968912  /// first count both their atoms and elements and update lists thereby ...
    969913  //Log() << Verbose(0) << "Counting atoms, updating list" << endl;
     
    977921  if (result) {
    978922    if (AtomCount != OtherMolecule->AtomCount) {
    979       Log() << Verbose(4) << "AtomCounts don't match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;
     923      DoLog(4) && (Log() << Verbose(4) << "AtomCounts don't match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl);
    980924      result = false;
    981925    } else Log() << Verbose(4) << "AtomCounts match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;
     
    984928  if (result) {
    985929    if (ElementCount != OtherMolecule->ElementCount) {
    986       Log() << Verbose(4) << "ElementCount don't match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl;
     930      DoLog(4) && (Log() << Verbose(4) << "ElementCount don't match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl);
    987931      result = false;
    988932    } else Log() << Verbose(4) << "ElementCount match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl;
     
    996940    }
    997941    if (flag < MAX_ELEMENTS) {
    998       Log() << Verbose(4) << "ElementsInMolecule don't match." << endl;
     942      DoLog(4) && (Log() << Verbose(4) << "ElementsInMolecule don't match." << endl);
    999943      result = false;
    1000944    } else Log() << Verbose(4) << "ElementsInMolecule match." << endl;
     
    1002946  /// then determine and compare center of gravity for each molecule ...
    1003947  if (result) {
    1004     Log() << Verbose(5) << "Calculating Centers of Gravity" << endl;
     948    DoLog(5) && (Log() << Verbose(5) << "Calculating Centers of Gravity" << endl);
    1005949    DeterminePeriodicCenter(CenterOfGravity);
    1006950    OtherMolecule->DeterminePeriodicCenter(OtherCenterOfGravity);
    1007     Log() << Verbose(5) << "Center of Gravity: ";
     951    DoLog(5) && (Log() << Verbose(5) << "Center of Gravity: ");
    1008952    CenterOfGravity.Output();
    1009     Log() << Verbose(0) << endl << Verbose(5) << "Other Center of Gravity: ";
     953    DoLog(0) && (Log() << Verbose(0) << endl << Verbose(5) << "Other Center of Gravity: ");
    1010954    OtherCenterOfGravity.Output();
    1011     Log() << Verbose(0) << endl;
     955    DoLog(0) && (Log() << Verbose(0) << endl);
    1012956    if (CenterOfGravity.DistanceSquared(&OtherCenterOfGravity) > threshold*threshold) {
    1013       Log() << Verbose(4) << "Centers of gravity don't match." << endl;
     957      DoLog(4) && (Log() << Verbose(4) << "Centers of gravity don't match." << endl);
    1014958      result = false;
    1015959    }
     
    1018962  /// ... then make a list with the euclidian distance to this center for each atom of both molecules
    1019963  if (result) {
    1020     Log() << Verbose(5) << "Calculating distances" << endl;
     964    DoLog(5) && (Log() << Verbose(5) << "Calculating distances" << endl);
    1021965    Distances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: Distances");
    1022966    OtherDistances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: OtherDistances");
     
    1025969
    1026970    /// ... sort each list (using heapsort (o(N log N)) from GSL)
    1027     Log() << Verbose(5) << "Sorting distances" << endl;
     971    DoLog(5) && (Log() << Verbose(5) << "Sorting distances" << endl);
    1028972    PermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermMap");
    1029973    OtherPermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *OtherPermMap");
     
    1031975    gsl_heapsort_index (OtherPermMap, OtherDistances, AtomCount, sizeof(double), CompareDoubles);
    1032976    PermutationMap = Calloc<int>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermutationMap");
    1033     Log() << Verbose(5) << "Combining Permutation Maps" << endl;
     977    DoLog(5) && (Log() << Verbose(5) << "Combining Permutation Maps" << endl);
    1034978    for(int i=AtomCount;i--;)
    1035979      PermutationMap[PermMap[i]] = (int) OtherPermMap[i];
    1036980
    1037981    /// ... and compare them step by step, whether the difference is individually(!) below \a threshold for all
    1038     Log() << Verbose(4) << "Comparing distances" << endl;
     982    DoLog(4) && (Log() << Verbose(4) << "Comparing distances" << endl);
    1039983    flag = 0;
    1040984    for (int i=0;i<AtomCount;i++) {
    1041       Log() << Verbose(5) << "Distances squared: |" << Distances[PermMap[i]] << " - " << OtherDistances[OtherPermMap[i]] << "| = " << fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) << " ?<? " <<  threshold << endl;
     985      DoLog(5) && (Log() << Verbose(5) << "Distances squared: |" << Distances[PermMap[i]] << " - " << OtherDistances[OtherPermMap[i]] << "| = " << fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) << " ?<? " <<  threshold << endl);
    1042986      if (fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) > threshold*threshold)
    1043987        flag = 1;
     
    1055999  }
    10561000  /// return pointer to map if all distances were below \a threshold
    1057   Log() << Verbose(3) << "End of IsEqualToWithinThreshold." << endl;
     1001  DoLog(3) && (Log() << Verbose(3) << "End of IsEqualToWithinThreshold." << endl);
    10581002  if (result) {
    1059     Log() << Verbose(3) << "Result: Equal." << endl;
     1003    DoLog(3) && (Log() << Verbose(3) << "Result: Equal." << endl);
    10601004    return PermutationMap;
    10611005  } else {
    1062     Log() << Verbose(3) << "Result: Not equal." << endl;
     1006    DoLog(3) && (Log() << Verbose(3) << "Result: Not equal." << endl);
    10631007    return NULL;
    10641008  }
     
    10751019{
    10761020  atom *Walker = NULL, *OtherWalker = NULL;
    1077   Log() << Verbose(3) << "Begin of GetFatherAtomicMap." << endl;
     1021  DoLog(3) && (Log() << Verbose(3) << "Begin of GetFatherAtomicMap." << endl);
    10781022  int *AtomicMap = Malloc<int>(AtomCount, "molecule::GetAtomicMap: *AtomicMap");
    10791023  for (int i=AtomCount;i--;)
     
    10821026    for (int i=AtomCount;i--;) // no need as -1 means already that there is trivial correspondence
    10831027      AtomicMap[i] = i;
    1084     Log() << Verbose(4) << "Map is trivial." << endl;
     1028    DoLog(4) && (Log() << Verbose(4) << "Map is trivial." << endl);
    10851029  } else {
    1086     Log() << Verbose(4) << "Map is ";
     1030    DoLog(4) && (Log() << Verbose(4) << "Map is ");
    10871031    Walker = start;
    10881032    while (Walker->next != end) {
     
    11011045        }
    11021046      }
    1103       Log() << Verbose(0) << AtomicMap[Walker->nr] << "\t";
    1104     }
    1105     Log() << Verbose(0) << endl;
    1106   }
    1107   Log() << Verbose(3) << "End of GetFatherAtomicMap." << endl;
     1047      DoLog(0) && (Log() << Verbose(0) << AtomicMap[Walker->nr] << "\t");
     1048    }
     1049    DoLog(0) && (Log() << Verbose(0) << endl);
     1050  }
     1051  DoLog(3) && (Log() << Verbose(3) << "End of GetFatherAtomicMap." << endl);
    11081052  return AtomicMap;
    11091053};
     
    11411085  }
    11421086};
    1143 
    1144 void molecule::flipActiveFlag(){
    1145   ActiveFlag = !ActiveFlag;
    1146 }
Note: See TracChangeset for help on using the changeset viewer.