source: src/Potentials/Specifics/SaturationPotential.cpp@ 3d2559

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 3d2559 was 3d2559, checked in by Frederik Heber <heber@…>, 12 years ago

Added virtual stream_to/from functions to SerializablePotential.

  • this allows overriding in SaturationPotential and hence printing/parsing of both contained types.
  • Property mode set to 100644
File size: 17.1 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
178void SaturationPotential::stream_to(std::ostream &ost) const
179{
180 morse.stream_to(ost);
181 ost << std::endl;
182 angle.stream_to(ost);
183}
184
185void SaturationPotential::stream_from(std::istream &ist)
186{
187 morse.stream_from(ist);
188 ist >> ws;
189 angle.stream_from(ist);
190}
191
192std::vector<FunctionModel::arguments_t>
193triplefunction(
194 const argument_t &argument,
195 const FunctionModel::arguments_t& args)
196{
197 const size_t firstindex = argument.indices.first;
198 const size_t secondindex = argument.indices.second;
199// LOG(2, "DEBUG: first index is " << firstindex << ", second index is " << secondindex << ".");
200
201 // place all arguments that share either index into a lookup map
202 typedef std::map< size_t, FunctionModel::arguments_t::const_iterator > IndexLookup_t;
203 IndexLookup_t LookuptoFirst;
204 IndexLookup_t LookuptoSecond;
205 for (FunctionModel::arguments_t::const_iterator iter = args.begin();
206 iter != args.end();
207 ++iter) {
208 if (((*iter).indices.first == argument.indices.first)
209 && ((*iter).indices.second == argument.indices.second))
210 continue;
211 if (firstindex == (*iter).indices.first) {
212 LookuptoFirst.insert( std::make_pair( (*iter).indices.second, iter) );
213 }
214 else if (firstindex == (*iter).indices.second) {
215 LookuptoFirst.insert( std::make_pair( (*iter).indices.first, iter) );
216 }
217 if (secondindex == (*iter).indices.first) {
218 LookuptoSecond.insert( std::make_pair( (*iter).indices.second, iter) );
219 }
220 else if (secondindex == (*iter).indices.second) {
221 LookuptoSecond.insert( std::make_pair((*iter).indices.first, iter) );
222 }
223 }
224// {
225// std::stringstream lookupstream;
226// for (IndexLookup_t::const_iterator iter = LookuptoFirst.begin();
227// iter != LookuptoFirst.end();
228// ++iter) {
229// lookupstream << "(" << iter->first << "," << *(iter->second) << ") ";
230// }
231// LOG(2, "DEBUG: LookupToFirst is " << lookupstream.str() << ".");
232// }
233// {
234// std::stringstream lookupstream;
235// for (IndexLookup_t::const_iterator iter = LookuptoSecond.begin();
236// iter != LookuptoSecond.end();
237// ++iter) {
238// lookupstream << "(" << iter->first << "," << *(iter->second) << ") ";
239// }
240// LOG(2, "DEBUG: LookuptoSecond is " << lookupstream.str() << ".");
241// }
242
243 // now go through the first lookup as the second argument and pick the
244 // corresponding third argument by the matching index
245 std::vector<FunctionModel::arguments_t> results;
246 for (IndexLookup_t::const_iterator iter = LookuptoFirst.begin();
247 iter != LookuptoFirst.end();
248 ++iter) {
249 IndexLookup_t::const_iterator otheriter = LookuptoSecond.find(iter->first);
250 ASSERT( otheriter != LookuptoSecond.end(),
251 "triplefunction() - cannot find index "+toString(iter->first)
252 +" in LookupToSecond");
253 FunctionModel::arguments_t result(1, argument);
254 result.reserve(3);
255 result.push_back(*(iter->second));
256 result.push_back(*(otheriter->second));
257 results.push_back(result);
258 }
259
260 return results;
261}
262
263SaturationPotential::results_t
264SaturationPotential::operator()(
265 const arguments_t &arguments
266 ) const
267{
268 double result = 0.;
269 const ParticleTypes_t &morse_types = morse.getParticleTypes();
270 for(arguments_t::const_iterator argiter = arguments.begin();
271 argiter != arguments.end();
272 ++argiter) {
273 const argument_t &r_ij = *argiter;
274 if (((r_ij.types.first == morse_types[0]) && (r_ij.types.second == morse_types[1]))
275 || ((r_ij.types.first == morse_types[1]) && (r_ij.types.second == morse_types[0]))) {
276 arguments_t args(1, r_ij);
277
278 // Morse contribution
279 const double tmp = morse(args)[0];
280// LOG(3, "DEBUG: Morse yields " << tmp << " for << " << r_ij << ".");
281 result += tmp;
282 if (result != result)
283 ELOG(1, "result is NAN.");
284
285 // Angle contribution
286 {
287 typedef std::vector<FunctionModel::arguments_t> tripleargs_t;
288 tripleargs_t tripleargs =
289 triplefunction(r_ij, arguments);
290 for (tripleargs_t::const_iterator iter = tripleargs.begin();
291 iter != tripleargs.end();
292 ++iter) {
293 FunctionModel::arguments_t tempargs =
294 Extractors::reorderArgumentsByParticleTypes(*iter, angle.getParticleTypes());
295 // We get both angles, e.g. 0-4-1 and 1-4-0, hence multiply with 0.5
296 const double tmp = .5*angle(tempargs)[0]; // as we have all distances we get both jk and kj
297// LOG(3, "DEBUG: angle yields " << tmp << " for << " << tempargs << ".");
298 result += tmp;
299 if (result != result)
300 ELOG(1, "result is NAN.");
301 }
302 }
303 }
304 }
305 return std::vector<result_t>(1, energy_offset + result);
306}
307
308SaturationPotential::derivative_components_t
309SaturationPotential::derivative(
310 const arguments_t &arguments
311 ) const
312{
313 ASSERT( 0,
314 "SaturationPotential::operator() - not implemented.");
315 derivative_components_t result;
316 return result;
317}
318
319SaturationPotential::results_t
320SaturationPotential::parameter_derivative(
321 const arguments_t &arguments,
322 const size_t index
323 ) const
324{
325 double result = 0.;
326 if (index == all_energy_offset) {
327 result = 1.;
328 } else {
329 const ParticleTypes_t &morse_types = morse.getParticleTypes();
330 for(arguments_t::const_iterator argiter = arguments.begin();
331 argiter != arguments.end();
332 ++argiter) {
333 const argument_t &r_ij = *argiter;
334 if (((r_ij.types.first == morse_types[0]) && (r_ij.types.second == morse_types[1]))
335 || ((r_ij.types.first == morse_types[1]) && (r_ij.types.second == morse_types[0]))) {
336 arguments_t args(1, r_ij);
337
338 switch (index) {
339 case morse_spring_constant:
340 result += morse.parameter_derivative(args, PairPotential_Morse::spring_constant)[0];
341 break;
342 case morse_equilibrium_distance:
343 result += morse.parameter_derivative(args, PairPotential_Morse::equilibrium_distance)[0];
344 break;
345 case morse_dissociation_energy:
346 result += morse.parameter_derivative(args, PairPotential_Morse::dissociation_energy)[0];
347 break;
348 case angle_spring_constant:
349 case angle_equilibrium_distance:
350 {
351 typedef std::vector<FunctionModel::arguments_t> tripleargs_t;
352 tripleargs_t tripleargs =
353 triplefunction(r_ij, arguments);
354 for (tripleargs_t::const_iterator iter = tripleargs.begin();
355 iter != tripleargs.end();
356 ++iter) {
357 FunctionModel::arguments_t tempargs =
358 Extractors::reorderArgumentsByParticleTypes(*iter, angle.getParticleTypes());
359 // We get both angles, e.g. 0-4-1 and 1-4-0, hence multiply with 0.5
360 if (index == angle_spring_constant)
361 result += .5*angle.parameter_derivative(tempargs, PairPotential_Angle::spring_constant)[0];
362 else if (index == angle_equilibrium_distance)
363 result += .5*angle.parameter_derivative(tempargs, PairPotential_Angle::equilibrium_distance)[0];
364// LOG(2, "DEBUG: angle yields " << tmp << " for << " << arguments << ".");
365// result += tmp;
366 if (result != result)
367 ELOG(1, "result is NAN.");
368 }
369 }
370 break;
371 default:
372 ASSERT( 0, "SaturationPotential::parameter_derivative() - impossible to get here.");
373 break;
374 }
375 }
376 }
377 }
378 return SaturationPotential::results_t(1, result);
379}
380
381const SaturationPotential::ParticleTypes_t
382SaturationPotential::symmetrizeTypes(const ParticleTypes_t &_ParticleTypes)
383{
384 ASSERT( _ParticleTypes.size() == (size_t)2,
385 "SaturationPotential::symmetrizeTypes() - require initial _ParticleTypes with two elements.");
386// // insert before couple
387// ParticleTypes_t types(1, _ParticleTypes[1]);
388// types.insert(types.end(), _ParticleTypes.begin(), _ParticleTypes.end());
389 // insert after the couple
390 ParticleTypes_t types(_ParticleTypes);
391 types.push_back( _ParticleTypes.back() );
392 ASSERT( types.size() == (size_t)3,
393 "SaturationPotential::symmetrizeTypes() - failed to generate three types for angle.");
394 return types;
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.