[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] | 57 | class Fragment;
|
---|
| 58 |
|
---|
[ed2551] | 59 | // static definitions
|
---|
[484e2a] | 60 | const ThreeBodyPotential_Angle::ParameterNames_t
|
---|
| 61 | ThreeBodyPotential_Angle::ParameterNames =
|
---|
[ed2551] | 62 | boost::assign::list_of<std::string>
|
---|
| 63 | ("spring_constant")
|
---|
| 64 | ("equilibrium_distance")
|
---|
| 65 | ;
|
---|
[484e2a] | 66 | const std::string ThreeBodyPotential_Angle::potential_token("harmonic_angle");
|
---|
[7d320c] | 67 | Coordinator::ptr ThreeBodyPotential_Angle::coordinator(Memory::ignore(new ThreeBody_Angle()));
|
---|
[ed2551] | 68 |
|
---|
[d5ca1a] | 69 | static HomologyGraph generateBindingModel(const EmpiricalPotential::ParticleTypes_t &_ParticleTypes)
|
---|
| 70 | {
|
---|
| 71 | // fill nodes
|
---|
| 72 | HomologyGraph::nodes_t nodes;
|
---|
| 73 | {
|
---|
| 74 | ASSERT( _ParticleTypes.size() == (size_t)3,
|
---|
| 75 | "generateBindingModel() - ThreeBodyPotential_Angle needs three types.");
|
---|
| 76 | std::pair<HomologyGraph::nodes_t::iterator, bool > inserter;
|
---|
| 77 | inserter = nodes.insert( std::make_pair(FragmentNode(_ParticleTypes[0], 1), 1) );
|
---|
| 78 | if (!inserter.second)
|
---|
| 79 | ++(inserter.first->second);
|
---|
| 80 | inserter = nodes.insert( std::make_pair(FragmentNode(_ParticleTypes[1], 2), 1) );
|
---|
| 81 | if (!inserter.second)
|
---|
| 82 | ++(inserter.first->second);
|
---|
| 83 | inserter = nodes.insert( std::make_pair(FragmentNode(_ParticleTypes[2], 1), 1) );
|
---|
| 84 | if (!inserter.second)
|
---|
| 85 | ++(inserter.first->second);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // there are no edges
|
---|
| 89 | HomologyGraph::edges_t edges;
|
---|
| 90 | {
|
---|
| 91 | std::pair<HomologyGraph::edges_t::iterator, bool > inserter;
|
---|
| 92 | inserter = edges.insert( std::make_pair( FragmentEdge(_ParticleTypes[0], _ParticleTypes[1]), 1) );
|
---|
| 93 | if (!inserter.second)
|
---|
| 94 | ++(inserter.first->second);
|
---|
| 95 | inserter = edges.insert( std::make_pair( FragmentEdge(_ParticleTypes[1], _ParticleTypes[2]), 1) );
|
---|
| 96 | if (!inserter.second)
|
---|
| 97 | ++(inserter.first->second);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return HomologyGraph(nodes, edges);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[484e2a] | 103 | ThreeBodyPotential_Angle::ThreeBodyPotential_Angle() :
|
---|
[a82d33] | 104 | EmpiricalPotential(),
|
---|
[d5ca1a] | 105 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
| 106 | bindingmodel(HomologyGraph())
|
---|
[a82d33] | 107 | {
|
---|
| 108 | // have some decent defaults for parameter_derivative checking
|
---|
| 109 | params[spring_constant] = 1.;
|
---|
| 110 | params[equilibrium_distance] = 0.1;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[484e2a] | 113 | ThreeBodyPotential_Angle::ThreeBodyPotential_Angle(
|
---|
[ed2551] | 114 | const ParticleTypes_t &_ParticleTypes
|
---|
| 115 | ) :
|
---|
[fdd23a] | 116 | EmpiricalPotential(_ParticleTypes),
|
---|
[d5ca1a] | 117 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
| 118 | bindingmodel(generateBindingModel(_ParticleTypes))
|
---|
[dbf8c8] | 119 | {
|
---|
| 120 | // have some decent defaults for parameter_derivative checking
|
---|
| 121 | params[spring_constant] = 1.;
|
---|
| 122 | params[equilibrium_distance] = 0.1;
|
---|
| 123 | }
|
---|
[a63187] | 124 |
|
---|
[484e2a] | 125 | ThreeBodyPotential_Angle::ThreeBodyPotential_Angle(
|
---|
[ed2551] | 126 | const ParticleTypes_t &_ParticleTypes,
|
---|
[a63187] | 127 | const double _spring_constant,
|
---|
[1e242a] | 128 | const double _equilibrium_distance) :
|
---|
[fdd23a] | 129 | EmpiricalPotential(_ParticleTypes),
|
---|
[d5ca1a] | 130 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
| 131 | bindingmodel(generateBindingModel(_ParticleTypes))
|
---|
[a63187] | 132 | {
|
---|
| 133 | params[spring_constant] = _spring_constant;
|
---|
| 134 | params[equilibrium_distance] = _equilibrium_distance;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[484e2a] | 137 | void ThreeBodyPotential_Angle::setParameters(const parameters_t &_params)
|
---|
[086070] | 138 | {
|
---|
| 139 | const size_t paramsDim = _params.size();
|
---|
| 140 | ASSERT( paramsDim <= getParameterDimension(),
|
---|
[484e2a] | 141 | "ThreeBodyPotential_Angle::setParameters() - we need not more than "
|
---|
[086070] | 142 | +toString(getParameterDimension())+" parameters.");
|
---|
| 143 | for(size_t i=0;i<paramsDim;++i)
|
---|
| 144 | params[i] = _params[i];
|
---|
| 145 |
|
---|
| 146 | #ifndef NDEBUG
|
---|
| 147 | parameters_t check_params(getParameters());
|
---|
| 148 | check_params.resize(paramsDim); // truncate to same size
|
---|
| 149 | ASSERT( check_params == _params,
|
---|
[484e2a] | 150 | "ThreeBodyPotential_Angle::setParameters() - failed, mismatch in to be set "
|
---|
[086070] | 151 | +toString(_params)+" and set "+toString(check_params)+" params.");
|
---|
| 152 | #endif
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[484e2a] | 155 | ThreeBodyPotential_Angle::result_t
|
---|
| 156 | ThreeBodyPotential_Angle::function_theta(
|
---|
[a63187] | 157 | const double &r_ij,
|
---|
[bbc422] | 158 | const double &r_jk,
|
---|
| 159 | const double &r_ik
|
---|
[a63187] | 160 | ) const
|
---|
| 161 | {
|
---|
| 162 | // Info info(__func__);
|
---|
[bbc422] | 163 | const double angle = Helpers::pow(r_ij,2) + Helpers::pow(r_jk,2) - Helpers::pow(r_ik,2);
|
---|
| 164 | const double divisor = 2.* r_ij * r_jk;
|
---|
[a63187] | 165 |
|
---|
| 166 | // LOG(2, "DEBUG: cos(theta)= " << angle/divisor);
|
---|
| 167 | if (divisor == 0.)
|
---|
| 168 | return 0.;
|
---|
| 169 | else
|
---|
| 170 | return angle/divisor;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[484e2a] | 173 | ThreeBodyPotential_Angle::results_t
|
---|
| 174 | ThreeBodyPotential_Angle::operator()(
|
---|
[e1fe7e] | 175 | const list_of_arguments_t &listarguments
|
---|
[a63187] | 176 | ) const
|
---|
| 177 | {
|
---|
[e1fe7e] | 178 | result_t result = 0.;
|
---|
| 179 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 180 | iter != listarguments.end(); ++iter) {
|
---|
| 181 | const arguments_t &arguments = *iter;
|
---|
| 182 | ASSERT( arguments.size() == 3,
|
---|
| 183 | "ThreeBodyPotential_Angle::operator() - requires exactly three arguments.");
|
---|
| 184 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 185 | arguments, getParticleTypes()),
|
---|
| 186 | "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments.");
|
---|
| 187 | const argument_t &r_ij = arguments[0]; // 01
|
---|
| 188 | const argument_t &r_jk = arguments[2]; // 12
|
---|
| 189 | const argument_t &r_ik = arguments[1]; // 02
|
---|
| 190 | result +=
|
---|
| 191 | params[spring_constant]
|
---|
| 192 | * Helpers::pow( function_theta(r_ij.distance, r_jk.distance, r_ik.distance)
|
---|
| 193 | - params[equilibrium_distance], 2 );
|
---|
| 194 | }
|
---|
| 195 | return results_t(1, result);
|
---|
[a63187] | 196 | }
|
---|
| 197 |
|
---|
[484e2a] | 198 | ThreeBodyPotential_Angle::derivative_components_t
|
---|
| 199 | ThreeBodyPotential_Angle::derivative(
|
---|
[e1fe7e] | 200 | const list_of_arguments_t &listarguments
|
---|
[a63187] | 201 | ) const
|
---|
| 202 | {
|
---|
[e1fe7e] | 203 | result_t result = 0.;
|
---|
| 204 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 205 | iter != listarguments.end(); ++iter) {
|
---|
| 206 | const arguments_t &arguments = *iter;
|
---|
| 207 | ASSERT( arguments.size() == 3,
|
---|
| 208 | "ThreeBodyPotential_Angle::operator() - requires exactly three arguments.");
|
---|
| 209 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 210 | arguments, getParticleTypes()),
|
---|
| 211 | "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments.");
|
---|
| 212 | const argument_t &r_ij = arguments[0]; //01
|
---|
| 213 | const argument_t &r_jk = arguments[2]; //12
|
---|
| 214 | const argument_t &r_ik = arguments[1]; //02
|
---|
| 215 | result +=
|
---|
| 216 | 2. * params[spring_constant] *
|
---|
| 217 | ( function_theta(r_ij.distance, r_jk.distance, r_ik.distance)
|
---|
| 218 | - params[equilibrium_distance]);
|
---|
| 219 | }
|
---|
| 220 | return derivative_components_t(1, result);
|
---|
[a63187] | 221 | }
|
---|
| 222 |
|
---|
[484e2a] | 223 | ThreeBodyPotential_Angle::results_t
|
---|
| 224 | ThreeBodyPotential_Angle::parameter_derivative(
|
---|
[e1fe7e] | 225 | const list_of_arguments_t &listarguments,
|
---|
[a63187] | 226 | const size_t index
|
---|
| 227 | ) const
|
---|
| 228 | {
|
---|
[e1fe7e] | 229 | result_t result = 0.;
|
---|
| 230 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 231 | iter != listarguments.end(); ++iter) {
|
---|
| 232 | const arguments_t &arguments = *iter;
|
---|
| 233 | ASSERT( arguments.size() == 3,
|
---|
| 234 | "ThreeBodyPotential_Angle::parameter_derivative() - requires exactly three arguments.");
|
---|
| 235 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 236 | arguments, getParticleTypes()),
|
---|
| 237 | "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments.");
|
---|
| 238 | const argument_t &r_ij = arguments[0]; //01
|
---|
| 239 | const argument_t &r_jk = arguments[2]; //12
|
---|
| 240 | const argument_t &r_ik = arguments[1]; //02
|
---|
| 241 | switch (index) {
|
---|
| 242 | case spring_constant:
|
---|
| 243 | {
|
---|
| 244 | result +=
|
---|
| 245 | Helpers::pow( function_theta(r_ij.distance, r_jk.distance, r_ik.distance) - params[equilibrium_distance], 2 );
|
---|
| 246 | break;
|
---|
| 247 | }
|
---|
| 248 | case equilibrium_distance:
|
---|
| 249 | {
|
---|
| 250 | result +=
|
---|
| 251 | -2. * params[spring_constant]
|
---|
| 252 | * ( function_theta(r_ij.distance, r_jk.distance, r_ik.distance) - params[equilibrium_distance]);
|
---|
| 253 | break;
|
---|
| 254 | }
|
---|
| 255 | default:
|
---|
| 256 | ASSERT(0, "ThreeBodyPotential_Angle::parameter_derivative() - derivative to unknown parameter desired.");
|
---|
| 257 | break;
|
---|
[a63187] | 258 | }
|
---|
| 259 | }
|
---|
[e1fe7e] | 260 | return results_t(1, result);
|
---|
[a63187] | 261 | }
|
---|
[7b019a] | 262 |
|
---|
[484e2a] | 263 | FunctionModel::filter_t ThreeBodyPotential_Angle::getSpecificFilter() const
|
---|
[0f5d38] | 264 | {
|
---|
| 265 | FunctionModel::filter_t returnfunction =
|
---|
| 266 | boost::bind(&Extractors::reorderArgumentsByParticleTypes,
|
---|
[51e0e3] | 267 | boost::bind(&Extractors::filterArgumentsByParticleTypes,
|
---|
[e60558] | 268 | _2, _1,
|
---|
| 269 | boost::cref(getParticleTypes()), boost::cref(getBindingModel())),
|
---|
| 270 | _1,
|
---|
[67044a] | 271 | boost::cref(getParticleTypes()), boost::cref(getBindingModel())
|
---|
[0f5d38] | 272 | );
|
---|
| 273 | return returnfunction;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[d52819] | 276 | void
|
---|
[484e2a] | 277 | ThreeBodyPotential_Angle::setParametersToRandomInitialValues(
|
---|
[d52819] | 278 | const TrainingData &data)
|
---|
| 279 | {
|
---|
[0932c2] | 280 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
|
---|
| 281 | const double rng_min = random.min();
|
---|
| 282 | const double rng_max = random.max();
|
---|
| 283 | params[ThreeBodyPotential_Angle::spring_constant] = 1e+0*(random()/(rng_max-rng_min));// 0.2;
|
---|
| 284 | params[ThreeBodyPotential_Angle::equilibrium_distance] = -0.3;//2e+0*(random()/(rng_max-rng_min)) - 1.;// 1.;
|
---|
[d52819] | 285 | }
|
---|
| 286 |
|
---|