| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 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 |  * 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 | 
 | 
|---|
| 40 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "Atom/atom.hpp"
 | 
|---|
| 44 | #include "Fragmentation/AtomMask.hpp"
 | 
|---|
| 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 |  */
 | 
|---|
| 70 | void AdaptivityMap::InsertIntoAdaptiveCriteriaList(int FragOrder, int No, double Value)
 | 
|---|
| 71 | {
 | 
|---|
| 72 |   ASSERT( AdaptiveCriteriaList != NULL,
 | 
|---|
| 73 |       "AdaptivityMap::InsertIntoAdaptiveCriteriaList() - AdaptiveCriteriaList is not allocated yet.");
 | 
|---|
| 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;
 | 
|---|
| 86 |         LOG(2, "Updated element (" <<  (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "]).");
 | 
|---|
| 87 |       } else {
 | 
|---|
| 88 |         LOG(2, "Did not update element " <<  (*PresentItem).first << " as " << FragOrder << " is less than or equal to " << (*PresentItem).second.second << ".");
 | 
|---|
| 89 |       }
 | 
|---|
| 90 |     } else {
 | 
|---|
| 91 |       LOG(2, "Inserted element (" <<  (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "]).");
 | 
|---|
| 92 |     }
 | 
|---|
| 93 |   } else {
 | 
|---|
| 94 |     LOG(1, "No Fragment under No. " << No << "found.");
 | 
|---|
| 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 |  */
 | 
|---|
| 102 | void AdaptivityMap::ScanAdaptiveFileIntoMap(const std::string &path)
 | 
|---|
| 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()) {
 | 
|---|
| 111 |     ELOG(1, "Cannot find file " << filename << ".");
 | 
|---|
| 112 |     return;
 | 
|---|
| 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) {
 | 
|---|
| 122 |         //LOG(2, "Scanning: " << buffer);
 | 
|---|
| 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
 | 
|---|
| 129 |         //LOG(2, " - yields (" << No << "," << Value << ", " << FragOrder << ")");
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         // clean the list of those entries that have been superceded by higher order terms already
 | 
|---|
| 132 |         InsertIntoAdaptiveCriteriaList(FragOrder, No, Value);
 | 
|---|
| 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 |  */
 | 
|---|
| 146 | void AdaptivityMap::ReMapAdaptiveCriteriaListToValue(molecule *mol)
 | 
|---|
| 147 | {
 | 
|---|
| 148 |   atom *Walker = NULL;
 | 
|---|
| 149 |   ASSERT( AdaptiveCriteriaList != NULL,
 | 
|---|
| 150 |       "AdaptivityMap::ReMapAdaptiveCriteriaListToValue() - AdaptiveCriteriaList is not allocated yet.");
 | 
|---|
| 151 |   FinalRootCandidates = new AdaptiveCriteriaValueMap;
 | 
|---|
| 152 |   LOG(1, "Root candidate list is: ");
 | 
|---|
| 153 |   for(AdaptiveCriteriaIndexMap::const_iterator runner = AdaptiveCriteriaList->begin(); runner != AdaptiveCriteriaList->end(); runner++) {
 | 
|---|
| 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 > Walker->AdaptiveOrder) {
 | 
|---|
| 158 |         LOG(2, "(" << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "])");
 | 
|---|
| 159 |         FinalRootCandidates->insert( make_pair( (*runner).second.first, pair<int,int>((*runner).first, (*runner).second.second) ) );
 | 
|---|
| 160 |       } else {
 | 
|---|
| 161 |         LOG(2, "Excluding (" << *Walker << ", " << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "]), as it has reached its maximum order.");
 | 
|---|
| 162 |       }
 | 
|---|
| 163 |     } else {
 | 
|---|
| 164 |       ELOG(0, "Atom No. " << (*runner).second.first << " was not found in this molecule.");
 | 
|---|
| 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 | };
 | 
|---|
| 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.
 | 
|---|
| 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
 | 
|---|
| 195 |  * \param Order desired order
 | 
|---|
| 196 |  * \param *mol molecule with atoms
 | 
|---|
| 197 |  * \return true - if update is necessary, false - not
 | 
|---|
| 198 |  */
 | 
|---|
| 199 | bool AdaptivityMap::MarkUpdateCandidates(AtomMask_t &AtomMask, int Order, molecule *mol) const
 | 
|---|
| 200 | {
 | 
|---|
| 201 |   ASSERT( FinalRootCandidates != NULL,
 | 
|---|
| 202 |       "AdaptivityMap::MarkUpdateCandidates() - FinalRootCandidates is not allocated yet.");
 | 
|---|
| 203 |   const AdaptiveCriteriaValueMap::const_iterator enditer = FinalRootCandidates->upper_bound(pow(10.,Order));
 | 
|---|
| 204 |   for(AdaptiveCriteriaValueMap::const_iterator runner = FinalRootCandidates->begin();
 | 
|---|
| 205 |       runner != enditer; ++runner) {
 | 
|---|
| 206 |     const int No = (*runner).second.first;
 | 
|---|
| 207 | //    const atom *Walker = mol->FindAtom(No);
 | 
|---|
| 208 |     LOG(2, "Root " << No << " is already below threshold (10^{" << Order <<"}: " << runner->first << ", setting entry " << No << " of Atom mask to false.");
 | 
|---|
| 209 |     AtomMask.setFalse(No);
 | 
|---|
| 210 |   }
 | 
|---|
| 211 |   return enditer != FinalRootCandidates->end();
 | 
|---|
| 212 | };
 | 
|---|
| 213 | 
 | 
|---|
| 214 | /** Checks whether there are any adaptive items currently.
 | 
|---|
| 215 |  *
 | 
|---|
| 216 |  * @return true - there are items for adaptive refinement, false - there are none
 | 
|---|
| 217 |  */
 | 
|---|
| 218 | bool AdaptivityMap::IsAdaptiveCriteriaListEmpty() const
 | 
|---|
| 219 | {
 | 
|---|
| 220 |   ASSERT( AdaptiveCriteriaList != NULL,
 | 
|---|
| 221 |       "AdaptivityMap::ReMapAdaptiveCriteriaListToValue() - AdaptiveCriteriaList is not allocated yet.");
 | 
|---|
| 222 |   return AdaptiveCriteriaList->empty();
 | 
|---|
| 223 | }
 | 
|---|