source: src/Fragmentation/Exporters/ExportGraph.cpp@ 6cabaac

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
Last change on this file since 6cabaac was 6cabaac, checked in by Frederik Heber <heber@…>, 12 years ago

Extended ExportGraph to spill our molecular fragments one by one.

  • the biggest scaling issue with fragmentation is the number of hydrogen atoms that are created to saturate all (e.g. 200.000 fragments frm 3.000 atoms at order 6!) the molecular fragments.
  • hence, create one by one and have a controlled pool of hydrogens that are re-used.
  • Property mode set to 100644
File size: 9.4 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2011 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * ExportGraph.cpp
25 *
26 * Created on: 08.03.2012
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
37#include "ExportGraph.hpp"
38
39#include "CodePatterns/Info.hpp"
40#include "CodePatterns/Log.hpp"
41
42#include "Bond/bond.hpp"
43#include "Element/element.hpp"
44#include "Fragmentation/Graph.hpp"
45#include "Fragmentation/KeySet.hpp"
46#include "Fragmentation/SortIndex.hpp"
47#include "Graph/ListOfLocalAtoms.hpp"
48#include "molecule.hpp"
49#include "MoleculeListClass.hpp"
50#include "World.hpp"
51
52/** Constructor for class ExportGraph.
53 *
54 * @param _graph
55 */
56ExportGraph::ExportGraph(
57 const Graph &_graph,
58 const enum HydrogenTreatment _treatment,
59 const enum HydrogenSaturation _saturation) :
60 TotalGraph(_graph),
61 CurrentKeySet(TotalGraph.begin()),
62 BondFragments(World::getPointer()),
63 treatment(_treatment),
64 saturation(_saturation)
65{
66}
67
68/** Destructor for class ExportGraph.
69 *
70 */
71ExportGraph::~ExportGraph()
72{
73 // remove all create molecules again from the World including their atoms
74 for (MoleculeList::iterator iter = BondFragments.ListOfMolecules.begin();
75 !BondFragments.ListOfMolecules.empty();
76 iter = BondFragments.ListOfMolecules.begin()) {
77 // remove copied atoms and molecule again
78 molecule *mol = *iter;
79 mol->removeAtomsinMolecule();
80 World::getInstance().destroyMolecule(mol);
81 BondFragments.ListOfMolecules.erase(iter);
82 }
83}
84
85void ExportGraph::operator()()
86{
87 if (BondFragments.ListOfMolecules.size() == 0)
88 prepareMolecule();
89}
90
91void ExportGraph::reset()
92{
93 CurrentKeySet = TotalGraph.begin();
94}
95
96ExportGraph::SaturatedFragment_ptr ExportGraph::getNextFragment()
97{
98 // if a fragment is still leased, return zero ptr.
99 if (!KeySetsInUse.empty()) {
100 ELOG(1, "Leasing KeySet while old one is not returned.");
101 return SaturatedFragment_ptr();
102 }
103
104 // else return current fragment or indicate end
105 if (CurrentKeySet != TotalGraph.end()) {
106 const KeySet &set = (CurrentKeySet++)->first;
107 return leaseFragment(set);
108 } else {
109 return leaseFragment(EmptySet);
110 }
111}
112
113ExportGraph::SaturatedFragment_ptr ExportGraph::leaseFragment(const KeySet &_set)
114{
115 // create the saturation which adds itself to KeySetsInUse
116 SaturatedFragment_ptr _ptr(new SaturatedFragment(_set, KeySetsInUse, hydrogens));
117 // and return
118 return _ptr;
119}
120
121void ExportGraph::releaseFragment(SaturatedFragment_ptr &_ptr)
122{
123 ASSERT(_ptr != NULL,
124 "ExportGraph::releaseFragment() - pointer is NULL.");
125 SaturatedFragment::KeySetsInUse_t::iterator iter = KeySetsInUse.find(_ptr->getKeySet());
126 if (iter == KeySetsInUse.end()) {
127 ASSERT(0,
128 "ExportGraph::releaseFragment() - returning unknown set "
129 +toString(_ptr->getKeySet())+".");
130 return;
131 } else {
132 // release instance which removes itself in KeySetsInUse
133 _ptr.reset();
134 }
135}
136
137/** Internal helper to create from each keyset a molecule
138 *
139 */
140void ExportGraph::prepareMolecule()
141{
142 size_t count = 0;
143 for(Graph::const_iterator runner = TotalGraph.begin(); runner != TotalGraph.end(); runner++) {
144 KeySet test = (*runner).first;
145 LOG(2, "DEBUG: Fragment No." << (*runner).second.first << " with TEFactor "
146 << (*runner).second.second << ".");
147 BondFragments.insert(StoreFragmentFromKeySet(test, World::getInstance().getConfig()));
148 ++count;
149 }
150 LOG(1, "INFO: " << count << "/" << BondFragments.ListOfMolecules.size()
151 << " fragments generated from the keysets.");
152}
153
154/** Stores a fragment from \a KeySet into \a molecule.
155 * First creates the minimal set of atoms from the KeySet, then creates the bond structure from the complete
156 * molecule and adds missing hydrogen where bonds were cut.
157 * \param &Leaflet pointer to KeySet structure
158 * \param IsAngstroem whether we have Ansgtroem or bohrradius
159 * \return pointer to constructed molecule
160 */
161molecule * ExportGraph::StoreFragmentFromKeySet(KeySet &Leaflet, bool IsAngstroem)
162{
163 Info info(__func__);
164 ListOfLocalAtoms_t SonList;
165 molecule *Leaf = World::getInstance().createMolecule();
166
167 StoreFragmentFromKeySet_Init(Leaf, Leaflet, SonList);
168 // create the bonds between all: Make it an induced subgraph and add hydrogen
169// LOG(2, "Creating bonds from father graph (i.e. induced subgraph creation).");
170 CreateInducedSubgraphOfFragment(Leaf, SonList, IsAngstroem);
171
172 //Leaflet->Leaf->ScanForPeriodicCorrection(out);
173 return Leaf;
174}
175
176/** Initializes some value for putting fragment of \a *mol into \a *Leaf.
177 * \param *Leaf fragment molecule
178 * \param &Leaflet pointer to KeySet structure
179 * \param SonList calloc'd list which atom of \a *Leaf is a son of which atom in \a *mol
180 * \return number of atoms in fragment
181 */
182int ExportGraph::StoreFragmentFromKeySet_Init(molecule *Leaf, KeySet &Leaflet, ListOfLocalAtoms_t &SonList)
183{
184 atom *FatherOfRunner = NULL;
185
186 // first create the minimal set of atoms from the KeySet
187 World &world = World::getInstance();
188 int size = 0;
189 for(KeySet::const_iterator runner = Leaflet.begin(); runner != Leaflet.end(); runner++) {
190 FatherOfRunner = world.getAtom(AtomById(*runner)); // find the id
191 SonList.insert( std::make_pair(FatherOfRunner->getNr(), Leaf->AddCopyAtom(FatherOfRunner) ) );
192 size++;
193 }
194 return size;
195}
196
197/** Creates an induced subgraph out of a fragmental key set, adding bonds and hydrogens (if treated specially).
198 * \param *Leaf fragment molecule
199 * \param IsAngstroem whether we have Ansgtroem or bohrradius
200 * \param SonList list which atom of \a *Leaf is another atom's son
201 */
202void ExportGraph::CreateInducedSubgraphOfFragment(molecule *Leaf, ListOfLocalAtoms_t &SonList, bool IsAngstroem)
203{
204 bool LonelyFlag = false;
205 atom *OtherFather = NULL;
206 atom *FatherOfRunner = NULL;
207
208 // we increment the iter just before skipping the hydrogen
209 // as we use AddBond, we cannot have a const_iterator here
210 for (molecule::iterator iter = Leaf->begin(); iter != Leaf->end();) {
211 LonelyFlag = true;
212 FatherOfRunner = (*iter)->father;
213 ASSERT(FatherOfRunner,"Atom without father found");
214 if (SonList.find(FatherOfRunner->getNr()) != SonList.end()) { // check if this, our father, is present in list
215 // create all bonds
216 const BondList& ListOfBonds = FatherOfRunner->getListOfBonds();
217 for (BondList::const_iterator BondRunner = ListOfBonds.begin();
218 BondRunner != ListOfBonds.end();
219 ++BondRunner) {
220 OtherFather = (*BondRunner)->GetOtherAtom(FatherOfRunner);
221 if (SonList.find(OtherFather->getNr()) != SonList.end()) {
222// LOG(2, "INFO: Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->getNr()]
223// << " is bound to " << *OtherFather << ", whose son is "
224// << *SonList[OtherFather->getNr()] << ".");
225 if (OtherFather->getNr() > FatherOfRunner->getNr()) { // add bond (Nr check is for adding only one of both variants: ab, ba)
226 std::stringstream output;
227// output << "ACCEPT: Adding Bond: "
228 output << Leaf->AddBond((*iter), SonList[OtherFather->getNr()], (*BondRunner)->BondDegree);
229// LOG(3, output.str());
230 //NumBonds[(*iter)->getNr()]++;
231 } else {
232// LOG(3, "REJECY: Not adding bond, labels in wrong order.");
233 }
234 LonelyFlag = false;
235 } else {
236// LOG(2, "INFO: Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->getNr()]
237// << " is bound to " << *OtherFather << ", who has no son in this fragment molecule.");
238 if (saturation == DoSaturate) {
239// LOG(3, "ACCEPT: Adding Hydrogen to " << (*iter)->Name << " and a bond in between.");
240 if (!Leaf->AddHydrogenReplacementAtom((*BondRunner), (*iter), FatherOfRunner, OtherFather, IsAngstroem))
241 exit(1);
242 } else if ((treatment == ExcludeHydrogen) && (OtherFather->getElementNo() == (atomicNumber_t)1)) {
243 // just copy the atom if it's a hydrogen
244 atom * const OtherWalker = Leaf->AddCopyAtom(OtherFather);
245 Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->BondDegree);
246 }
247 //NumBonds[(*iter)->getNr()] += Binder->BondDegree;
248 }
249 }
250 } else {
251 ELOG(1, "Son " << (*iter)->getName() << " has father " << FatherOfRunner->getName() << " but its entry in SonList is " << SonList[FatherOfRunner->getNr()] << "!");
252 }
253 if ((LonelyFlag) && (Leaf->getAtomCount() > 1)) {
254 LOG(0, **iter << "has got bonds only to hydrogens!");
255 }
256 ++iter;
257 if (saturation == DoSaturate) {
258 while ((iter != Leaf->end()) && ((*iter)->getType()->getAtomicNumber() == 1)){ // skip added hydrogen
259 iter++;
260 }
261 }
262 }
263}
Note: See TracBrowser for help on using the repository browser.