source: src/Fragmentation/AdaptivityMap.cpp@ 730d7a

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 Candidate_v1.7.0 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 730d7a was 730d7a, checked in by Frederik Heber <heber@…>, 14 years ago

New class AdaptivityMap and moved some functions from fragmentation_helpers into it.

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * AdaptivityMap.cpp
10 *
11 * Created on: Oct 20, 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 "AdaptivityMap.hpp"
22
23#include <fstream>
24
25#include "CodePatterns/Log.hpp"
26
27#include "atom.hpp"
28#include "Helpers/defs.hpp"
29#include "Helpers/helpers.hpp"
30#include "molecule.hpp"
31
32/** Constructor of class AdaptivityMap.
33 *
34 */
35AdaptivityMap::AdaptivityMap()
36{}
37
38/** Destructor of class AdaptivityMap.
39 *
40 */
41AdaptivityMap::~AdaptivityMap()
42{}
43
44/** Inserts a (\a No, \a value) pair into the list, overwriting present one.
45 * Note if values are equal, No will decided on which is first
46 * \param *out output stream for debugging
47 * \param &AdaptiveCriteriaList list to insert into
48 * \param &IndexedKeySetList list to find key set for a given index \a No
49 * \param FragOrder current bond order of fragment
50 * \param No index of keyset
51 * \param value energy value
52 */
53void AdaptivityMap::InsertIntoAdaptiveCriteriaList(std::map<int, std::pair<double,int> > *AdaptiveCriteriaList, int FragOrder, int No, double Value) const
54{
55 const_iterator marker = find(No); // find keyset to Frag No.
56 if (marker != end()) { // if found
57 Value *= 1 + MYEPSILON*(*((*marker).second.begin())); // in case of equal energies this makes them not equal without changing anything actually
58 // as the smallest number in each set has always been the root (we use global id to keep the doubles away), seek smallest and insert into AtomMask
59 std::pair <map<int, pair<double,int> >::iterator, bool> InsertedElement = AdaptiveCriteriaList->insert( make_pair(*((*marker).second.begin()), pair<double,int>( fabs(Value), FragOrder) ));
60 std::map<int, pair<double,int> >::iterator PresentItem = InsertedElement.first;
61 if (!InsertedElement.second) { // this root is already present
62 if ((*PresentItem).second.second < FragOrder) // if order there is lower, update entry with higher-order term
63 //if ((*PresentItem).second.first < (*runner).first) // as higher-order terms are not always better, we skip this part (which would always include this site into adaptive increase)
64 { // if value is smaller, update value and order
65 (*PresentItem).second.first = fabs(Value);
66 (*PresentItem).second.second = FragOrder;
67 DoLog(2) && (Log() << Verbose(2) << "Updated element (" << (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "])." << endl);
68 } else {
69 DoLog(2) && (Log() << Verbose(2) << "Did not update element " << (*PresentItem).first << " as " << FragOrder << " is less than or equal to " << (*PresentItem).second.second << "." << endl);
70 }
71 } else {
72 DoLog(2) && (Log() << Verbose(2) << "Inserted element (" << (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "])." << endl);
73 }
74 } else {
75 DoLog(1) && (Log() << Verbose(1) << "No Fragment under No. " << No << "found." << endl);
76 }
77};
78
79
80/** Scans the adaptive order file and insert (index, value) into map.
81 * \param &path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
82 * \return adaptive criteria list from file
83 */
84std::map<int, std::pair<double,int> > * AdaptivityMap::ScanAdaptiveFileIntoMap(std::string &path)
85{
86 std::map<int, pair<double,int> > *AdaptiveCriteriaList = new std::map<int, pair<double,int> >;
87 int No = 0, FragOrder = 0;
88 double Value = 0.;
89 char buffer[MAXSTRINGSIZE];
90 std::string filename = path + ENERGYPERFRAGMENT;
91 std::ifstream InputFile(filename.c_str());
92
93 if (InputFile.fail()) {
94 DoeLog(1) && (eLog() << Verbose(1) << "Cannot find file " << filename << "." << endl);
95 return AdaptiveCriteriaList;
96 }
97
98 if (CountLinesinFile(InputFile) > 0) {
99 // each line represents a fragment root (Atom::Nr) id and its energy contribution
100 InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
101 InputFile.getline(buffer, MAXSTRINGSIZE);
102 while(!InputFile.eof()) {
103 InputFile.getline(buffer, MAXSTRINGSIZE);
104 if (strlen(buffer) > 2) {
105 //Log() << Verbose(2) << "Scanning: " << buffer << endl;
106 stringstream line(buffer);
107 line >> FragOrder;
108 line >> ws >> No;
109 line >> ws >> Value; // skip time entry
110 line >> ws >> Value;
111 No -= 1; // indices start at 1 in file, not 0
112 //Log() << Verbose(2) << " - yields (" << No << "," << Value << ", " << FragOrder << ")" << endl;
113
114 // clean the list of those entries that have been superceded by higher order terms already
115 InsertIntoAdaptiveCriteriaList(AdaptiveCriteriaList, FragOrder, No, Value);
116 }
117 }
118 // close and done
119 InputFile.close();
120 InputFile.clear();
121 }
122
123 return AdaptiveCriteriaList;
124};
125
126/** Maps adaptive criteria list back onto (Value, (Root Nr., Order))
127 * (i.e. sorted by value to pick the highest ones)
128 * \param *out output stream for debugging
129 * \param &AdaptiveCriteriaList list to insert into
130 * \param *mol molecule with atoms
131 * \return remapped list
132 */
133std::map<double, std::pair<int,int> > * AdaptivityMap::ReMapAdaptiveCriteriaListToValue(std::map<int, std::pair<double,int> > *AdaptiveCriteriaList, molecule *mol)
134{
135 atom *Walker = NULL;
136 std::map<double, std::pair<int,int> > *FinalRootCandidates = new std::map<double, std::pair<int,int> > ;
137 DoLog(1) && (Log() << Verbose(1) << "Root candidate list is: " << endl);
138 for(std::map<int, std::pair<double,int> >::iterator runner = AdaptiveCriteriaList->begin(); runner != AdaptiveCriteriaList->end(); runner++) {
139 Walker = mol->FindAtom((*runner).first);
140 if (Walker != NULL) {
141 //if ((*runner).second.second >= Walker->AdaptiveOrder) { // only insert if this is an "active" root site for the current order
142 if (!Walker->MaxOrder) {
143 DoLog(2) && (Log() << Verbose(2) << "(" << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "])" << endl);
144 FinalRootCandidates->insert( make_pair( (*runner).second.first, pair<int,int>((*runner).first, (*runner).second.second) ) );
145 } else {
146 DoLog(2) && (Log() << Verbose(2) << "Excluding (" << *Walker << ", " << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "]), as it has reached its maximum order." << endl);
147 }
148 } else {
149 DoeLog(0) && (eLog()<< Verbose(0) << "Atom No. " << (*runner).second.first << " was not found in this molecule." << endl);
150 performCriticalExit();
151 }
152 }
153 return FinalRootCandidates;
154};
155
156/** Counts lines in file.
157 * Note we are scanning lines from current position, not from beginning.
158 * \param InputFile file to be scanned.
159 */
160int AdaptivityMap::CountLinesinFile(std::ifstream &InputFile) const
161{
162 char *buffer = new char[MAXSTRINGSIZE];
163 int lines=0;
164
165 int PositionMarker = InputFile.tellg(); // not needed as Inputfile is copied, given by value, not by ref
166 // count the number of lines, i.e. the number of fragments
167 InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
168 InputFile.getline(buffer, MAXSTRINGSIZE);
169 while(!InputFile.eof()) {
170 InputFile.getline(buffer, MAXSTRINGSIZE);
171 lines++;
172 }
173 InputFile.seekg(PositionMarker, ios::beg);
174 delete[](buffer);
175 return lines;
176};
Note: See TracBrowser for help on using the repository browser.