source: src/Fragmentation/Fragmentation.cpp@ 8fc1a6

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 8fc1a6 was 52ed5b, checked in by Frederik Heber <heber@…>, 13 years ago

Ids handed out by molecule now start with 1.

  • Property mode set to 100644
File size: 30.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * Fragmentation.cpp
10 *
11 * Created on: Oct 18, 2011
12 * Author: heber
13 */
14
15#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif
18
19#include "CodePatterns/MemDebug.hpp"
20
21#include "Fragmentation.hpp"
22
23#include "CodePatterns/Assert.hpp"
24#include "CodePatterns/Info.hpp"
25#include "CodePatterns/Log.hpp"
26
27#include "Atom/atom.hpp"
28#include "Bond/bond.hpp"
29#include "Element/element.hpp"
30#include "Element/periodentafel.hpp"
31#include "Fragmentation/AdaptivityMap.hpp"
32#include "Fragmentation/fragmentation_helpers.hpp"
33#include "Fragmentation/Graph.hpp"
34#include "Fragmentation/KeySet.hpp"
35#include "Fragmentation/PowerSetGenerator.hpp"
36#include "Fragmentation/UniqueFragments.hpp"
37#include "Graph/BondGraph.hpp"
38#include "Graph/CheckAgainstAdjacencyFile.hpp"
39#include "molecule.hpp"
40#include "MoleculeLeafClass.hpp"
41#include "MoleculeListClass.hpp"
42#include "Parser/FormatParserStorage.hpp"
43#include "World.hpp"
44
45
46/** Constructor of class Fragmentation.
47 *
48 * \param _mol molecule for internal use (looking up atoms)
49 * \param _saturation whether to treat hydrogen special and saturate dangling bonds or not
50 */
51Fragmentation::Fragmentation(molecule *_mol, const enum HydrogenSaturation _saturation) :
52 mol(_mol),
53 saturation(_saturation)
54{}
55
56/** Destructor of class Fragmentation.
57 *
58 */
59Fragmentation::~Fragmentation()
60{}
61
62
63/** Performs a many-body bond order analysis for a given bond order.
64 * -# parses adjacency, keysets and orderatsite files
65 * -# RootStack is created for every subgraph (here, later we implement the "update 10 sites with highest energ
66y contribution", and that's why this consciously not done in the following loop)
67 * -# in a loop over all subgraphs
68 * -# calls FragmentBOSSANOVA with this RootStack and within the subgraph molecule structure
69 * -# creates molecule (fragment)s from the returned keysets (StoreFragmentFromKeySet)
70 * -# combines the generated molecule lists from all subgraphs
71 * -# saves to disk: fragment configs, adjacency, orderatsite, keyset files
72 * Note that as we split "this" molecule up into a list of subgraphs, i.e. a MoleculeListClass, we have two sets
73 * of vertex indices: Global always means the index in "this" molecule, whereas local refers to the molecule or
74 * subgraph in the MoleculeListClass.
75 * \param Order up to how many neighbouring bonds a fragment contains in BondOrderScheme::BottumUp scheme
76 * \param prefix prefix string for every fragment file name (may include path)
77 * \param &DFS contains bond structure analysis data
78 * \return 1 - continue, 2 - stop (no fragmentation occured)
79 */
80int Fragmentation::FragmentMolecule(int Order, std::string prefix, DepthFirstSearchAnalysis &DFS)
81{
82 MoleculeListClass *BondFragments = NULL;
83 int FragmentCounter;
84 MoleculeLeafClass *MolecularWalker = NULL;
85 MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
86 fstream File;
87 bool FragmentationToDo = true;
88 bool CheckOrder = false;
89 Graph **FragmentList = NULL;
90 Graph TotalGraph; // graph with all keysets however local numbers
91 int TotalNumberOfKeySets = 0;
92 atom ***ListOfLocalAtoms = NULL;
93 bool *AtomMask = NULL;
94
95 LOG(0, endl);
96 switch (saturation) {
97 case DoSaturate:
98 LOG(0, "I will treat hydrogen special and saturate dangling bonds with it.");
99 break;
100 case DontSaturate:
101 LOG(0, "Hydrogen is treated just like the rest of the lot.");
102 break;
103 default:
104 ASSERT(0, "Fragmentation::FragmentMolecule() - there is a saturation setting which I have no idea about.");
105 break;
106 }
107
108 // ++++++++++++++++++++++++++++ INITIAL STUFF: Bond structure analysis, file parsing, ... ++++++++++++++++++++++++++++++++++++++++++
109
110 // ===== 1. Check whether bond structure is same as stored in files ====
111
112 // === compare it with adjacency file ===
113 {
114 std::ifstream File;
115 std::string filename;
116 filename = prefix + ADJACENCYFILE;
117 File.open(filename.c_str(), ios::out);
118 LOG(1, "Looking at bond structure stored in adjacency file and comparing to present one ... ");
119
120 CheckAgainstAdjacencyFile FileChecker(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection());
121 FragmentationToDo = FragmentationToDo && FileChecker(File);
122 }
123
124 // === reset bond degree and perform CorrectBondDegree ===
125 for(World::MoleculeIterator iter = World::getInstance().getMoleculeIter();
126 iter != World::getInstance().moleculeEnd();
127 ++iter) {
128 // correct bond degree
129 World::AtomComposite Set = (*iter)->getAtomSet();
130 World::getInstance().getBondGraph()->CorrectBondDegree(Set);
131 }
132
133 // ===== 2. perform a DFS analysis to gather info on cyclic structure and a list of disconnected subgraphs =====
134 // NOTE: We assume here that DFS has been performed and molecule structure is current.
135 Subgraphs = DFS.getMoleculeStructure();
136
137 // ===== 3. if structure still valid, parse key set file and others =====
138 Graph ParsedFragmentList;
139 FragmentationToDo = FragmentationToDo && ParsedFragmentList.ParseKeySetFile(prefix);
140
141 // ===== 4. check globally whether there's something to do actually (first adaptivity check)
142 FragmentationToDo = FragmentationToDo && ParseOrderAtSiteFromFile(prefix);
143
144 // =================================== Begin of FRAGMENTATION ===============================
145 // ===== 6a. assign each keyset to its respective subgraph =====
146 const int MolCount = World::getInstance().numMolecules();
147 ListOfLocalAtoms = new atom **[MolCount];
148 for (int i=0;i<MolCount;i++)
149 ListOfLocalAtoms[i] = NULL;
150 FragmentCounter = 0;
151 Subgraphs->next->AssignKeySetsToFragment(mol, &ParsedFragmentList, ListOfLocalAtoms, FragmentList, FragmentCounter, true);
152 delete[](ListOfLocalAtoms);
153
154 // ===== 6b. prepare and go into the adaptive (Order<0), single-step (Order==0) or incremental (Order>0) cycle
155 KeyStack *RootStack = new KeyStack[Subgraphs->next->Count()];
156 AtomMask = new bool[mol->getAtomCount()+1];
157 AtomMask[mol->getAtomCount()] = false;
158 FragmentationToDo = false; // if CheckOrderAtSite just ones recommends fragmentation, we will save fragments afterwards
159 while ((CheckOrder = CheckOrderAtSite(AtomMask, &ParsedFragmentList, Order, prefix))) {
160 FragmentationToDo = FragmentationToDo || CheckOrder;
161 AtomMask[mol->getAtomCount()] = true; // last plus one entry is used as marker that we have been through this loop once already in CheckOrderAtSite()
162 // ===== 6b. fill RootStack for each subgraph (second adaptivity check) =====
163 Subgraphs->next->FillRootStackForSubgraphs(RootStack, AtomMask, (FragmentCounter = 0), saturation);
164
165 // ===== 7. fill the bond fragment list =====
166 FragmentCounter = 0;
167 MolecularWalker = Subgraphs;
168 while (MolecularWalker->next != NULL) {
169 MolecularWalker = MolecularWalker->next;
170 LOG(1, "Fragmenting subgraph " << MolecularWalker << ".");
171 if (MolecularWalker->Leaf->hasBondStructure()) {
172 // call BOSSANOVA method
173 LOG(0, endl << " ========== BOND ENERGY of subgraph " << FragmentCounter << " ========================= ");
174 FragmentBOSSANOVA(MolecularWalker->Leaf, FragmentList[FragmentCounter], RootStack[FragmentCounter]);
175 } else {
176 ELOG(1, "Subgraph " << MolecularWalker << " has no atoms!");
177 }
178 FragmentCounter++; // next fragment list
179 }
180 }
181 LOG(2, "CheckOrder is " << CheckOrder << ".");
182 delete[](RootStack);
183 delete[](AtomMask);
184
185 // ==================================== End of FRAGMENTATION ============================================
186
187 // ===== 8a. translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf)
188 Subgraphs->next->TranslateIndicesToGlobalIDs(FragmentList, (FragmentCounter = 0), TotalNumberOfKeySets, TotalGraph);
189
190 // free subgraph memory again
191 FragmentCounter = 0;
192 while (Subgraphs != NULL) {
193 // remove entry in fragment list
194 // remove subgraph fragment
195 MolecularWalker = Subgraphs->next;
196 Subgraphs->Leaf = NULL;
197 delete(Subgraphs);
198 Subgraphs = MolecularWalker;
199 }
200 // free fragment list
201 for (int i=0; i< FragmentCounter; ++i )
202 delete(FragmentList[i]);
203 delete[](FragmentList);
204
205 LOG(0, FragmentCounter << " subgraph fragments have been removed.");
206
207 // ===== 8b. gather keyset lists (graphs) from all subgraphs and transform into MoleculeListClass =====
208 //if (FragmentationToDo) { // we should always store the fragments again as coordination might have changed slightly without changing bond structure
209 // allocate memory for the pointer array and transmorph graphs into full molecular fragments
210 BondFragments = new MoleculeListClass(World::getPointer());
211 int k=0;
212 for(Graph::iterator runner = TotalGraph.begin(); runner != TotalGraph.end(); runner++) {
213 KeySet test = (*runner).first;
214 LOG(0, "Fragment No." << (*runner).second.first << " with TEFactor " << (*runner).second.second << ".");
215 BondFragments->insert(StoreFragmentFromKeySet(test, World::getInstance().getConfig()));
216 k++;
217 }
218 LOG(0, k << "/" << BondFragments->ListOfMolecules.size() << " fragments generated from the keysets.");
219
220 // ===== 9. Save fragments' configuration and keyset files et al to disk ===
221 if (BondFragments->ListOfMolecules.size() != 0) {
222 // create the SortIndex from BFS labels to order in the config file
223 int *SortIndex = NULL;
224 CreateMappingLabelsToConfigSequence(SortIndex);
225
226 LOG(1, "Writing " << BondFragments->ListOfMolecules.size() << " possible bond fragmentation configs");
227 bool write_status = true;
228 for (std::vector<std::string>::const_iterator iter = typelist.begin();
229 iter != typelist.end();
230 ++iter) {
231 LOG(2, "INFO: Writing bond fragments for type " << (*iter) << ".");
232 write_status = write_status
233 && BondFragments->OutputConfigForListOfFragments(
234 prefix,
235 SortIndex,
236 FormatParserStorage::getInstance().getTypeFromName(*iter));
237 }
238 if (write_status)
239 LOG(1, "All configs written.");
240 else
241 LOG(1, "Some config writing failed.");
242
243 // store force index reference file
244 BondFragments->StoreForcesFile(prefix, SortIndex);
245
246 // store keysets file
247 TotalGraph.StoreKeySetFile(prefix);
248
249 {
250 // store Adjacency file
251 std::string filename = prefix + ADJACENCYFILE;
252 mol->StoreAdjacencyToFile(filename);
253 }
254
255 // store Hydrogen saturation correction file
256 BondFragments->AddHydrogenCorrection(prefix);
257
258 // store adaptive orders into file
259 StoreOrderAtSiteFile(prefix);
260
261 // restore orbital and Stop values
262 //CalculateOrbitals(*configuration);
263
264 // free memory for bond part
265 LOG(1, "Freeing bond memory");
266 delete[](SortIndex);
267 } else {
268 LOG(1, "FragmentList is zero on return, splitting failed.");
269 }
270 // remove all create molecules again from the World including their atoms
271 for (MoleculeList::iterator iter = BondFragments->ListOfMolecules.begin();
272 !BondFragments->ListOfMolecules.empty();
273 iter = BondFragments->ListOfMolecules.begin()) {
274 // remove copied atoms and molecule again
275 molecule *mol = *iter;
276 mol->removeAtomsinMolecule();
277 World::getInstance().destroyMolecule(mol);
278 BondFragments->ListOfMolecules.erase(iter);
279 }
280 delete(BondFragments);
281 LOG(0, "End of bond fragmentation.");
282
283 return ((int)(!FragmentationToDo)+1); // 1 - continue, 2 - stop (no fragmentation occured)
284};
285
286
287/** Performs BOSSANOVA decomposition at selected sites, increasing the cutoff by one at these sites.
288 * -# constructs a complete keyset of the molecule
289 * -# In a loop over all possible roots from the given rootstack
290 * -# increases order of root site
291 * -# calls PowerSetGenerator with this order, the complete keyset and the rootkeynr
292 * -# for all consecutive lower levels PowerSetGenerator is called with the suborder, the higher order keyset
293as the restricted one and each site in the set as the root)
294 * -# these are merged into a fragment list of keysets
295 * -# All fragment lists (for all orders, i.e. from all destination fields) are merged into one list for return
296 * Important only is that we create all fragments, it is not important if we create them more than once
297 * as these copies are filtered out via use of the hash table (KeySet).
298 * \param *out output stream for debugging
299 * \param Fragment&*List list of already present keystacks (adaptive scheme) or empty list
300 * \param &RootStack stack with all root candidates (unequal to each atom in complete molecule if adaptive scheme is applied)
301 * \return pointer to Graph list
302 */
303void Fragmentation::FragmentBOSSANOVA(molecule *mol, Graph *&FragmentList, KeyStack &RootStack)
304{
305 Graph ***FragmentLowerOrdersList = NULL;
306 int NumLevels = 0;
307 int NumMolecules = 0;
308 int TotalNumMolecules = 0;
309 int *NumMoleculesOfOrder = NULL;
310 int Order = 0;
311 int UpgradeCount = RootStack.size();
312 KeyStack FragmentRootStack;
313 int RootKeyNr = 0;
314 int RootNr = 0;
315 UniqueFragments FragmentSearch;
316
317 LOG(0, "Begin of FragmentBOSSANOVA.");
318
319 // FragmentLowerOrdersList is a 2D-array of pointer to MoleculeListClass objects, one dimension represents the ANOVA expansion of a single order (i.e. 5)
320 // with all needed lower orders that are subtracted, the other dimension is the BondOrder (i.e. from 1 to 5)
321 NumMoleculesOfOrder = new int[UpgradeCount];
322 FragmentLowerOrdersList = new Graph**[UpgradeCount];
323
324 for(int i=0;i<UpgradeCount;i++) {
325 NumMoleculesOfOrder[i] = 0;
326 FragmentLowerOrdersList[i] = NULL;
327 }
328
329 FragmentSearch.Init(mol->FindAtom(RootKeyNr));
330
331 // Construct the complete KeySet which we need for topmost level only (but for all Roots)
332 KeySet CompleteMolecule;
333 for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
334 CompleteMolecule.insert((*iter)->GetTrueFather()->getNr());
335 }
336
337 // this can easily be seen: if Order is 5, then the number of levels for each lower order is the total sum of the number of levels above, as
338 // each has to be split up. E.g. for the second level we have one from 5th, one from 4th, two from 3th (which in turn is one from 5th, one from 4th),
339 // hence we have overall four 2th order levels for splitting. This also allows for putting all into a single array (FragmentLowerOrdersList[])
340 // with the order along the cells as this: 5433222211111111 for BondOrder 5 needing 16=pow(2,5-1) cells (only we use bit-shifting which is faster)
341 RootNr = 0; // counts through the roots in RootStack
342 while ((RootNr < UpgradeCount) && (!RootStack.empty())) {
343 RootKeyNr = RootStack.front();
344 RootStack.pop_front();
345 atom *Walker = mol->FindAtom(RootKeyNr);
346 // check cyclic lengths
347 //if ((MinimumRingSize[Walker->GetTrueFather()->getNr()] != -1) && (Walker->GetTrueFather()->AdaptiveOrder+1 > MinimumRingSize[Walker->GetTrueFather()->getNr()])) {
348 // LOG(0, "Bond order " << Walker->GetTrueFather()->AdaptiveOrder << " of Root " << *Walker << " greater than or equal to Minimum Ring size of " << MinimumRingSize << " found is not allowed.");
349 //} else
350 {
351 // increase adaptive order by one
352 Walker->GetTrueFather()->AdaptiveOrder++;
353 Order = Walker->AdaptiveOrder = Walker->GetTrueFather()->AdaptiveOrder;
354
355 // initialise Order-dependent entries of UniqueFragments structure
356 class PowerSetGenerator PSG(&FragmentSearch, Walker->AdaptiveOrder);
357
358 // allocate memory for all lower level orders in this 1D-array of ptrs
359 NumLevels = 1 << (Order-1); // (int)pow(2,Order);
360 FragmentLowerOrdersList[RootNr] = new Graph*[NumLevels];
361 for (int i=0;i<NumLevels;i++)
362 FragmentLowerOrdersList[RootNr][i] = NULL;
363
364 // create top order where nothing is reduced
365 LOG(0, "==============================================================================================================");
366 LOG(0, "Creating KeySets of Bond Order " << Order << " for " << *Walker << ", " << (RootStack.size()-RootNr) << " Roots remaining."); // , NumLevels is " << NumLevels << "
367
368 // Create list of Graphs of current Bond Order (i.e. F_{ij})
369 FragmentLowerOrdersList[RootNr][0] = new Graph;
370 FragmentSearch.PrepareForPowersetGeneration(1., FragmentLowerOrdersList[RootNr][0], Walker);
371 NumMoleculesOfOrder[RootNr] = PSG(CompleteMolecule, saturation);
372
373 // output resulting number
374 LOG(1, "Number of resulting KeySets is: " << NumMoleculesOfOrder[RootNr] << ".");
375 if (NumMoleculesOfOrder[RootNr] != 0) {
376 NumMolecules = 0;
377 } else {
378 Walker->GetTrueFather()->MaxOrder = true;
379 }
380 // now, we have completely filled each cell of FragmentLowerOrdersList[] for the current Walker->AdaptiveOrder
381 //NumMoleculesOfOrder[Walker->AdaptiveOrder-1] = NumMolecules;
382 TotalNumMolecules += NumMoleculesOfOrder[RootNr];
383// LOG(1, "Number of resulting molecules for Order " << (int)Walker->GetTrueFather()->AdaptiveOrder << " is: " << NumMoleculesOfOrder[RootNr] << ".");
384 RootStack.push_back(RootKeyNr); // put back on stack
385 RootNr++;
386 }
387 }
388 LOG(0, "==============================================================================================================");
389 LOG(1, "Total number of resulting molecules is: " << TotalNumMolecules << ".");
390 LOG(0, "==============================================================================================================");
391
392 // cleanup FragmentSearch structure
393 FragmentSearch.Cleanup();
394
395 // now, FragmentLowerOrdersList is complete, it looks - for BondOrder 5 - as this (number is the ANOVA Order of the terms therein)
396 // 5433222211111111
397 // 43221111
398 // 3211
399 // 21
400 // 1
401
402 // Subsequently, we combine all into a single list (FragmentList)
403 CombineAllOrderListIntoOne(FragmentList, FragmentLowerOrdersList, RootStack, mol);
404 FreeAllOrdersList(FragmentLowerOrdersList, RootStack, mol);
405 delete[](NumMoleculesOfOrder);
406
407 LOG(0, "End of FragmentBOSSANOVA.");
408};
409
410/** Stores a fragment from \a KeySet into \a molecule.
411 * First creates the minimal set of atoms from the KeySet, then creates the bond structure from the complete
412 * molecule and adds missing hydrogen where bonds were cut.
413 * \param *out output stream for debugging messages
414 * \param &Leaflet pointer to KeySet structure
415 * \param IsAngstroem whether we have Ansgtroem or bohrradius
416 * \return pointer to constructed molecule
417 */
418molecule * Fragmentation::StoreFragmentFromKeySet(KeySet &Leaflet, bool IsAngstroem)
419{
420 Info info(__func__);
421 atom **SonList = new atom*[mol->getAtomCount()+1];
422 molecule *Leaf = World::getInstance().createMolecule();
423
424 for(int i=0;i<=mol->getAtomCount();i++)
425 SonList[i] = NULL;
426
427 StoreFragmentFromKeySet_Init(mol, Leaf, Leaflet, SonList);
428 // create the bonds between all: Make it an induced subgraph and add hydrogen
429// LOG(2, "Creating bonds from father graph (i.e. induced subgraph creation).");
430 CreateInducedSubgraphOfFragment(mol, Leaf, SonList, IsAngstroem);
431
432 //Leaflet->Leaf->ScanForPeriodicCorrection(out);
433 delete[](SonList);
434 return Leaf;
435};
436
437
438/** Estimates by educated guessing (using upper limit) the expected number of fragments.
439 * The upper limit is
440 * \f[
441 * n = N \cdot C^k
442 * \f]
443 * where \f$C=2^c\f$ and c is the maximum bond degree over N number of atoms.
444 * \param *out output stream for debugging
445 * \param order bond order k
446 * \return number n of fragments
447 */
448int Fragmentation::GuesstimateFragmentCount(int order)
449{
450 size_t c = 0;
451 int FragmentCount;
452 // get maximum bond degree
453 for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
454 const BondList& ListOfBonds = (*iter)->getListOfBonds();
455 c = (ListOfBonds.size() > c) ? ListOfBonds.size() : c;
456 }
457 FragmentCount = mol->NoNonHydrogen*(1 << (c*order));
458 LOG(1, "Upper limit for this subgraph is " << FragmentCount << " for " << mol->NoNonHydrogen << " non-H atoms with maximum bond degree of " << c << ".");
459 return FragmentCount;
460};
461
462
463/** Checks whether the OrderAtSite is still below \a Order at some site.
464 * \param *AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
465 * \param *GlobalKeySetList list of keysets with global ids (valid in "this" molecule) needed for adaptive increase
466 * \param Order desired Order if positive, desired exponent in threshold criteria if negative (0 is single-step)
467 * \param path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
468 * \return true - needs further fragmentation, false - does not need fragmentation
469 */
470bool Fragmentation::CheckOrderAtSite(bool *AtomMask, Graph *GlobalKeySetList, int Order, std::string path)
471{
472 bool status = false;
473
474 // initialize mask list
475 for(int i=mol->getAtomCount();i--;)
476 AtomMask[i] = false;
477
478 if (Order < 0) { // adaptive increase of BondOrder per site
479 if (AtomMask[mol->getAtomCount()] == true) // break after one step
480 return false;
481
482 // transmorph graph keyset list into indexed KeySetList
483 if (GlobalKeySetList == NULL) {
484 ELOG(1, "Given global key set list (graph) is NULL!");
485 return false;
486 }
487 AdaptivityMap * IndexKeySetList = GlobalKeySetList->GraphToAdaptivityMap();
488
489 // parse the EnergyPerFragment file
490 IndexKeySetList->ScanAdaptiveFileIntoMap(path); // (Root No., (Value, Order)) !
491 // then map back onto (Value, (Root Nr., Order)) (i.e. sorted by value to pick the highest ones)
492 IndexKeySetList->ReMapAdaptiveCriteriaListToValue(mol);
493
494 // pick the ones still below threshold and mark as to be adaptively updated
495 if (IndexKeySetList->IsAdaptiveCriteriaListEmpty()) {
496 ELOG(2, "Unable to parse file, incrementing all.");
497 for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
498 if ((saturation == DontSaturate) || ((*iter)->getType()->getAtomicNumber() != 1)) // skip hydrogen
499 {
500 AtomMask[(*iter)->getNr()] = true; // include all (non-hydrogen) atoms
501 status = true;
502 }
503 }
504 } else {
505 IndexKeySetList->MarkUpdateCandidates(AtomMask, Order, mol);
506 }
507
508 delete[](IndexKeySetList);
509 } else { // global increase of Bond Order
510 for(molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
511 if ((saturation == DontSaturate) || ((*iter)->getType()->getAtomicNumber() != 1)) // skip hydrogen
512 {
513 AtomMask[(*iter)->getNr()] = true; // include all (non-hydrogen) atoms
514 if ((Order != 0) && ((*iter)->AdaptiveOrder < Order)) // && ((*iter)->AdaptiveOrder < MinimumRingSize[(*iter)->getNr()]))
515 status = true;
516 }
517 }
518 if ((!Order) && (!AtomMask[mol->getAtomCount()])) // single stepping, just check
519 status = true;
520
521 if (!status) {
522 if (Order == 0)
523 LOG(1, "Single stepping done.");
524 else
525 LOG(1, "Order at every site is already equal or above desired order " << Order << ".");
526 }
527 }
528
529 PrintAtomMask(AtomMask, mol->getAtomCount()); // for debugging
530
531 return status;
532};
533
534/** Stores pairs (Atom::Nr, Atom::AdaptiveOrder) into file.
535 * Atoms not present in the file get "-1".
536 * \param &path path to file ORDERATSITEFILE
537 * \return true - file writable, false - not writable
538 */
539bool Fragmentation::StoreOrderAtSiteFile(std::string &path)
540{
541 string line;
542 ofstream file;
543
544 line = path + ORDERATSITEFILE;
545 file.open(line.c_str());
546 LOG(1, "Writing OrderAtSite " << ORDERATSITEFILE << " ... ");
547 if (file.good()) {
548 for_each(mol->begin(),mol->end(),bind2nd(mem_fun(&atom::OutputOrder), &file));
549 file.close();
550 LOG(1, "done.");
551 return true;
552 } else {
553 LOG(1, "failed to open file " << line << ".");
554 return false;
555 }
556};
557
558
559/** Parses pairs(Atom::Nr, Atom::AdaptiveOrder) from file and stores in molecule's Atom's.
560 * Atoms not present in the file get "0".
561 * \param &path path to file ORDERATSITEFILEe
562 * \return true - file found and scanned, false - file not found
563 * \sa ParseKeySetFile() and CheckAdjacencyFileAgainstMolecule() as this is meant to be used in conjunction with the two
564 */
565bool Fragmentation::ParseOrderAtSiteFromFile(std::string &path)
566{
567 unsigned char *OrderArray = new unsigned char[mol->getAtomCount()];
568 bool *MaxArray = new bool[mol->getAtomCount()];
569 bool status;
570 int AtomNr, value;
571 string line;
572 ifstream file;
573
574 for(int i=0;i<mol->getAtomCount();i++) {
575 OrderArray[i] = 0;
576 MaxArray[i] = false;
577 }
578
579 LOG(1, "Begin of ParseOrderAtSiteFromFile");
580 line = path + ORDERATSITEFILE;
581 file.open(line.c_str());
582 if (file.good()) {
583 while (!file.eof()) { // parse from file
584 AtomNr = -1;
585 file >> AtomNr;
586 if (AtomNr != -1) { // test whether we really parsed something (this is necessary, otherwise last atom is set twice and to 0 on second time)
587 file >> value;
588 OrderArray[AtomNr] = value;
589 file >> value;
590 MaxArray[AtomNr] = value;
591 //LOG(2, "AtomNr " << AtomNr << " with order " << (int)OrderArray[AtomNr] << " and max order set to " << (int)MaxArray[AtomNr] << ".");
592 }
593 }
594 file.close();
595
596 // set atom values
597 for(molecule::iterator iter=mol->begin();iter!=mol->end();++iter){
598 (*iter)->AdaptiveOrder = OrderArray[(*iter)->getNr()];
599 (*iter)->MaxOrder = MaxArray[(*iter)->getNr()];
600 }
601 //SetAtomValueToIndexedArray( OrderArray, &atom::getNr(), &atom::AdaptiveOrder );
602 //SetAtomValueToIndexedArray( MaxArray, &atom::getNr(), &atom::MaxOrder );
603
604 LOG(1, "\t ... done.");
605 status = true;
606 } else {
607 LOG(1, "\t ... failed to open file " << line << ".");
608 status = false;
609 }
610 delete[](OrderArray);
611 delete[](MaxArray);
612
613 LOG(1, "End of ParseOrderAtSiteFromFile");
614 return status;
615};
616
617/** Create a SortIndex to map from atomic labels to the sequence in which the atoms are given in the config file.
618 * \param *out output stream for debugging
619 * \param *&SortIndex Mapping array of size molecule::AtomCount
620 * \return true - success, false - failure of SortIndex alloc
621 */
622bool Fragmentation::CreateMappingLabelsToConfigSequence(int *&SortIndex)
623{
624 if (SortIndex != NULL) {
625 LOG(1, "SortIndex is " << SortIndex << " and not NULL as expected.");
626 return false;
627 }
628 SortIndex = new int[mol->getAtomCount()+1];
629 for(int i=mol->getAtomCount()+1;i--;)
630 SortIndex[i] = -1;
631
632 int AtomNo = 0;
633 for(molecule::const_iterator iter=mol->begin();iter!=mol->end();++iter){
634 ASSERT(SortIndex[(*iter)->getNr()]==-1,"Same SortIndex set twice");
635 SortIndex[(*iter)->getNr()] = AtomNo++;
636 }
637
638 return true;
639};
640
641
642/** Initializes some value for putting fragment of \a *mol into \a *Leaf.
643 * \param *mol total molecule
644 * \param *Leaf fragment molecule
645 * \param &Leaflet pointer to KeySet structure
646 * \param **SonList calloc'd list which atom of \a *Leaf is a son of which atom in \a *mol
647 * \return number of atoms in fragment
648 */
649int Fragmentation::StoreFragmentFromKeySet_Init(molecule *mol, molecule *Leaf, KeySet &Leaflet, atom **SonList)
650{
651 atom *FatherOfRunner = NULL;
652
653 // first create the minimal set of atoms from the KeySet
654 int size = 0;
655 for(KeySet::iterator runner = Leaflet.begin(); runner != Leaflet.end(); runner++) {
656 FatherOfRunner = mol->FindAtom((*runner)); // find the id
657 SonList[FatherOfRunner->getNr()] = Leaf->AddCopyAtom(FatherOfRunner);
658 size++;
659 }
660 return size;
661};
662
663
664/** Creates an induced subgraph out of a fragmental key set, adding bonds and hydrogens (if treated specially).
665 * \param *out output stream for debugging messages
666 * \param *mol total molecule
667 * \param *Leaf fragment molecule
668 * \param IsAngstroem whether we have Ansgtroem or bohrradius
669 * \param **SonList list which atom of \a *Leaf is a son of which atom in \a *mol
670 */
671void Fragmentation::CreateInducedSubgraphOfFragment(molecule *mol, molecule *Leaf, atom **SonList, bool IsAngstroem)
672{
673 bool LonelyFlag = false;
674 atom *OtherFather = NULL;
675 atom *FatherOfRunner = NULL;
676
677 // we increment the iter just before skipping the hydrogen
678 // as we use AddBond, we cannot have a const_iterator here
679 for (molecule::iterator iter = Leaf->begin(); iter != Leaf->end();) {
680 LonelyFlag = true;
681 FatherOfRunner = (*iter)->father;
682 ASSERT(FatherOfRunner,"Atom without father found");
683 if (SonList[FatherOfRunner->getNr()] != NULL) { // check if this, our father, is present in list
684 // create all bonds
685 const BondList& ListOfBonds = FatherOfRunner->getListOfBonds();
686 for (BondList::const_iterator BondRunner = ListOfBonds.begin();
687 BondRunner != ListOfBonds.end();
688 ++BondRunner) {
689 OtherFather = (*BondRunner)->GetOtherAtom(FatherOfRunner);
690 if (SonList[OtherFather->getNr()] != NULL) {
691// LOG(2, "INFO: Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->getNr()]
692// << " is bound to " << *OtherFather << ", whose son is "
693// << *SonList[OtherFather->getNr()] << ".");
694 if (OtherFather->getNr() > FatherOfRunner->getNr()) { // add bond (Nr check is for adding only one of both variants: ab, ba)
695 std::stringstream output;
696// output << "ACCEPT: Adding Bond: "
697 output << Leaf->AddBond((*iter), SonList[OtherFather->getNr()], (*BondRunner)->BondDegree);
698// LOG(3, output.str());
699 //NumBonds[(*iter)->getNr()]++;
700 } else {
701// LOG(3, "REJECY: Not adding bond, labels in wrong order.");
702 }
703 LonelyFlag = false;
704 } else {
705// LOG(2, "INFO: Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->getNr()]
706// << " is bound to " << *OtherFather << ", who has no son in this fragment molecule.");
707 if (saturation == DoSaturate) {
708// LOG(3, "ACCEPT: Adding Hydrogen to " << (*iter)->Name << " and a bond in between.");
709 if (!Leaf->AddHydrogenReplacementAtom((*BondRunner), (*iter), FatherOfRunner, OtherFather, IsAngstroem))
710 exit(1);
711 }
712 //NumBonds[(*iter)->getNr()] += Binder->BondDegree;
713 }
714 }
715 } else {
716 ELOG(1, "Son " << (*iter)->getName() << " has father " << FatherOfRunner->getName() << " but its entry in SonList is " << SonList[FatherOfRunner->getNr()] << "!");
717 }
718 if ((LonelyFlag) && (Leaf->getAtomCount() > 1)) {
719 LOG(0, **iter << "has got bonds only to hydrogens!");
720 }
721 ++iter;
722 if (saturation == DoSaturate) {
723 while ((iter != Leaf->end()) && ((*iter)->getType()->getAtomicNumber() == 1)){ // skip added hydrogen
724 iter++;
725 }
726 }
727 }
728};
729
730/** Sets the desired output types of the fragment configurations.
731 *
732 * @param types vector of desired types.
733 */
734void Fragmentation::setOutputTypes(const std::vector<std::string> &types)
735{
736 typelist = types;
737}
Note: See TracBrowser for help on using the repository browser.