source: src/Potentials/Specifics/ThreeBodyPotential_Angle.cpp@ 35760a

Fix_FitPotential_needs_atomicnumbers
Last change on this file since 35760a was 35760a, checked in by Frederik Heber <heber@…>, 9 years ago

tempcommit: Merge with b794f5e8

  • Property mode set to 100644
File size: 8.6 KB
RevLine 
[a63187]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
[acc9b1]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[a63187]6 * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
7 *
8 *
9 * This file is part of MoleCuilder.
10 *
11 * MoleCuilder is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * MoleCuilder is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25/*
[484e2a]26 * ThreeBodyPotential_Angle.cpp
[a63187]27 *
28 * Created on: Oct 11, 2012
29 * Author: heber
30 */
31
32
33// include config.h
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36#endif
37
38#include "CodePatterns/MemDebug.hpp"
39
[484e2a]40#include "ThreeBodyPotential_Angle.hpp"
[a63187]41
[ed2551]42#include <boost/assign/list_of.hpp> // for 'map_list_of()'
[7b019a]43#include <boost/bind.hpp>
[da2d5c]44#include <boost/lambda/lambda.hpp>
[ed2551]45#include <string>
46
[a63187]47#include "CodePatterns/Assert.hpp"
48
[7b019a]49#include "FunctionApproximation/Extractors.hpp"
[d52819]50#include "FunctionApproximation/TrainingData.hpp"
[a63187]51#include "Potentials/helpers.hpp"
[94453f1]52#include "Potentials/InternalCoordinates/ThreeBody_Angle.hpp"
[b760bc3]53#include "Potentials/ParticleTypeCheckers.hpp"
[0932c2]54#include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
55#include "RandomNumbers/RandomNumberGenerator.hpp"
[a63187]56
[7b019a]57class Fragment;
58
[ed2551]59// static definitions
[484e2a]60const ThreeBodyPotential_Angle::ParameterNames_t
61ThreeBodyPotential_Angle::ParameterNames =
[ed2551]62 boost::assign::list_of<std::string>
63 ("spring_constant")
64 ("equilibrium_distance")
65 ;
[484e2a]66const std::string ThreeBodyPotential_Angle::potential_token("harmonic_angle");
[7d320c]67Coordinator::ptr ThreeBodyPotential_Angle::coordinator(Memory::ignore(new ThreeBody_Angle()));
[35760a]68static PotentialSubgraph generateSubgraph()
69{
70 return PotentialSubgraph();
71}
72const PotentialSubgraph ThreeBodyPotential_Angle::subgraph(generateSubgraph());
[ed2551]73
[484e2a]74ThreeBodyPotential_Angle::ThreeBodyPotential_Angle() :
[a82d33]75 EmpiricalPotential(),
76 params(parameters_t(MAXPARAMS, 0.))
77{
78 // have some decent defaults for parameter_derivative checking
79 params[spring_constant] = 1.;
80 params[equilibrium_distance] = 0.1;
81}
82
[484e2a]83ThreeBodyPotential_Angle::ThreeBodyPotential_Angle(
[ed2551]84 const ParticleTypes_t &_ParticleTypes
85 ) :
[fdd23a]86 EmpiricalPotential(_ParticleTypes),
[a63187]87 params(parameters_t(MAXPARAMS, 0.))
[dbf8c8]88{
89 // have some decent defaults for parameter_derivative checking
90 params[spring_constant] = 1.;
91 params[equilibrium_distance] = 0.1;
92}
[a63187]93
[484e2a]94ThreeBodyPotential_Angle::ThreeBodyPotential_Angle(
[ed2551]95 const ParticleTypes_t &_ParticleTypes,
[a63187]96 const double _spring_constant,
[1e242a]97 const double _equilibrium_distance) :
[fdd23a]98 EmpiricalPotential(_ParticleTypes),
[ed2551]99 params(parameters_t(MAXPARAMS, 0.))
[a63187]100{
101 params[spring_constant] = _spring_constant;
102 params[equilibrium_distance] = _equilibrium_distance;
103}
104
[484e2a]105void ThreeBodyPotential_Angle::setParameters(const parameters_t &_params)
[086070]106{
107 const size_t paramsDim = _params.size();
108 ASSERT( paramsDim <= getParameterDimension(),
[484e2a]109 "ThreeBodyPotential_Angle::setParameters() - we need not more than "
[086070]110 +toString(getParameterDimension())+" parameters.");
111 for(size_t i=0;i<paramsDim;++i)
112 params[i] = _params[i];
113
114#ifndef NDEBUG
115 parameters_t check_params(getParameters());
116 check_params.resize(paramsDim); // truncate to same size
117 ASSERT( check_params == _params,
[484e2a]118 "ThreeBodyPotential_Angle::setParameters() - failed, mismatch in to be set "
[086070]119 +toString(_params)+" and set "+toString(check_params)+" params.");
120#endif
121}
122
[484e2a]123ThreeBodyPotential_Angle::result_t
124ThreeBodyPotential_Angle::function_theta(
[a63187]125 const double &r_ij,
[bbc422]126 const double &r_jk,
127 const double &r_ik
[a63187]128 ) const
129{
130// Info info(__func__);
[bbc422]131 const double angle = Helpers::pow(r_ij,2) + Helpers::pow(r_jk,2) - Helpers::pow(r_ik,2);
132 const double divisor = 2.* r_ij * r_jk;
[a63187]133
134// LOG(2, "DEBUG: cos(theta)= " << angle/divisor);
135 if (divisor == 0.)
136 return 0.;
137 else
138 return angle/divisor;
139}
140
[484e2a]141ThreeBodyPotential_Angle::results_t
142ThreeBodyPotential_Angle::operator()(
[e1fe7e]143 const list_of_arguments_t &listarguments
[a63187]144 ) const
145{
[e1fe7e]146 result_t result = 0.;
147 for(list_of_arguments_t::const_iterator iter = listarguments.begin();
148 iter != listarguments.end(); ++iter) {
149 const arguments_t &arguments = *iter;
150 ASSERT( arguments.size() == 3,
151 "ThreeBodyPotential_Angle::operator() - requires exactly three arguments.");
152 ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
153 arguments, getParticleTypes()),
154 "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments.");
155 const argument_t &r_ij = arguments[0]; // 01
156 const argument_t &r_jk = arguments[2]; // 12
157 const argument_t &r_ik = arguments[1]; // 02
158 result +=
159 params[spring_constant]
160 * Helpers::pow( function_theta(r_ij.distance, r_jk.distance, r_ik.distance)
161 - params[equilibrium_distance], 2 );
162 }
163 return results_t(1, result);
[a63187]164}
165
[484e2a]166ThreeBodyPotential_Angle::derivative_components_t
167ThreeBodyPotential_Angle::derivative(
[e1fe7e]168 const list_of_arguments_t &listarguments
[a63187]169 ) const
170{
[e1fe7e]171 result_t result = 0.;
172 for(list_of_arguments_t::const_iterator iter = listarguments.begin();
173 iter != listarguments.end(); ++iter) {
174 const arguments_t &arguments = *iter;
175 ASSERT( arguments.size() == 3,
176 "ThreeBodyPotential_Angle::operator() - requires exactly three arguments.");
177 ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
178 arguments, getParticleTypes()),
179 "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments.");
180 const argument_t &r_ij = arguments[0]; //01
181 const argument_t &r_jk = arguments[2]; //12
182 const argument_t &r_ik = arguments[1]; //02
183 result +=
184 2. * params[spring_constant] *
185 ( function_theta(r_ij.distance, r_jk.distance, r_ik.distance)
186 - params[equilibrium_distance]);
187 }
188 return derivative_components_t(1, result);
[a63187]189}
190
[484e2a]191ThreeBodyPotential_Angle::results_t
192ThreeBodyPotential_Angle::parameter_derivative(
[e1fe7e]193 const list_of_arguments_t &listarguments,
[a63187]194 const size_t index
195 ) const
196{
[e1fe7e]197 result_t result = 0.;
198 for(list_of_arguments_t::const_iterator iter = listarguments.begin();
199 iter != listarguments.end(); ++iter) {
200 const arguments_t &arguments = *iter;
201 ASSERT( arguments.size() == 3,
202 "ThreeBodyPotential_Angle::parameter_derivative() - requires exactly three arguments.");
203 ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
204 arguments, getParticleTypes()),
205 "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments.");
206 const argument_t &r_ij = arguments[0]; //01
207 const argument_t &r_jk = arguments[2]; //12
208 const argument_t &r_ik = arguments[1]; //02
209 switch (index) {
210 case spring_constant:
211 {
212 result +=
213 Helpers::pow( function_theta(r_ij.distance, r_jk.distance, r_ik.distance) - params[equilibrium_distance], 2 );
214 break;
215 }
216 case equilibrium_distance:
217 {
218 result +=
219 -2. * params[spring_constant]
220 * ( function_theta(r_ij.distance, r_jk.distance, r_ik.distance) - params[equilibrium_distance]);
221 break;
222 }
223 default:
224 ASSERT(0, "ThreeBodyPotential_Angle::parameter_derivative() - derivative to unknown parameter desired.");
225 break;
[a63187]226 }
227 }
[e1fe7e]228 return results_t(1, result);
[a63187]229}
[7b019a]230
[484e2a]231FunctionModel::filter_t ThreeBodyPotential_Angle::getSpecificFilter() const
[0f5d38]232{
233 FunctionModel::filter_t returnfunction =
234 boost::bind(&Extractors::reorderArgumentsByParticleTypes,
[51e0e3]235 boost::bind(&Extractors::filterArgumentsByParticleTypes,
236 _1,
[11bab4]237 getParticleTypes(), getSubgraph()),
238 getParticleTypes(), getSubgraph()
[0f5d38]239 );
240 return returnfunction;
241}
242
[d52819]243void
[484e2a]244ThreeBodyPotential_Angle::setParametersToRandomInitialValues(
[d52819]245 const TrainingData &data)
246{
[0932c2]247 RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
248 const double rng_min = random.min();
249 const double rng_max = random.max();
250 params[ThreeBodyPotential_Angle::spring_constant] = 1e+0*(random()/(rng_max-rng_min));// 0.2;
251 params[ThreeBodyPotential_Angle::equilibrium_distance] = -0.3;//2e+0*(random()/(rng_max-rng_min)) - 1.;// 1.;
[d52819]252}
253
Note: See TracBrowser for help on using the repository browser.