source: src/Potentials/Specifics/SaturationPotential.cpp@ 5a4955

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing 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_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions 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 FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 5a4955 was a82127, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: SaturationPotential takes both angles in each triple.

  • multiplying with 0.5 does the trick.
  • Property mode set to 100644
File size: 17.2 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
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 * SaturationPotential.cpp
26 *
27 * Created on: Oct 11, 2012
28 * Author: heber
29 */
30
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "CodePatterns/MemDebug.hpp"
38
39#include "SaturationPotential.hpp"
40
41#include <boost/assign.hpp>
42#include <boost/assign/list_of.hpp> // for 'map_list_of()'
43#include <iostream>
44#include <string>
45
46#include "CodePatterns/Assert.hpp"
47#include "CodePatterns/Log.hpp"
48
49#include "FunctionApproximation/Extractors.hpp"
50#include "FunctionApproximation/TrainingData.hpp"
51#include "Potentials/helpers.hpp"
52#include "Potentials/ParticleTypeCheckers.hpp"
53
54class Fragment;
55
56using namespace boost::assign;
57
58// static definitions
59const SaturationPotential::ParameterNames_t
60SaturationPotential::ParameterNames =
61 boost::assign::list_of<std::string>
62 ("all_energy_offset")
63 ("")
64 ("")
65 ("")
66 ("")
67 ("")
68 ;
69const std::string SaturationPotential::potential_token("saturation");
70
71SaturationPotential::SaturationPotential(
72 const ParticleTypes_t &_ParticleTypes) :
73 SerializablePotential(_ParticleTypes),
74 morse(_ParticleTypes),
75 angle(symmetrizeTypes(_ParticleTypes)),
76 energy_offset(0.)
77{
78 // have some decent defaults for parameter_derivative checking
79 // Morse and Angle have their own defaults, offset is set
80 ASSERT( _ParticleTypes.size() == (size_t)2,
81 "SaturationPotential::SaturationPotential() - exactly two types must be given.");
82 ASSERT( _ParticleTypes[1] == 1,
83 "SaturationPotential::SaturationPotential() - second type must be hydrogen.");
84}
85
86SaturationPotential::SaturationPotential(
87 const ParticleTypes_t &_ParticleTypes,
88 const double _all_energy_offset,
89 const double _morse_spring_constant,
90 const double _morse_equilibrium_distance,
91 const double _morse_dissociation_energy,
92 const double _angle_spring_constant,
93 const double _angle_equilibrium_distance) :
94 SerializablePotential(_ParticleTypes),
95 morse(_ParticleTypes),
96 angle(symmetrizeTypes(_ParticleTypes)),
97 energy_offset(_all_energy_offset)
98{
99 ASSERT( _ParticleTypes.size() == (size_t)2,
100 "SaturationPotential::SaturationPotential() - exactly two types must be given.");
101 ASSERT( _ParticleTypes[1] == 1,
102 "SaturationPotential::SaturationPotential() - second type must be hydrogen.");
103 parameters_t morse_params(morse.getParameterDimension());
104 morse_params[PairPotential_Morse::spring_constant] = _morse_spring_constant;
105 morse_params[PairPotential_Morse::equilibrium_distance] = _morse_equilibrium_distance;
106 morse_params[PairPotential_Morse::dissociation_energy] = _morse_dissociation_energy;
107 morse_params[PairPotential_Morse::energy_offset] = 0.;
108 morse.setParameters(morse_params);
109 parameters_t angle_params(angle.getParameterDimension());
110 angle_params[PairPotential_Angle::spring_constant] = _angle_spring_constant;
111 angle_params[PairPotential_Angle::equilibrium_distance] = _angle_equilibrium_distance;
112 angle_params[PairPotential_Angle::energy_offset] = 0.;
113 angle.setParameters(angle_params);
114}
115
116void SaturationPotential::setParameters(const parameters_t &_params)
117{
118 const size_t paramsDim = _params.size();
119 ASSERT( paramsDim <= getParameterDimension(),
120 "SaturationPotential::setParameters() - we need not more than "
121 +toString(getParameterDimension())+" parameters.");
122// LOG(1, "INFO: Setting new SaturationPotential params: " << _params);
123
124
125 // offsets
126 if (paramsDim > all_energy_offset)
127 energy_offset = _params[all_energy_offset];
128
129 // Morse
130 {
131 parameters_t morse_params(morse.getParameters());
132 if (paramsDim > morse_spring_constant)
133 morse_params[PairPotential_Morse::spring_constant] = _params[morse_spring_constant];
134 if (paramsDim > morse_equilibrium_distance)
135 morse_params[PairPotential_Morse::equilibrium_distance] = _params[morse_equilibrium_distance];
136 if (paramsDim > morse_dissociation_energy)
137 morse_params[PairPotential_Morse::dissociation_energy] = _params[morse_dissociation_energy];
138 morse_params[PairPotential_Morse::energy_offset] = 0.;
139 morse.setParameters(morse_params);
140 }
141
142 // Angle
143 {
144 parameters_t angle_params(angle.getParameters());
145 if (paramsDim > angle_spring_constant)
146 angle_params[PairPotential_Angle::spring_constant] = _params[angle_spring_constant];
147 if (paramsDim > angle_equilibrium_distance)
148 angle_params[PairPotential_Angle::equilibrium_distance] = _params[angle_equilibrium_distance];
149 angle_params[PairPotential_Angle::energy_offset] = 0.;
150 angle.setParameters(angle_params);
151 }
152#ifndef NDEBUG
153 parameters_t check_params(getParameters());
154 check_params.resize(paramsDim); // truncate to same size
155 ASSERT( check_params == _params,
156 "SaturationPotential::setParameters() - failed, mismatch in to be set "
157 +toString(_params)+" and set "+toString(check_params)+" params.");
158#endif
159}
160
161SaturationPotential::parameters_t SaturationPotential::getParameters() const
162{
163 parameters_t params(getParameterDimension());
164 const parameters_t morse_params = morse.getParameters();
165 const parameters_t angle_params = angle.getParameters();
166
167 params[all_energy_offset] = energy_offset;
168
169 params[morse_spring_constant] = morse_params[PairPotential_Morse::spring_constant];
170 params[morse_equilibrium_distance] = morse_params[PairPotential_Morse::equilibrium_distance];
171 params[morse_dissociation_energy] = morse_params[PairPotential_Morse::dissociation_energy];
172
173 params[angle_spring_constant] = angle_params[PairPotential_Angle::spring_constant];
174 params[angle_equilibrium_distance] = angle_params[PairPotential_Angle::equilibrium_distance];
175 return params;
176}
177
178std::vector<FunctionModel::arguments_t>
179triplefunction(
180 const argument_t &argument,
181 const FunctionModel::arguments_t& args)
182{
183 const size_t firstindex = argument.indices.first;
184 const size_t secondindex = argument.indices.second;
185// LOG(2, "DEBUG: first index is " << firstindex << ", second index is " << secondindex << ".");
186
187 // place all arguments that share either index into a lookup map
188 typedef std::map< size_t, FunctionModel::arguments_t::const_iterator > IndexLookup_t;
189 IndexLookup_t LookuptoFirst;
190 IndexLookup_t LookuptoSecond;
191 for (FunctionModel::arguments_t::const_iterator iter = args.begin();
192 iter != args.end();
193 ++iter) {
194 if (((*iter).indices.first == argument.indices.first)
195 && ((*iter).indices.second == argument.indices.second))
196 continue;
197 if (firstindex == (*iter).indices.first) {
198 LookuptoFirst.insert( std::make_pair( (*iter).indices.second, iter) );
199 }
200 else if (firstindex == (*iter).indices.second) {
201 LookuptoFirst.insert( std::make_pair( (*iter).indices.first, iter) );
202 }
203 if (secondindex == (*iter).indices.first) {
204 LookuptoSecond.insert( std::make_pair( (*iter).indices.second, iter) );
205 }
206 else if (secondindex == (*iter).indices.second) {
207 LookuptoSecond.insert( std::make_pair((*iter).indices.first, iter) );
208 }
209 }
210// {
211// std::stringstream lookupstream;
212// for (IndexLookup_t::const_iterator iter = LookuptoFirst.begin();
213// iter != LookuptoFirst.end();
214// ++iter) {
215// lookupstream << "(" << iter->first << "," << *(iter->second) << ") ";
216// }
217// LOG(2, "DEBUG: LookupToFirst is " << lookupstream.str() << ".");
218// }
219// {
220// std::stringstream lookupstream;
221// for (IndexLookup_t::const_iterator iter = LookuptoSecond.begin();
222// iter != LookuptoSecond.end();
223// ++iter) {
224// lookupstream << "(" << iter->first << "," << *(iter->second) << ") ";
225// }
226// LOG(2, "DEBUG: LookuptoSecond is " << lookupstream.str() << ".");
227// }
228
229 // now go through the first lookup as the second argument and pick the
230 // corresponding third argument by the matching index
231 std::vector<FunctionModel::arguments_t> results;
232 for (IndexLookup_t::const_iterator iter = LookuptoFirst.begin();
233 iter != LookuptoFirst.end();
234 ++iter) {
235 IndexLookup_t::const_iterator otheriter = LookuptoSecond.find(iter->first);
236 ASSERT( otheriter != LookuptoSecond.end(),
237 "triplefunction() - cannot find index "+toString(iter->first)
238 +" in LookupToSecond");
239 FunctionModel::arguments_t result(1, argument);
240 result.reserve(3);
241 result.push_back(*(iter->second));
242 result.push_back(*(otheriter->second));
243 results.push_back(result);
244 }
245
246 return results;
247}
248
249SaturationPotential::results_t
250SaturationPotential::operator()(
251 const arguments_t &arguments
252 ) const
253{
254 double result = 0.;
255 const ParticleTypes_t &morse_types = morse.getParticleTypes();
256 for(arguments_t::const_iterator argiter = arguments.begin();
257 argiter != arguments.end();
258 ++argiter) {
259 const argument_t &r_ij = *argiter;
260 if (((r_ij.types.first == morse_types[0]) && (r_ij.types.second == morse_types[1]))
261 || ((r_ij.types.first == morse_types[1]) && (r_ij.types.second == morse_types[0]))) {
262 arguments_t args(1, r_ij);
263
264 // Morse contribution
265 const double tmp = morse(args)[0];
266// LOG(3, "DEBUG: Morse yields " << tmp << " for << " << r_ij << ".");
267 result += tmp;
268 if (result != result)
269 ELOG(1, "result is NAN.");
270
271 // Angle contribution
272 {
273 typedef std::vector<FunctionModel::arguments_t> tripleargs_t;
274 tripleargs_t tripleargs =
275 triplefunction(r_ij, arguments);
276 for (tripleargs_t::const_iterator iter = tripleargs.begin();
277 iter != tripleargs.end();
278 ++iter) {
279 FunctionModel::arguments_t tempargs =
280 Extractors::reorderArgumentsByParticleTypes(*iter, angle.getParticleTypes());
281 // We get both angles, e.g. 0-4-1 and 1-4-0, hence multiply with 0.5
282 const double tmp = .5*angle(tempargs)[0]; // as we have all distances we get both jk and kj
283// LOG(3, "DEBUG: angle yields " << tmp << " for << " << tempargs << ".");
284 result += tmp;
285 if (result != result)
286 ELOG(1, "result is NAN.");
287 }
288 }
289 }
290 }
291 return std::vector<result_t>(1, energy_offset + result);
292}
293
294SaturationPotential::derivative_components_t
295SaturationPotential::derivative(
296 const arguments_t &arguments
297 ) const
298{
299 ASSERT( 0,
300 "SaturationPotential::operator() - not implemented.");
301 derivative_components_t result;
302 return result;
303}
304
305SaturationPotential::results_t
306SaturationPotential::parameter_derivative(
307 const arguments_t &arguments,
308 const size_t index
309 ) const
310{
311 double result = 0.;
312 if (index == all_energy_offset) {
313 result = 1.;
314 } else {
315 const ParticleTypes_t &morse_types = morse.getParticleTypes();
316 for(arguments_t::const_iterator argiter = arguments.begin();
317 argiter != arguments.end();
318 ++argiter) {
319 const argument_t &r_ij = *argiter;
320 if (((r_ij.types.first == morse_types[0]) && (r_ij.types.second == morse_types[1]))
321 || ((r_ij.types.first == morse_types[1]) && (r_ij.types.second == morse_types[0]))) {
322 arguments_t args(1, r_ij);
323
324 switch (index) {
325 case morse_spring_constant:
326 result += morse.parameter_derivative(args, PairPotential_Morse::spring_constant)[0];
327 break;
328 case morse_equilibrium_distance:
329 result += morse.parameter_derivative(args, PairPotential_Morse::equilibrium_distance)[0];
330 break;
331 case morse_dissociation_energy:
332 result += morse.parameter_derivative(args, PairPotential_Morse::dissociation_energy)[0];
333 break;
334 case angle_spring_constant:
335 case angle_equilibrium_distance:
336 {
337 typedef std::vector<FunctionModel::arguments_t> tripleargs_t;
338 tripleargs_t tripleargs =
339 triplefunction(r_ij, arguments);
340 for (tripleargs_t::const_iterator iter = tripleargs.begin();
341 iter != tripleargs.end();
342 ++iter) {
343 FunctionModel::arguments_t tempargs =
344 Extractors::reorderArgumentsByParticleTypes(*iter, angle.getParticleTypes());
345 // We get both angles, e.g. 0-4-1 and 1-4-0, hence multiply with 0.5
346 if (index == angle_spring_constant)
347 result += .5*angle.parameter_derivative(tempargs, PairPotential_Angle::spring_constant)[0];
348 else if (index == angle_equilibrium_distance)
349 result += .5*angle.parameter_derivative(tempargs, PairPotential_Angle::equilibrium_distance)[0];
350// LOG(2, "DEBUG: angle yields " << tmp << " for << " << arguments << ".");
351// result += tmp;
352 if (result != result)
353 ELOG(1, "result is NAN.");
354 }
355 }
356 break;
357 default:
358 ASSERT( 0, "SaturationPotential::parameter_derivative() - impossible to get here.");
359 break;
360 }
361 }
362 }
363 }
364 return SaturationPotential::results_t(1, result);
365}
366
367const SaturationPotential::ParticleTypes_t
368SaturationPotential::symmetrizeTypes(const ParticleTypes_t &_ParticleTypes)
369{
370 ASSERT( _ParticleTypes.size() == (size_t)2,
371 "SaturationPotential::symmetrizeTypes() - require initial _ParticleTypes with two elements.");
372// // insert before couple
373// ParticleTypes_t types(1, _ParticleTypes[1]);
374// types.insert(types.end(), _ParticleTypes.begin(), _ParticleTypes.end());
375 // insert after the couple
376 ParticleTypes_t types(_ParticleTypes);
377 types.push_back( _ParticleTypes.back() );
378 ASSERT( types.size() == (size_t)3,
379 "SaturationPotential::symmetrizeTypes() - failed to generate three types for angle.");
380 return types;
381}
382
383std::ostream& operator<<(std::ostream &ost, const SaturationPotential &potential)
384{
385 ost << potential.morse;
386 ost << potential.angle;
387 return ost;
388}
389
390std::istream& operator>>(std::istream &ist, SaturationPotential &potential)
391{
392 ist >> potential.morse;
393 ist >> potential.angle;
394 return ist;
395}
396
397FunctionModel::extractor_t
398SaturationPotential::getFragmentSpecificExtractor(
399 const charges_t &charges) const
400{
401 ASSERT(charges.size() == (size_t)2,
402 "SaturationPotential::getFragmentSpecificExtractor() - requires 2 charges.");
403 FunctionModel::extractor_t returnfunction;
404 if (charges[0] == charges[1]) {
405 // In case both types are equal there is only a single pair of possible
406 // type combinations.
407 returnfunction =
408 boost::bind(&Extractors::gatherAllDistancesFromFragment,
409 boost::bind(&Fragment::getPositions, _1),
410 boost::bind(&Fragment::getCharges, _1),
411 boost::cref(charges),
412 _2);
413 } else {
414 // we have to chain here a rather complex "tree" of functions
415 // as we only have a couple of ParticleTypes but need to get
416 // all possible three pairs of the set of the two types.
417 // Finally, we also need to arrange them in correct order
418 // (for PairPotentiale_Angle).
419 //charges_t firstpair(2, boost::cref(charges[0]));
420 // only that saturation potential never has its middle element twice!
421 // hence, we skip the firstpair but keep the code for later generalization
422 charges_t secondpair(2, boost::cref(charges[1]));
423 const charges_t &thirdpair = charges;
424 returnfunction =
425// boost::bind(&Extractors::reorderArgumentsByParticleTypes,
426 boost::bind(&Extractors::combineArguments,
427// boost::bind(&Extractors::combineArguments,
428// boost::bind(&Extractors::gatherAllDistancesFromFragment,
429// boost::bind(&Fragment::getPositions, _1),
430// boost::bind(&Fragment::getCharges, _1),
431// firstpair, // no crefs here as are temporaries!
432// _2),
433 boost::bind(&Extractors::gatherAllDistancesFromFragment,
434 boost::bind(&Fragment::getPositions, _1),
435 boost::bind(&Fragment::getCharges, _1),
436 secondpair, // no crefs here as are temporaries!
437 _2),
438// ),
439 boost::bind(&Extractors::gatherAllDistancesFromFragment,
440 boost::bind(&Fragment::getPositions, _1),
441 boost::bind(&Fragment::getCharges, _1),
442 boost::cref(thirdpair), // only the last one is no temporary
443 _2)
444 );
445// boost::cref(angle.getParticleTypes())
446// );
447}
448 return returnfunction;
449}
450
451void
452SaturationPotential::setParametersToRandomInitialValues(
453 const TrainingData &data)
454{
455 energy_offset = data.getTrainingOutputAverage()[0];
456 morse.setParametersToRandomInitialValues(data);
457 angle.setParametersToRandomInitialValues(data);
458}
Note: See TracBrowser for help on using the repository browser.