source: src/Actions/PotentialAction/FitPotentialAction.cpp@ ce70d25

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator 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_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions 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 GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since ce70d25 was fde8e7, checked in by Frederik Heber <heber@…>, 9 years ago

Refactored PotentialSerializer and -Deserializer into more general templated classes.

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