Changeset f67817f


Ignore:
Timestamp:
Oct 25, 2011, 12:08:03 PM (13 years ago)
Author:
Frederik Heber <heber@…>
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:
d0b375b
Parents:
23fb72
git-author:
Frederik Heber <heber@…> (10/18/11 12:26:01)
git-committer:
Frederik Heber <heber@…> (10/25/11 12:08:03)
Message:

Extracted power set generation into own class PowerSetGenerator.

Location:
src
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • src/Fragmentation/Makefile.am

    r23fb72 rf67817f  
    1010        Fragmentation/KeySetsContainer.cpp \
    1111        Fragmentation/MatrixContainer.cpp \
     12        Fragmentation/PowerSetGenerator.cpp \
    1213        Fragmentation/UniqueFragments.cpp
    1314
     
    1516        Fragmentation/defs.hpp \
    1617        Fragmentation/EnergyMatrix.hpp \
     18        Fragmentation/ForceMatrix.hpp \
    1719        Fragmentation/fragmentation_helpers.hpp \
    18         Fragmentation/ForceMatrix.hpp \
    1920        Fragmentation/helpers.hpp \
    2021        Fragmentation/HessianMatrix.hpp \
    2122        Fragmentation/KeySetsContainer.hpp \
    2223        Fragmentation/MatrixContainer.hpp \
     24        Fragmentation/PowerSetGenerator.hpp \
    2325        Fragmentation/UniqueFragments.hpp
    2426
  • src/Fragmentation/fragmentation_helpers.cpp

    r23fb72 rf67817f  
    426426
    427427
    428 /** Clears the touched list
    429  * \param *out output stream for debugging
    430  * \param verbosity verbosity level
    431  * \param *&TouchedList touched list
    432  * \param SubOrder current suborder
    433  * \param TouchedIndex currently touched
    434  */
    435 void SPFragmentGenerator_ClearingTouched(int verbosity, int *&TouchedList, int SubOrder, int &TouchedIndex)
    436 {
    437   Log() << Verbose(1+verbosity) << "Clearing touched list." << endl;
    438   for (TouchedIndex=SubOrder+1;TouchedIndex--;)  // empty touched list
    439     TouchedList[TouchedIndex] = -1;
    440   TouchedIndex = 0;
    441 
    442 }
    443 
    444 /** Adds the current combination of the power set to the snake stack.
    445  * \param *out output stream for debugging
    446  * \param verbosity verbosity level
    447  * \param CurrentCombination
    448  * \param SetDimension maximum number of bits in power set
    449  * \param *FragmentSet snake stack to remove from
    450  * \param &BondsSet set of bonds
    451  * \param *&TouchedList touched list
    452  * \param TouchedIndex currently touched
    453  * \return number of set bits
    454  */
    455 int AddPowersetToSnakeStack(int verbosity, int CurrentCombination, int SetDimension, KeySet *FragmentSet, std::vector<bond *> &BondsSet, int *&TouchedList, int &TouchedIndex)
    456 {
    457   atom *OtherWalker = NULL;
    458   bool bit = false;
    459   KeySetTestPair TestKeySetInsert;
    460 
    461   int Added = 0;
    462   for (int j=0;j<SetDimension;j++) {  // pull out every bit by shifting
    463     bit = ((CurrentCombination & (1 << j)) != 0);  // mask the bit for the j-th bond
    464     if (bit) {  // if bit is set, we add this bond partner
    465       OtherWalker = BondsSet[j]->rightatom;  // rightatom is always the one more distant, i.e. the one to add
    466       //Log() << Verbose(1+verbosity) << "Current Bond is " << BondsSet[j] << ", checking on " << *OtherWalker << "." << endl;
    467       Log() << Verbose(2+verbosity) << "Adding " << *OtherWalker << " with nr " << OtherWalker->getNr() << "." << endl;
    468       TestKeySetInsert = FragmentSet->insert(OtherWalker->getNr());
    469       if (TestKeySetInsert.second) {
    470         TouchedList[TouchedIndex++] = OtherWalker->getNr();  // note as added
    471         Added++;
    472       } else {
    473         Log() << Verbose(2+verbosity) << "This was item was already present in the keyset." << endl;
    474       }
    475     } else {
    476       Log() << Verbose(2+verbosity) << "Not adding." << endl;
    477     }
    478   }
    479   return Added;
    480 };
    481 
    482 /** Counts the number of elements in a power set.
    483  * \param SetFirst begin iterator first bond
    484  * \param SetLast end iterator
    485  * \param *&TouchedList touched list
    486  * \param TouchedIndex currently touched
    487  * \return number of elements
    488  */
    489 int CountSetMembers(std::list<bond *>::const_iterator SetFirst, std::list<bond *>::const_iterator SetLast, int *&TouchedList, int TouchedIndex)
    490 {
    491   int SetDimension = 0;
    492   for( std::list<bond *>::const_iterator Binder = SetFirst;
    493       Binder != SetLast;
    494       ++Binder) {
    495     for (int k=TouchedIndex;k--;) {
    496       if ((*Binder)->Contains(TouchedList[k]))   // if we added this very endpiece
    497         SetDimension++;
    498     }
    499   }
    500   return SetDimension;
    501 };
    502 
    503 /** Fills a list of bonds from another
    504  * \param *BondsList bonds array/vector to fill
    505  * \param SetFirst begin iterator first bond
    506  * \param SetLast end iterator
    507  * \param *&TouchedList touched list
    508  * \param TouchedIndex currently touched
    509  * \return number of elements
    510  */
    511 int FillBondsList(std::vector<bond *> &BondsList, std::list<bond *>::const_iterator SetFirst, std::list<bond *>::const_iterator SetLast, int *&TouchedList, int TouchedIndex)
    512 {
    513   int SetDimension = 0;
    514   for( std::list<bond *>::const_iterator Binder = SetFirst;
    515       Binder != SetLast;
    516       ++Binder) {
    517     for (int k=0;k<TouchedIndex;k++) {
    518       if ((*Binder)->leftatom->getNr() == TouchedList[k])   // leftatom is always the closer one
    519         BondsList[SetDimension++] = (*Binder);
    520     }
    521   }
    522   return SetDimension;
    523 };
    524 
    525 /** Remove all items that were added on this SP level.
    526  * \param *out output stream for debugging
    527  * \param verbosity verbosity level
    528  * \param *FragmentSet snake stack to remove from
    529  * \param *&TouchedList touched list
    530  * \param TouchedIndex currently touched
    531  */
    532 void RemoveAllTouchedFromSnakeStack(int verbosity, KeySet *FragmentSet, int *&TouchedList, int &TouchedIndex)
    533 {
    534   int Removal = 0;
    535   for(int j=0;j<TouchedIndex;j++) {
    536     Removal = TouchedList[j];
    537     Log() << Verbose(2+verbosity) << "Removing item nr. " << Removal << " from snake stack." << endl;
    538     FragmentSet->erase(Removal);
    539     TouchedList[j] = -1;
    540   }
    541   DoLog(2) && (Log() << Verbose(2) << "Remaining local nr.s on snake stack are: ");
    542   for(KeySet::iterator runner = FragmentSet->begin(); runner != FragmentSet->end(); runner++)
    543     DoLog(0) && (Log() << Verbose(0) << (*runner) << " ");
    544   DoLog(0) && (Log() << Verbose(0) << endl);
    545   TouchedIndex = 0; // set Index to 0 for list of atoms added on this level
    546 };
    547 
    548 
    549428bool KeyCompare::operator() (const KeySet SubgraphA, const KeySet SubgraphB) const
    550429{
  • src/Fragmentation/fragmentation_helpers.hpp

    r23fb72 rf67817f  
    3636bool MarkUpdateCandidates(bool *AtomMask, std::map<double, std::pair<int,int> > &FinalRootCandidates, int Order, molecule *mol);
    3737void PrintAtomMask(bool *AtomMask, int AtomCount);
    38 void SPFragmentGenerator_ClearingTouched(int verbosity, int *&TouchedList, int SubOrder, int &TouchedIndex);
    39 int AddPowersetToSnakeStack(int verbosity, int CurrentCombination, int SetDimension, KeySet *FragmentSet, std::vector<bond *> &BondsSet, int *&TouchedList, int &TouchedIndex);
    40 int CountSetMembers(std::list<bond *>::const_iterator SetFirst, std::list<bond *>::const_iterator SetLast, int *&TouchedList, int TouchedIndex);
    41 int FillBondsList(std::vector<bond *> &BondsList, std::list<bond *>::const_iterator SetFirst, std::list<bond *>::const_iterator SetLast, int *&TouchedList, int TouchedIndex);
    42 void RemoveAllTouchedFromSnakeStack(int verbosity, KeySet *FragmentSet, int *&TouchedList, int &TouchedIndex);
    4338
    4439int CombineAllOrderListIntoOne(Graph *&FragmentList, Graph ***FragmentLowerOrdersList, KeyStack &RootStack, molecule *mol);
  • src/molecule.hpp

    r23fb72 rf67817f  
    224224  /// -# BOSSANOVA
    225225  void FragmentBOSSANOVA(Graph *&FragmentList, KeyStack &RootStack);
    226   int PowerSetGenerator(int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet);
    227226  molecule * StoreFragmentFromKeySet(KeySet &Leaflet, bool IsAngstroem);
    228   void SPFragmentGenerator(struct UniqueFragments *FragmentSearch, int RootDistance, std::vector<bond *> &BondsSet, int SetDimension, int SubOrder);
    229227  int LookForRemovalCandidate(KeySet *&Leaf, int *&ShortestPathList);
    230228  int GuesstimateFragmentCount(int order);
  • src/molecule_fragmentation.cpp

    r23fb72 rf67817f  
    3131#include "Element/periodentafel.hpp"
    3232#include "Fragmentation/fragmentation_helpers.hpp"
     33#include "Fragmentation/PowerSetGenerator.hpp"
    3334#include "Fragmentation/UniqueFragments.hpp"
    3435#include "Graph/BondGraph.hpp"
     
    658659};
    659660
    660 /** From a given set of Bond sorted by Shortest Path distance, create all possible fragments of size \a SetDimension.
    661  * -# loops over every possible combination (2^dimension of edge set)
    662  *  -# inserts current set, if there's still space left
    663  *    -# yes: calls SPFragmentGenerator with structure, created new edge list and size respective to root dist
    664 ance+1
    665  *    -# no: stores fragment into keyset list by calling InsertFragmentIntoGraph
    666  *  -# removes all items added into the snake stack (in UniqueFragments structure) added during level (root
    667 distance) and current set
    668  * \param FragmentSearch UniqueFragments structure with all values needed
    669  * \param RootDistance current shortest path level, whose set of edges is represented by **BondsSet
    670  * \param BondsSet array of bonds to check
    671  * \param SetDimension Number of possible bonds on this level (i.e. size of the array BondsSet[])
    672  * \param SubOrder remaining number of allowed vertices to add
    673  */
    674 void molecule::SPFragmentGenerator(struct UniqueFragments *FragmentSearch, int RootDistance, std::vector<bond *> &BondsSet, int SetDimension, int SubOrder)
    675 {
    676   int verbosity = 0; //FragmentSearch->ANOVAOrder-SubOrder;
    677   int NumCombinations;
    678   int bits, TouchedIndex, SubSetDimension, SP, Added;
    679   int SpaceLeft;
    680   int *TouchedList = new int[SubOrder + 1];
    681   KeySetTestPair TestKeySetInsert;
    682 
    683   NumCombinations = 1 << SetDimension;
    684 
    685   // here for all bonds of Walker all combinations of end pieces (from the bonds)
    686   // have to be added and for the remaining ANOVA order GraphCrawler be called
    687   // recursively for the next level
    688 
    689   Log() << Verbose(1+verbosity) << "Begin of SPFragmentGenerator." << endl;
    690   Log() << Verbose(1+verbosity) << "We are " << RootDistance << " away from Root, which is " << *FragmentSearch->getRoot() << ", SubOrder is " << SubOrder << ", SetDimension is " << SetDimension << " and this means " <<  NumCombinations-1 << " combination(s)." << endl;
    691 
    692   // initialised touched list (stores added atoms on this level)
    693   SPFragmentGenerator_ClearingTouched(verbosity, TouchedList, SubOrder, TouchedIndex);
    694 
    695   // create every possible combination of the endpieces
    696   Log() << Verbose(1+verbosity) << "Going through all combinations of the power set." << endl;
    697   for (int i=1;i<NumCombinations;i++) {  // sweep through all power set combinations (skip empty set!)
    698     // count the set bit of i
    699     bits = 0;
    700     for (int j=SetDimension;j--;)
    701       bits += (i & (1 << j)) >> j;
    702 
    703     Log() << Verbose(1+verbosity) << "Current set is " << Binary(i | (1 << SetDimension)) << ", number of bits is " << bits << "." << endl;
    704     if (bits <= SubOrder) { // if not greater than additional atoms allowed on stack, continue
    705       // --1-- add this set of the power set of bond partners to the snake stack
    706       Added = AddPowersetToSnakeStack(verbosity, i, SetDimension, FragmentSearch->FragmentSet, BondsSet, TouchedList, TouchedIndex);
    707 
    708       SpaceLeft = SubOrder - Added ;// SubOrder - bits; // due to item's maybe being already present, this does not work anymore
    709       if (SpaceLeft > 0) {
    710         Log() << Verbose(1+verbosity) << "There's still some space left on stack: " << SpaceLeft << "." << endl;
    711         if (SubOrder > 1) {    // Due to Added above we have to check extra whether we're not already reaching beyond the desired Order
    712           // --2-- look at all added end pieces of this combination, construct bond subsets and sweep through a power set of these by recursion
    713           SP = RootDistance+1;  // this is the next level
    714 
    715           // first count the members in the subset
    716           SubSetDimension = CountSetMembers(FragmentSearch->BondsPerSPList[SP].begin(), FragmentSearch->BondsPerSPList[SP].end(), TouchedList, TouchedIndex);
    717 
    718           // then allocate and fill the list
    719           std::vector<bond *> BondsList;
    720           BondsList.resize(SubSetDimension);
    721           SubSetDimension = FillBondsList(BondsList, FragmentSearch->BondsPerSPList[SP].begin(), FragmentSearch->BondsPerSPList[SP].end(), TouchedList, TouchedIndex);
    722 
    723           // then iterate
    724           Log() << Verbose(2+verbosity) << "Calling subset generator " << SP << " away from root " << *FragmentSearch->getRoot() << " with sub set dimension " << SubSetDimension << "." << endl;
    725           SPFragmentGenerator(FragmentSearch, SP, BondsList, SubSetDimension, SubOrder-bits);
    726         }
    727       } else {
    728         // --2-- otherwise store the complete fragment
    729         Log() << Verbose(1+verbosity) << "Enough items on stack for a fragment!" << endl;
    730         // store fragment as a KeySet
    731         DoLog(2) && (Log() << Verbose(2) << "Found a new fragment[" << FragmentSearch->FragmentCounter << "], local nr.s are: ");
    732         for(KeySet::iterator runner = FragmentSearch->FragmentSet->begin(); runner != FragmentSearch->FragmentSet->end(); runner++)
    733           DoLog(0) && (Log() << Verbose(0) << (*runner) << " ");
    734         DoLog(0) && (Log() << Verbose(0) << endl);
    735         FragmentSearch->InsertFragmentIntoGraph();
    736       }
    737 
    738       // --3-- remove all added items in this level from snake stack
    739       Log() << Verbose(1+verbosity) << "Removing all items that were added on this SP level " << RootDistance << "." << endl;
    740       RemoveAllTouchedFromSnakeStack(verbosity, FragmentSearch->FragmentSet, TouchedList, TouchedIndex);
    741     } else {
    742       Log() << Verbose(2+verbosity) << "More atoms to add for this set (" << bits << ") than space left on stack " << SubOrder << ", skipping this set." << endl;
    743     }
    744   }
    745   delete[](TouchedList);
    746   Log() << Verbose(1+verbosity) << "End of SPFragmentGenerator, " << RootDistance << " away from Root " << *FragmentSearch->getRoot() << " and SubOrder is " << SubOrder << "." << endl;
    747 };
    748 
    749 
    750 /** Creates a list of all unique fragments of certain vertex size from a given graph \a Fragment for a given root vertex in the context of \a this molecule.
    751  * -# initialises UniqueFragments structure
    752  * -# fills edge list via BFS
    753  * -# creates the fragment by calling recursive function SPFragmentGenerator with UniqueFragments structure, 0 as
    754  root distance, the edge set, its dimension and the current suborder
    755  * -# Free'ing structure
    756  * Note that we may use the fact that the atoms are SP-ordered on the atomstack. I.e. when popping always the last, we first get all
    757  * with SP of 2, then those with SP of 3, then those with SP of 4 and so on.
    758  * \param *out output stream for debugging
    759  * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
    760  * \param FragmentSearch UniqueFragments structure containing TEFactor, root atom and so on
    761  * \param RestrictedKeySet Restricted vertex set to use in context of molecule
    762  * \return number of inserted fragments
    763  * \note ShortestPathList in FragmentSearch structure is probably due to NumberOfAtomsSPLevel and SP not needed anymore
    764  */
    765 int molecule::PowerSetGenerator(int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet)
    766 {
    767   int Counter = FragmentSearch.FragmentCounter; // mark current value of counter
    768 
    769   DoLog(0) && (Log() << Verbose(0) << endl);
    770   DoLog(0) && (Log() << Verbose(0) << "Begin of PowerSetGenerator with order " << Order << " at Root " << *FragmentSearch.getRoot() << "." << endl);
    771 
    772   FragmentSearch.SetSPList(Order);
    773 
    774   // do a BFS search to fill the SP lists and label the found vertices
    775   FragmentSearch.FillSPListandLabelVertices(Order, RestrictedKeySet);
    776 
    777   // outputting all list for debugging
    778   FragmentSearch.OutputSPList(Order);
    779 
    780   // creating fragments with the found edge sets  (may be done in reverse order, faster)
    781   int SP = FragmentSearch.CountNumbersInBondsList(Order);
    782   DoLog(0) && (Log() << Verbose(0) << "Total number of edges is " << SP << "." << endl);
    783   if (SP >= (Order-1)) {
    784     // start with root (push on fragment stack)
    785     DoLog(0) && (Log() << Verbose(0) << "Starting fragment generation with " << *FragmentSearch.getRoot() << ", local nr is " << FragmentSearch.getRoot()->getNr() << "." << endl);
    786     FragmentSearch.FragmentSet->clear();
    787     DoLog(0) && (Log() << Verbose(0) << "Preparing subset for this root and calling generator." << endl);
    788 
    789     // prepare the subset and call the generator
    790     std::vector<bond*> BondsList;
    791     BondsList.resize(FragmentSearch.BondsPerSPCount[0]);
    792     ASSERT(FragmentSearch.BondsPerSPList[0].size() != 0,
    793         "molecule::PowerSetGenerator() - FragmentSearch.BondsPerSPList[0] contains no root bond.");
    794     BondsList[0] = (*FragmentSearch.BondsPerSPList[0].begin());  // on SP level 0 there's only the root bond
    795 
    796     SPFragmentGenerator(&FragmentSearch, 0, BondsList, FragmentSearch.BondsPerSPCount[0], Order);
    797   } else {
    798     DoLog(0) && (Log() << Verbose(0) << "Not enough total number of edges to build " << Order << "-body fragments." << endl);
    799   }
    800 
    801   // as FragmentSearch structure is used only once, we don't have to clean it anymore
    802   // remove root from stack
    803   DoLog(0) && (Log() << Verbose(0) << "Removing root again from stack." << endl);
    804   FragmentSearch.FragmentSet->erase(FragmentSearch.getRoot()->getNr());
    805 
    806   // free'ing the bonds lists
    807   FragmentSearch.ResetSPList(Order);
    808 
    809   // return list
    810   DoLog(0) && (Log() << Verbose(0) << "End of PowerSetGenerator." << endl);
    811   return (FragmentSearch.FragmentCounter - Counter);
    812 };
    813 
    814 
    815 
    816 
    817661/** Performs BOSSANOVA decomposition at selected sites, increasing the cutoff by one at these sites.
    818662 * -# constructs a complete keyset of the molecule
     
    844688  int RootNr = 0;
    845689  struct UniqueFragments FragmentSearch;
     690  class PowerSetGenerator PSG;
    846691
    847692  DoLog(0) && (Log() << Verbose(0) << "Begin of FragmentBOSSANOVA." << endl);
     
    901746      FragmentSearch.Leaflet = FragmentLowerOrdersList[RootNr][0];      // set to insertion graph
    902747      FragmentSearch.setRoot(Walker);
    903       NumMoleculesOfOrder[RootNr] = PowerSetGenerator(Walker->AdaptiveOrder, FragmentSearch, CompleteMolecule);
     748      NumMoleculesOfOrder[RootNr] = PSG(&FragmentSearch, Walker->AdaptiveOrder, CompleteMolecule);
    904749
    905750      // output resulting number
Note: See TracChangeset for help on using the changeset viewer.