Changeset ea7176 for src/molecule.cpp


Ignore:
Timestamp:
Mar 19, 2010, 1:29:01 PM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
e0b6fd
Parents:
80c63d
Message:

FIX: Made AtomCount a Cacheable so that the number of atoms in a molecule will always be correct

All unittests working.
All Complete testcases fail.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/molecule.cpp

    r80c63d rea7176  
    3232 */
    3333molecule::molecule(const periodentafel * const teil) : elemente(teil),
    34   first(new bond(0, 0, 1, -1)), last(new bond(0, 0, 1, -1)), MDSteps(0), AtomCount(0),
     34  first(new bond(0, 0, 1, -1)), last(new bond(0, 0, 1, -1)), MDSteps(0),
    3535  BondCount(0), ElementCount(0), NoNonHydrogen(0), NoNonBonds(0), NoCyclicBonds(0), BondDistance(0.),
    3636  ActiveFlag(false), IndexNr(-1),
    3737  formula(this,boost::bind(&molecule::calcFormula,this)),
    38   last_atom(0),
    39   InternalPointer(begin())
     38  AtomCount(this,boost::bind(&molecule::doCountAtoms,this)), last_atom(0),  InternalPointer(begin())
    4039{
    4140  // init bond chain list
     
    7271const std::string molecule::getName(){
    7372  return std::string(name);
     73}
     74
     75int molecule::getAtomCount() const{
     76  return *AtomCount;
    7477}
    7578
     
    180183  if (pointer != NULL) {
    181184    pointer->sort = &pointer->nr;
    182     AtomCount++;
    183185    if (pointer->type != NULL) {
    184186      if (ElementsInMolecule[pointer->type->Z] == 0)
     
    214216    if ((pointer->type != NULL) && (pointer->type->Z != 1))
    215217      NoNonHydrogen++;
    216     AtomCount++;
    217218    retval=walker;
    218219  }
     
    611612
    612613  // copy values
    613   copy->CountAtoms();
    614614  copy->CountElements();
    615615  if (first->next != last) {  // if adjaceny list is present
     
    728728bool molecule::RemoveAtom(atom *pointer)
    729729{
     730  OBSERVE;
    730731  if (ElementsInMolecule[pointer->type->Z] != 0)  { // this would indicate an error
    731732    ElementsInMolecule[pointer->type->Z]--;  // decrease number of atom of this element
    732     AtomCount--;
    733733  } else
    734734    eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;
     
    909909    now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
    910910    for (int step=0;step<MDSteps;step++) {
    911       *output << AtomCount << "\n\tCreated by molecuilder, step " << step << ", on " << ctime(&now);
     911      *output << getAtomCount() << "\n\tCreated by molecuilder, step " << step << ", on " << ctime(&now);
    912912      ActOnAllAtoms( &atom::OutputTrajectoryXYZ, output, step );
    913913    }
     
    926926  if (output != NULL) {
    927927    now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
    928     *output << AtomCount << "\n\tCreated by molecuilder on " << ctime(&now);
     928    *output << getAtomCount() << "\n\tCreated by molecuilder on " << ctime(&now);
    929929    ActOnAllAtoms( &atom::OutputXYZLine, output );
    930930    return true;
     
    936936 * \param *out output stream for debugging
    937937 */
    938 void molecule::CountAtoms()
    939 {
    940   int i = size();
    941   if ((AtomCount == 0) || (i != AtomCount)) {
    942     cout << "!!!!!!!!! Counting needed" << endl;
    943     Log() << Verbose(3) << "Mismatch in AtomCount " << AtomCount << " and recounted number " << i << ", renaming all." << endl;
    944     AtomCount = i;
    945 
    946     // count NonHydrogen atoms and give each atom a unique name
    947     if (AtomCount != 0) {
    948       i=0;
    949       NoNonHydrogen = 0;
    950       for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
    951         (*iter)->nr = i;   // update number in molecule (for easier referencing in FragmentMolecule lateron)
    952         if ((*iter)->type->Z != 1) // count non-hydrogen atoms whilst at it
    953           NoNonHydrogen++;
    954         Free(&(*iter)->Name);
    955         (*iter)->Name = Malloc<char>(6, "molecule::CountAtoms: *walker->Name");
    956         sprintf((*iter)->Name, "%2s%02d", (*iter)->type->symbol, (*iter)->nr+1);
    957         Log() << Verbose(3) << "Naming atom nr. " << (*iter)->nr << " " << (*iter)->Name << "." << endl;
    958         i++;
    959       }
    960     } else
    961       Log() << Verbose(3) << "AtomCount is still " << AtomCount << ", thus counting nothing." << endl;
    962   }
     938int molecule::doCountAtoms()
     939{
     940  int res = size();
     941  int i = 0;
     942  NoNonHydrogen = 0;
     943  for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
     944    (*iter)->nr = i;   // update number in molecule (for easier referencing in FragmentMolecule lateron)
     945    if ((*iter)->type->Z != 1) // count non-hydrogen atoms whilst at it
     946      NoNonHydrogen++;
     947    Free(&(*iter)->Name);
     948    (*iter)->Name = Malloc<char>(6, "molecule::CountAtoms: *walker->Name");
     949    sprintf((*iter)->Name, "%2s%02d", (*iter)->type->symbol, (*iter)->nr+1);
     950    Log() << Verbose(3) << "Naming atom nr. " << (*iter)->nr << " " << (*iter)->Name << "." << endl;
     951    i++;
     952  }
     953  return res;
    963954};
    964955
     
    10221013  /// first count both their atoms and elements and update lists thereby ...
    10231014  //Log() << Verbose(0) << "Counting atoms, updating list" << endl;
    1024   CountAtoms();
    1025   OtherMolecule->CountAtoms();
    10261015  CountElements();
    10271016  OtherMolecule->CountElements();
     
    10301019  /// -# AtomCount
    10311020  if (result) {
    1032     if (AtomCount != OtherMolecule->AtomCount) {
    1033       Log() << Verbose(4) << "AtomCounts don't match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;
     1021    if (getAtomCount() != OtherMolecule->getAtomCount()) {
     1022      Log() << Verbose(4) << "AtomCounts don't match: " << getAtomCount() << " == " << OtherMolecule->getAtomCount() << endl;
    10341023      result = false;
    1035     } else Log() << Verbose(4) << "AtomCounts match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;
     1024    } else Log() << Verbose(4) << "AtomCounts match: " << getAtomCount() << " == " << OtherMolecule->getAtomCount() << endl;
    10361025  }
    10371026  /// -# ElementCount
     
    10731062  if (result) {
    10741063    Log() << Verbose(5) << "Calculating distances" << endl;
    1075     Distances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: Distances");
    1076     OtherDistances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: OtherDistances");
     1064    Distances = Calloc<double>(getAtomCount(), "molecule::IsEqualToWithinThreshold: Distances");
     1065    OtherDistances = Calloc<double>(getAtomCount(), "molecule::IsEqualToWithinThreshold: OtherDistances");
    10771066    SetIndexedArrayForEachAtomTo ( Distances, &atom::nr, &atom::DistanceSquaredToVector, (const Vector &)CenterOfGravity);
    10781067    SetIndexedArrayForEachAtomTo ( OtherDistances, &atom::nr, &atom::DistanceSquaredToVector, (const Vector &)CenterOfGravity);
     
    10801069    /// ... sort each list (using heapsort (o(N log N)) from GSL)
    10811070    Log() << Verbose(5) << "Sorting distances" << endl;
    1082     PermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermMap");
    1083     OtherPermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *OtherPermMap");
    1084     gsl_heapsort_index (PermMap, Distances, AtomCount, sizeof(double), CompareDoubles);
    1085     gsl_heapsort_index (OtherPermMap, OtherDistances, AtomCount, sizeof(double), CompareDoubles);
    1086     PermutationMap = Calloc<int>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermutationMap");
     1071    PermMap = Calloc<size_t>(getAtomCount(), "molecule::IsEqualToWithinThreshold: *PermMap");
     1072    OtherPermMap = Calloc<size_t>(getAtomCount(), "molecule::IsEqualToWithinThreshold: *OtherPermMap");
     1073    gsl_heapsort_index (PermMap, Distances, getAtomCount(), sizeof(double), CompareDoubles);
     1074    gsl_heapsort_index (OtherPermMap, OtherDistances, getAtomCount(), sizeof(double), CompareDoubles);
     1075    PermutationMap = Calloc<int>(getAtomCount(), "molecule::IsEqualToWithinThreshold: *PermutationMap");
    10871076    Log() << Verbose(5) << "Combining Permutation Maps" << endl;
    1088     for(int i=AtomCount;i--;)
     1077    for(int i=getAtomCount();i--;)
    10891078      PermutationMap[PermMap[i]] = (int) OtherPermMap[i];
    10901079
     
    10921081    Log() << Verbose(4) << "Comparing distances" << endl;
    10931082    flag = 0;
    1094     for (int i=0;i<AtomCount;i++) {
     1083    for (int i=0;i<getAtomCount();i++) {
    10951084      Log() << Verbose(5) << "Distances squared: |" << Distances[PermMap[i]] << " - " << OtherDistances[OtherPermMap[i]] << "| = " << fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) << " ?<? " <<  threshold << endl;
    10961085      if (fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) > threshold*threshold)
     
    11291118{
    11301119  Log() << Verbose(3) << "Begin of GetFatherAtomicMap." << endl;
    1131   int *AtomicMap = Malloc<int>(AtomCount, "molecule::GetAtomicMap: *AtomicMap");
    1132   for (int i=AtomCount;i--;)
     1120  int *AtomicMap = Malloc<int>(getAtomCount(), "molecule::GetAtomicMap: *AtomicMap");
     1121  for (int i=getAtomCount();i--;)
    11331122    AtomicMap[i] = -1;
    11341123  if (OtherMolecule == this) {  // same molecule
    1135     for (int i=AtomCount;i--;) // no need as -1 means already that there is trivial correspondence
     1124    for (int i=getAtomCount();i--;) // no need as -1 means already that there is trivial correspondence
    11361125      AtomicMap[i] = i;
    11371126    Log() << Verbose(4) << "Map is trivial." << endl;
Note: See TracChangeset for help on using the changeset viewer.