| [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 |  | 
|---|
|  | 39 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 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" | 
|---|
|  | 60 | #include "Potentials/PotentialSerializer.hpp" | 
|---|
| [98d166] | 61 | #include "Potentials/PotentialTrainer.hpp" | 
|---|
| [48d20d] | 62 | #include "Potentials/SerializablePotential.hpp" | 
|---|
| [98d166] | 63 | #include "World.hpp" | 
|---|
| [48d20d] | 64 |  | 
|---|
|  | 65 | using namespace MoleCuilder; | 
|---|
|  | 66 |  | 
|---|
|  | 67 | // and construct the stuff | 
|---|
|  | 68 | #include "FitPotentialAction.def" | 
|---|
|  | 69 | #include "Action_impl_pre.hpp" | 
|---|
|  | 70 | /** =========== define the function ====================== */ | 
|---|
|  | 71 |  | 
|---|
| [b5b01e] | 72 | ActionState::ptr PotentialFitPotentialAction::performCall() { | 
|---|
| [48d20d] | 73 | // fragment specifies the homology fragment to use | 
|---|
| [4e009d] | 74 | SerializablePotential::ParticleTypes_t fragmentnumbers = | 
|---|
| [98d166] | 75 | PotentialTrainer::getNumbersFromElements(params.fragment.get()); | 
|---|
| [48d20d] | 76 |  | 
|---|
| [baccf6] | 77 | // either charges and a potential is specified or a file | 
|---|
| [98d166] | 78 | if (params.charges.get().empty()) { | 
|---|
|  | 79 | STATUS("No charges given!"); | 
|---|
|  | 80 | return Action::failure; | 
|---|
| [baccf6] | 81 | } else { | 
|---|
| [98d166] | 82 | // charges specify the potential type | 
|---|
|  | 83 | SerializablePotential::ParticleTypes_t chargenumbers = | 
|---|
|  | 84 | PotentialTrainer::getNumbersFromElements(params.charges.get()); | 
|---|
|  | 85 |  | 
|---|
|  | 86 | LOG(0, "STATUS: I'm training now a " << params.potentialtype.get() | 
|---|
|  | 87 | << " potential on charges " << chargenumbers << " on data from World's homologies."); | 
|---|
|  | 88 |  | 
|---|
|  | 89 | // register desired potential and an additional constant one | 
|---|
|  | 90 | { | 
|---|
|  | 91 | EmpiricalPotential *potential = | 
|---|
|  | 92 | PotentialFactory::getInstance().createInstance( | 
|---|
|  | 93 | params.potentialtype.get(), | 
|---|
|  | 94 | chargenumbers); | 
|---|
|  | 95 | // check whether such a potential already exists | 
|---|
|  | 96 | const std::string potential_name = potential->getName(); | 
|---|
|  | 97 | if (PotentialRegistry::getInstance().isPresentByName(potential_name)) { | 
|---|
|  | 98 | delete potential; | 
|---|
|  | 99 | potential = PotentialRegistry::getInstance().getByName(potential_name); | 
|---|
|  | 100 | } else | 
|---|
|  | 101 | PotentialRegistry::getInstance().registerInstance(potential); | 
|---|
|  | 102 | } | 
|---|
|  | 103 | { | 
|---|
|  | 104 | EmpiricalPotential *constant = | 
|---|
|  | 105 | PotentialFactory::getInstance().createInstance( | 
|---|
|  | 106 | std::string("constant"), | 
|---|
|  | 107 | SerializablePotential::ParticleTypes_t()); | 
|---|
|  | 108 | // check whether such a potential already exists | 
|---|
|  | 109 | const std::string constant_name = constant->getName(); | 
|---|
|  | 110 | if (PotentialRegistry::getInstance().isPresentByName(constant_name)) { | 
|---|
|  | 111 | delete constant; | 
|---|
|  | 112 | constant = PotentialRegistry::getInstance().getByName(constant_name); | 
|---|
|  | 113 | } else | 
|---|
|  | 114 | PotentialRegistry::getInstance().registerInstance(constant); | 
|---|
| [baccf6] | 115 | } | 
|---|
|  | 116 | } | 
|---|
|  | 117 |  | 
|---|
| [48d20d] | 118 | // parse homologies into container | 
|---|
| [98d166] | 119 | const HomologyContainer &homologies = World::getInstance().getHomologies(); | 
|---|
| [48d20d] | 120 |  | 
|---|
|  | 121 | // first we try to look into the HomologyContainer | 
|---|
|  | 122 | LOG(1, "INFO: Listing all present homologies ..."); | 
|---|
|  | 123 | for (HomologyContainer::container_t::const_iterator iter = | 
|---|
|  | 124 | homologies.begin(); iter != homologies.end(); ++iter) { | 
|---|
| [bf1d1b] | 125 | LOG(1, "INFO: graph " << iter->first | 
|---|
|  | 126 | << " has Fragment " << iter->second.fragment | 
|---|
|  | 127 | << ", associated energy " << iter->second.energy | 
|---|
|  | 128 | << ", and sampled grid integral " << iter->second.charge_distribution.integral() | 
|---|
|  | 129 | << "."); | 
|---|
| [48d20d] | 130 | } | 
|---|
|  | 131 |  | 
|---|
| [baccf6] | 132 | // then we ought to pick the right HomologyGraph ... | 
|---|
| [98d166] | 133 | const HomologyGraph graph = | 
|---|
|  | 134 | PotentialTrainer::getFirstGraphwithSpecifiedElements(homologies,fragmentnumbers); | 
|---|
| [baccf6] | 135 | if (graph != HomologyGraph()) { | 
|---|
|  | 136 | LOG(1, "First representative graph containing fragment " | 
|---|
|  | 137 | << fragmentnumbers << " is " << graph << "."); | 
|---|
|  | 138 | } else { | 
|---|
| [26b4d62] | 139 | STATUS("Specific fragment "+toString(fragmentnumbers)+" not found in homologies!"); | 
|---|
| [baccf6] | 140 | return Action::failure; | 
|---|
|  | 141 | } | 
|---|
|  | 142 |  | 
|---|
| [98d166] | 143 | // training | 
|---|
|  | 144 | PotentialTrainer trainer; | 
|---|
|  | 145 | const bool status = trainer( | 
|---|
|  | 146 | homologies, | 
|---|
|  | 147 | graph, | 
|---|
|  | 148 | params.training_file.get(), | 
|---|
| [b40690] | 149 | params.max_iterations.get(), | 
|---|
| [98d166] | 150 | params.threshold.get(), | 
|---|
|  | 151 | params.best_of_howmany.get()); | 
|---|
|  | 152 | if (!status) { | 
|---|
|  | 153 | STATUS("No required parameter derivatives for a box constraint minimization known."); | 
|---|
|  | 154 | return Action::failure; | 
|---|
|  | 155 | } | 
|---|
| [baccf6] | 156 |  | 
|---|
| [48d20d] | 157 | return Action::success; | 
|---|
|  | 158 | } | 
|---|
|  | 159 |  | 
|---|
| [b5b01e] | 160 | ActionState::ptr PotentialFitPotentialAction::performUndo(ActionState::ptr _state) { | 
|---|
| [48d20d] | 161 | return Action::success; | 
|---|
|  | 162 | } | 
|---|
|  | 163 |  | 
|---|
| [b5b01e] | 164 | ActionState::ptr PotentialFitPotentialAction::performRedo(ActionState::ptr _state){ | 
|---|
| [48d20d] | 165 | return Action::success; | 
|---|
|  | 166 | } | 
|---|
|  | 167 |  | 
|---|
| [f5724f] | 168 | bool PotentialFitPotentialAction::canUndo() { | 
|---|
| [48d20d] | 169 | return false; | 
|---|
|  | 170 | } | 
|---|
|  | 171 |  | 
|---|
| [f5724f] | 172 | bool PotentialFitPotentialAction::shouldUndo() { | 
|---|
| [48d20d] | 173 | return false; | 
|---|
|  | 174 | } | 
|---|
|  | 175 | /** =========== end of function ====================== */ | 
|---|