| [48d20d] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2013 University of Bonn. All rights reserved. | 
|---|
| [5aaa43] | 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| [48d20d] | 6 | * | 
|---|
|  | 7 | * | 
|---|
|  | 8 | *   This file is part of MoleCuilder. | 
|---|
|  | 9 | * | 
|---|
|  | 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
|  | 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
|  | 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
|  | 13 | *    (at your option) any later version. | 
|---|
|  | 14 | * | 
|---|
|  | 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
|  | 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 18 | *    GNU General Public License for more details. | 
|---|
|  | 19 | * | 
|---|
|  | 20 | *    You should have received a copy of the GNU General Public License | 
|---|
|  | 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
|  | 22 | */ | 
|---|
|  | 23 |  | 
|---|
|  | 24 | /* | 
|---|
|  | 25 | * FitPotentialAction.cpp | 
|---|
|  | 26 | * | 
|---|
|  | 27 | *  Created on: Apr 09, 2013 | 
|---|
|  | 28 | *      Author: heber | 
|---|
|  | 29 | */ | 
|---|
|  | 30 |  | 
|---|
|  | 31 | // include config.h | 
|---|
|  | 32 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 33 | #include <config.h> | 
|---|
|  | 34 | #endif | 
|---|
|  | 35 |  | 
|---|
|  | 36 | // needs to come before MemDebug due to placement new | 
|---|
|  | 37 | #include <boost/archive/text_iarchive.hpp> | 
|---|
|  | 38 |  | 
|---|
| [9eb71b3] | 39 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| [48d20d] | 40 |  | 
|---|
|  | 41 | #include <algorithm> | 
|---|
|  | 42 | #include <boost/bind.hpp> | 
|---|
|  | 43 | #include <boost/filesystem.hpp> | 
|---|
|  | 44 | #include <boost/foreach.hpp> | 
|---|
|  | 45 | #include <map> | 
|---|
|  | 46 | #include <string> | 
|---|
|  | 47 | #include <sstream> | 
|---|
|  | 48 |  | 
|---|
| [f5724f] | 49 | #include "Actions/PotentialAction/FitPotentialAction.hpp" | 
|---|
| [48d20d] | 50 |  | 
|---|
|  | 51 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 52 |  | 
|---|
|  | 53 | #include "Element/element.hpp" | 
|---|
|  | 54 | #include "Fragmentation/Homology/HomologyContainer.hpp" | 
|---|
|  | 55 | #include "Fragmentation/Homology/HomologyGraph.hpp" | 
|---|
|  | 56 | #include "Fragmentation/Summation/SetValues/Fragment.hpp" | 
|---|
| [98d166] | 57 | #include "Potentials/EmpiricalPotential.hpp" | 
|---|
| [48d20d] | 58 | #include "Potentials/PotentialFactory.hpp" | 
|---|
| [baccf6] | 59 | #include "Potentials/PotentialRegistry.hpp" | 
|---|
| [98d166] | 60 | #include "Potentials/PotentialTrainer.hpp" | 
|---|
| [48d20d] | 61 | #include "Potentials/SerializablePotential.hpp" | 
|---|
| [98d166] | 62 | #include "World.hpp" | 
|---|
| [48d20d] | 63 |  | 
|---|
|  | 64 | using namespace MoleCuilder; | 
|---|
|  | 65 |  | 
|---|
|  | 66 | // and construct the stuff | 
|---|
|  | 67 | #include "FitPotentialAction.def" | 
|---|
|  | 68 | #include "Action_impl_pre.hpp" | 
|---|
|  | 69 | /** =========== define the function ====================== */ | 
|---|
|  | 70 |  | 
|---|
| [95304c] | 71 | static void registerPotentialIfNotPresent( | 
|---|
|  | 72 | const std::string &_potentialtype, | 
|---|
|  | 73 | const SerializablePotential::ParticleTypes_t &_types) | 
|---|
|  | 74 | { | 
|---|
|  | 75 | EmpiricalPotential *potential = | 
|---|
|  | 76 | PotentialFactory::getInstance().createInstance(_potentialtype,_types); | 
|---|
|  | 77 | // check whether such a potential already exists | 
|---|
|  | 78 | const std::string potential_name = potential->getName(); | 
|---|
|  | 79 | if (PotentialRegistry::getInstance().isPresentByName(potential_name)) { | 
|---|
|  | 80 | delete potential; | 
|---|
|  | 81 | potential = PotentialRegistry::getInstance().getByName(potential_name); | 
|---|
|  | 82 | } else | 
|---|
|  | 83 | PotentialRegistry::getInstance().registerInstance(potential); | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
|  | 86 |  | 
|---|
| [b5b01e] | 87 | ActionState::ptr PotentialFitPotentialAction::performCall() { | 
|---|
| [48d20d] | 88 | // fragment specifies the homology fragment to use | 
|---|
| [4e009d] | 89 | SerializablePotential::ParticleTypes_t fragmentnumbers = | 
|---|
| [98d166] | 90 | PotentialTrainer::getNumbersFromElements(params.fragment.get()); | 
|---|
| [48d20d] | 91 |  | 
|---|
| [baccf6] | 92 | // either charges and a potential is specified or a file | 
|---|
| [98d166] | 93 | if (params.charges.get().empty()) { | 
|---|
|  | 94 | STATUS("No charges given!"); | 
|---|
|  | 95 | return Action::failure; | 
|---|
| [baccf6] | 96 | } else { | 
|---|
| [98d166] | 97 | // charges specify the potential type | 
|---|
|  | 98 | SerializablePotential::ParticleTypes_t chargenumbers = | 
|---|
|  | 99 | PotentialTrainer::getNumbersFromElements(params.charges.get()); | 
|---|
|  | 100 |  | 
|---|
|  | 101 | LOG(0, "STATUS: I'm training now a " << params.potentialtype.get() | 
|---|
|  | 102 | << " potential on charges " << chargenumbers << " on data from World's homologies."); | 
|---|
|  | 103 |  | 
|---|
|  | 104 | // register desired potential and an additional constant one | 
|---|
| [95304c] | 105 | registerPotentialIfNotPresent(params.potentialtype.get(),chargenumbers); | 
|---|
|  | 106 | registerPotentialIfNotPresent( | 
|---|
|  | 107 | std::string("constant"), | 
|---|
|  | 108 | SerializablePotential::ParticleTypes_t()); | 
|---|
| [baccf6] | 109 | } | 
|---|
|  | 110 |  | 
|---|
| [48d20d] | 111 | // parse homologies into container | 
|---|
| [98d166] | 112 | const HomologyContainer &homologies = World::getInstance().getHomologies(); | 
|---|
| [48d20d] | 113 |  | 
|---|
| [baccf6] | 114 | // then we ought to pick the right HomologyGraph ... | 
|---|
| [98d166] | 115 | const HomologyGraph graph = | 
|---|
|  | 116 | PotentialTrainer::getFirstGraphwithSpecifiedElements(homologies,fragmentnumbers); | 
|---|
| [baccf6] | 117 | if (graph != HomologyGraph()) { | 
|---|
|  | 118 | LOG(1, "First representative graph containing fragment " | 
|---|
|  | 119 | << fragmentnumbers << " is " << graph << "."); | 
|---|
|  | 120 | } else { | 
|---|
| [26b4d62] | 121 | STATUS("Specific fragment "+toString(fragmentnumbers)+" not found in homologies!"); | 
|---|
| [baccf6] | 122 | return Action::failure; | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
| [27e6a7] | 125 | // for debugging we list all matching fragments | 
|---|
|  | 126 | HomologyContainer::range_t graphrange = homologies.getHomologousGraphs(graph); | 
|---|
|  | 127 | LOG(1, "INFO: Listing all matching homologous graphs ..."); | 
|---|
|  | 128 | for (HomologyContainer::container_t::const_iterator iter = | 
|---|
|  | 129 | graphrange.first; iter != graphrange.second; ++iter) { | 
|---|
|  | 130 | LOG(1, "INFO: graph " << iter->first | 
|---|
|  | 131 | << " has Fragment " << iter->second.fragment | 
|---|
| [564f17] | 132 | << ", associated energy " << iter->second.contribution | 
|---|
| [27e6a7] | 133 | << ", and sampled grid integral " << iter->second.charge_distribution.integral() | 
|---|
|  | 134 | << "."); | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [98d166] | 137 | // training | 
|---|
|  | 138 | PotentialTrainer trainer; | 
|---|
|  | 139 | const bool status = trainer( | 
|---|
|  | 140 | homologies, | 
|---|
|  | 141 | graph, | 
|---|
|  | 142 | params.training_file.get(), | 
|---|
| [82e5fb] | 143 | params.error_file.get(), | 
|---|
| [b40690] | 144 | params.max_iterations.get(), | 
|---|
| [98d166] | 145 | params.threshold.get(), | 
|---|
|  | 146 | params.best_of_howmany.get()); | 
|---|
|  | 147 | if (!status) { | 
|---|
|  | 148 | STATUS("No required parameter derivatives for a box constraint minimization known."); | 
|---|
|  | 149 | return Action::failure; | 
|---|
|  | 150 | } | 
|---|
| [baccf6] | 151 |  | 
|---|
| [48d20d] | 152 | return Action::success; | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
| [b5b01e] | 155 | ActionState::ptr PotentialFitPotentialAction::performUndo(ActionState::ptr _state) { | 
|---|
| [48d20d] | 156 | return Action::success; | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
| [b5b01e] | 159 | ActionState::ptr PotentialFitPotentialAction::performRedo(ActionState::ptr _state){ | 
|---|
| [48d20d] | 160 | return Action::success; | 
|---|
|  | 161 | } | 
|---|
|  | 162 |  | 
|---|
| [f5724f] | 163 | bool PotentialFitPotentialAction::canUndo() { | 
|---|
| [48d20d] | 164 | return false; | 
|---|
|  | 165 | } | 
|---|
|  | 166 |  | 
|---|
| [f5724f] | 167 | bool PotentialFitPotentialAction::shouldUndo() { | 
|---|
| [48d20d] | 168 | return false; | 
|---|
|  | 169 | } | 
|---|
|  | 170 | /** =========== end of function ====================== */ | 
|---|