[730d7a] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[730d7a] | 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * AdaptivityMap.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Oct 20, 2011
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | #ifdef HAVE_CONFIG_H
|
---|
| 31 | #include <config.h>
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 35 |
|
---|
| 36 | #include "AdaptivityMap.hpp"
|
---|
| 37 |
|
---|
| 38 | #include <fstream>
|
---|
| 39 |
|
---|
[851be8] | 40 | #include "CodePatterns/Assert.hpp"
|
---|
[730d7a] | 41 | #include "CodePatterns/Log.hpp"
|
---|
| 42 |
|
---|
[6f0841] | 43 | #include "Atom/atom.hpp"
|
---|
[f96874] | 44 | #include "Fragmentation/AtomMask.hpp"
|
---|
[730d7a] | 45 | #include "Helpers/defs.hpp"
|
---|
| 46 | #include "Helpers/helpers.hpp"
|
---|
| 47 | #include "molecule.hpp"
|
---|
| 48 |
|
---|
| 49 | /** Constructor of class AdaptivityMap.
|
---|
| 50 | *
|
---|
| 51 | */
|
---|
| 52 | AdaptivityMap::AdaptivityMap()
|
---|
| 53 | {}
|
---|
| 54 |
|
---|
| 55 | /** Destructor of class AdaptivityMap.
|
---|
| 56 | *
|
---|
| 57 | */
|
---|
| 58 | AdaptivityMap::~AdaptivityMap()
|
---|
| 59 | {}
|
---|
| 60 |
|
---|
| 61 | /** Inserts a (\a No, \a value) pair into the list, overwriting present one.
|
---|
| 62 | * Note if values are equal, No will decided on which is first
|
---|
| 63 | * \param *out output stream for debugging
|
---|
| 64 | * \param &AdaptiveCriteriaList list to insert into
|
---|
| 65 | * \param &IndexedKeySetList list to find key set for a given index \a No
|
---|
| 66 | * \param FragOrder current bond order of fragment
|
---|
| 67 | * \param No index of keyset
|
---|
| 68 | * \param value energy value
|
---|
| 69 | */
|
---|
[851be8] | 70 | void AdaptivityMap::InsertIntoAdaptiveCriteriaList(int FragOrder, int No, double Value)
|
---|
[730d7a] | 71 | {
|
---|
[851be8] | 72 | ASSERT( AdaptiveCriteriaList != NULL,
|
---|
| 73 | "AdaptivityMap::InsertIntoAdaptiveCriteriaList() - AdaptiveCriteriaList is not allocated yet.");
|
---|
[730d7a] | 74 | const_iterator marker = find(No); // find keyset to Frag No.
|
---|
| 75 | if (marker != end()) { // if found
|
---|
| 76 | Value *= 1 + MYEPSILON*(*((*marker).second.begin())); // in case of equal energies this makes them not equal without changing anything actually
|
---|
| 77 | // 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
|
---|
| 78 | std::pair <map<int, pair<double,int> >::iterator, bool> InsertedElement = AdaptiveCriteriaList->insert( make_pair(*((*marker).second.begin()), pair<double,int>( fabs(Value), FragOrder) ));
|
---|
| 79 | std::map<int, pair<double,int> >::iterator PresentItem = InsertedElement.first;
|
---|
| 80 | if (!InsertedElement.second) { // this root is already present
|
---|
| 81 | if ((*PresentItem).second.second < FragOrder) // if order there is lower, update entry with higher-order term
|
---|
| 82 | //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)
|
---|
| 83 | { // if value is smaller, update value and order
|
---|
| 84 | (*PresentItem).second.first = fabs(Value);
|
---|
| 85 | (*PresentItem).second.second = FragOrder;
|
---|
[47d041] | 86 | LOG(2, "Updated element (" << (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "]).");
|
---|
[730d7a] | 87 | } else {
|
---|
[47d041] | 88 | LOG(2, "Did not update element " << (*PresentItem).first << " as " << FragOrder << " is less than or equal to " << (*PresentItem).second.second << ".");
|
---|
[730d7a] | 89 | }
|
---|
| 90 | } else {
|
---|
[47d041] | 91 | LOG(2, "Inserted element (" << (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "]).");
|
---|
[730d7a] | 92 | }
|
---|
| 93 | } else {
|
---|
[47d041] | 94 | LOG(1, "No Fragment under No. " << No << "found.");
|
---|
[730d7a] | 95 | }
|
---|
| 96 | };
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | /** Scans the adaptive order file and insert (index, value) into map.
|
---|
| 100 | * \param &path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
|
---|
| 101 | */
|
---|
[851be8] | 102 | void AdaptivityMap::ScanAdaptiveFileIntoMap(std::string &path)
|
---|
[730d7a] | 103 | {
|
---|
| 104 | int No = 0, FragOrder = 0;
|
---|
| 105 | double Value = 0.;
|
---|
| 106 | char buffer[MAXSTRINGSIZE];
|
---|
| 107 | std::string filename = path + ENERGYPERFRAGMENT;
|
---|
| 108 | std::ifstream InputFile(filename.c_str());
|
---|
| 109 |
|
---|
| 110 | if (InputFile.fail()) {
|
---|
[47d041] | 111 | ELOG(1, "Cannot find file " << filename << ".");
|
---|
[851be8] | 112 | return;
|
---|
[730d7a] | 113 | }
|
---|
| 114 |
|
---|
| 115 | if (CountLinesinFile(InputFile) > 0) {
|
---|
| 116 | // each line represents a fragment root (Atom::Nr) id and its energy contribution
|
---|
| 117 | InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
|
---|
| 118 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 119 | while(!InputFile.eof()) {
|
---|
| 120 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 121 | if (strlen(buffer) > 2) {
|
---|
[47d041] | 122 | //LOG(2, "Scanning: " << buffer);
|
---|
[730d7a] | 123 | stringstream line(buffer);
|
---|
| 124 | line >> FragOrder;
|
---|
| 125 | line >> ws >> No;
|
---|
| 126 | line >> ws >> Value; // skip time entry
|
---|
| 127 | line >> ws >> Value;
|
---|
| 128 | No -= 1; // indices start at 1 in file, not 0
|
---|
[47d041] | 129 | //LOG(2, " - yields (" << No << "," << Value << ", " << FragOrder << ")");
|
---|
[730d7a] | 130 |
|
---|
| 131 | // clean the list of those entries that have been superceded by higher order terms already
|
---|
[851be8] | 132 | InsertIntoAdaptiveCriteriaList(FragOrder, No, Value);
|
---|
[730d7a] | 133 | }
|
---|
| 134 | }
|
---|
| 135 | // close and done
|
---|
| 136 | InputFile.close();
|
---|
| 137 | InputFile.clear();
|
---|
| 138 | }
|
---|
| 139 | };
|
---|
| 140 |
|
---|
| 141 | /** Maps adaptive criteria list back onto (Value, (Root Nr., Order))
|
---|
| 142 | * (i.e. sorted by value to pick the highest ones)
|
---|
| 143 | * \param *mol molecule with atoms
|
---|
| 144 | * \return remapped list
|
---|
| 145 | */
|
---|
[851be8] | 146 | void AdaptivityMap::ReMapAdaptiveCriteriaListToValue(molecule *mol)
|
---|
[730d7a] | 147 | {
|
---|
| 148 | atom *Walker = NULL;
|
---|
[851be8] | 149 | ASSERT( AdaptiveCriteriaList != NULL,
|
---|
| 150 | "AdaptivityMap::ReMapAdaptiveCriteriaListToValue() - AdaptiveCriteriaList is not allocated yet.");
|
---|
| 151 | FinalRootCandidates = new AdaptiveCriteriaValueMap;
|
---|
[47d041] | 152 | LOG(1, "Root candidate list is: ");
|
---|
[851be8] | 153 | for(AdaptiveCriteriaIndexMap::const_iterator runner = AdaptiveCriteriaList->begin(); runner != AdaptiveCriteriaList->end(); runner++) {
|
---|
[730d7a] | 154 | Walker = mol->FindAtom((*runner).first);
|
---|
| 155 | if (Walker != NULL) {
|
---|
| 156 | //if ((*runner).second.second >= Walker->AdaptiveOrder) { // only insert if this is an "active" root site for the current order
|
---|
| 157 | if (!Walker->MaxOrder) {
|
---|
[47d041] | 158 | LOG(2, "(" << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "])");
|
---|
[730d7a] | 159 | FinalRootCandidates->insert( make_pair( (*runner).second.first, pair<int,int>((*runner).first, (*runner).second.second) ) );
|
---|
| 160 | } else {
|
---|
[47d041] | 161 | LOG(2, "Excluding (" << *Walker << ", " << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "]), as it has reached its maximum order.");
|
---|
[730d7a] | 162 | }
|
---|
| 163 | } else {
|
---|
[47d041] | 164 | ELOG(0, "Atom No. " << (*runner).second.first << " was not found in this molecule.");
|
---|
[730d7a] | 165 | performCriticalExit();
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | };
|
---|
| 169 |
|
---|
| 170 | /** Counts lines in file.
|
---|
| 171 | * Note we are scanning lines from current position, not from beginning.
|
---|
| 172 | * \param InputFile file to be scanned.
|
---|
| 173 | */
|
---|
| 174 | int AdaptivityMap::CountLinesinFile(std::ifstream &InputFile) const
|
---|
| 175 | {
|
---|
| 176 | char *buffer = new char[MAXSTRINGSIZE];
|
---|
| 177 | int lines=0;
|
---|
| 178 |
|
---|
| 179 | int PositionMarker = InputFile.tellg(); // not needed as Inputfile is copied, given by value, not by ref
|
---|
| 180 | // count the number of lines, i.e. the number of fragments
|
---|
| 181 | InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
|
---|
| 182 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 183 | while(!InputFile.eof()) {
|
---|
| 184 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 185 | lines++;
|
---|
| 186 | }
|
---|
| 187 | InputFile.seekg(PositionMarker, ios::beg);
|
---|
| 188 | delete[](buffer);
|
---|
| 189 | return lines;
|
---|
| 190 | };
|
---|
[851be8] | 191 |
|
---|
| 192 | /** Marks all candidate sites for update if below adaptive threshold.
|
---|
| 193 | * Picks a given number of highest values and set *AtomMask to true.
|
---|
[f96874] | 194 | * \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
|
---|
[851be8] | 195 | * \param Order desired order
|
---|
| 196 | * \param *mol molecule with atoms
|
---|
| 197 | * \return true - if update is necessary, false - not
|
---|
| 198 | */
|
---|
[f96874] | 199 | bool AdaptivityMap::MarkUpdateCandidates(AtomMask_t &AtomMask, int Order, molecule *mol) const
|
---|
[851be8] | 200 | {
|
---|
| 201 | atom *Walker = NULL;
|
---|
| 202 | int No = -1;
|
---|
| 203 | bool status = false;
|
---|
| 204 | ASSERT( FinalRootCandidates != NULL,
|
---|
| 205 | "AdaptivityMap::MarkUpdateCandidates() - FinalRootCandidates is not allocated yet.");
|
---|
| 206 | for(AdaptiveCriteriaValueMap::const_iterator runner = FinalRootCandidates->upper_bound(pow(10.,Order)); runner != FinalRootCandidates->end(); runner++) {
|
---|
| 207 | No = (*runner).second.first;
|
---|
| 208 | Walker = mol->FindAtom(No);
|
---|
| 209 | //if (Walker->AdaptiveOrder < MinimumRingSize[Walker->getNr()]) {
|
---|
[47d041] | 210 | LOG(2, "Root " << No << " is still above threshold (10^{" << Order <<"}: " << runner->first << ", setting entry " << No << " of Atom mask to true.");
|
---|
[f96874] | 211 | AtomMask.setTrue(No);
|
---|
[851be8] | 212 | status = true;
|
---|
[2a0eb0] | 213 | //} else {
|
---|
| 214 | //AtomMask.setFalse(No);
|
---|
[47d041] | 215 | //LOG(2, "Root " << No << " is still above threshold (10^{" << Order <<"}: " << runner->first << ", however MinimumRingSize of " << MinimumRingSize[Walker->getNr()] << " does not allow further adaptive increase.");
|
---|
[2a0eb0] | 216 | //}
|
---|
[851be8] | 217 | }
|
---|
| 218 | return status;
|
---|
| 219 | };
|
---|
| 220 |
|
---|
| 221 | /** Checks whether there are any adaptive items currently.
|
---|
| 222 | *
|
---|
| 223 | * @return true - there are items for adaptive refinement, false - there are none
|
---|
| 224 | */
|
---|
| 225 | bool AdaptivityMap::IsAdaptiveCriteriaListEmpty() const
|
---|
| 226 | {
|
---|
| 227 | ASSERT( AdaptiveCriteriaList != NULL,
|
---|
| 228 | "AdaptivityMap::ReMapAdaptiveCriteriaListToValue() - AdaptiveCriteriaList is not allocated yet.");
|
---|
| 229 | return AdaptiveCriteriaList->empty();
|
---|
| 230 | }
|
---|