| [155cc2] | 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.
 | 
|---|
| [155cc2] | 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 | /*
 | 
|---|
 | 26 |  * PairPotential_Morse.cpp
 | 
|---|
 | 27 |  *
 | 
|---|
 | 28 |  *  Created on: Oct 03, 2012
 | 
|---|
 | 29 |  *      Author: heber
 | 
|---|
 | 30 |  */
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | // include config.h
 | 
|---|
 | 34 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 35 | #include <config.h>
 | 
|---|
 | 36 | #endif
 | 
|---|
 | 37 | 
 | 
|---|
| [9eb71b3] | 38 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [155cc2] | 39 | 
 | 
|---|
 | 40 | #include "PairPotential_Morse.hpp"
 | 
|---|
 | 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>
 | 
|---|
| [155cc2] | 45 | #include <cmath>
 | 
|---|
| [ed2551] | 46 | #include <string>
 | 
|---|
| [155cc2] | 47 | 
 | 
|---|
 | 48 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 49 | 
 | 
|---|
| [7b019a] | 50 | #include "FunctionApproximation/Extractors.hpp"
 | 
|---|
| [d52819] | 51 | #include "FunctionApproximation/TrainingData.hpp"
 | 
|---|
| [155cc2] | 52 | #include "Potentials/helpers.hpp"
 | 
|---|
| [94453f1] | 53 | #include "Potentials/InternalCoordinates/TwoBody_Length.hpp"
 | 
|---|
| [b760bc3] | 54 | #include "Potentials/ParticleTypeCheckers.hpp"
 | 
|---|
| [0932c2] | 55 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
 | 
|---|
 | 56 | #include "RandomNumbers/RandomNumberGenerator.hpp"
 | 
|---|
| [155cc2] | 57 | 
 | 
|---|
| [7b019a] | 58 | class Fragment;
 | 
|---|
 | 59 | 
 | 
|---|
| [ed2551] | 60 | // static definitions
 | 
|---|
 | 61 | const PairPotential_Morse::ParameterNames_t
 | 
|---|
 | 62 | PairPotential_Morse::ParameterNames =
 | 
|---|
 | 63 |       boost::assign::list_of<std::string>
 | 
|---|
 | 64 |       ("spring_constant")
 | 
|---|
 | 65 |       ("equilibrium_distance")
 | 
|---|
 | 66 |       ("dissociation_energy")
 | 
|---|
 | 67 |     ;
 | 
|---|
 | 68 | const std::string PairPotential_Morse::potential_token("morse");
 | 
|---|
| [9eb71b3] | 69 | Coordinator::ptr PairPotential_Morse::coordinator( /* Memory::ignore( */ new TwoBody_Length() /* ) */ );
 | 
|---|
| [ed2551] | 70 | 
 | 
|---|
| [9c793c] | 71 | static BindingModel generateBindingModel(const EmpiricalPotential::ParticleTypes_t &_ParticleTypes)
 | 
|---|
| [d5ca1a] | 72 | {
 | 
|---|
 | 73 |   // fill nodes
 | 
|---|
| [9c793c] | 74 |   BindingModel::vector_nodes_t nodes;
 | 
|---|
| [d5ca1a] | 75 |   {
 | 
|---|
 | 76 |     ASSERT( _ParticleTypes.size() == (size_t)2,
 | 
|---|
 | 77 |         "generateBindingModel() - PairPotential_Morse needs two types.");
 | 
|---|
| [9c793c] | 78 |     nodes.push_back( FragmentNode(_ParticleTypes[0], 1) );
 | 
|---|
 | 79 |     nodes.push_back( FragmentNode(_ParticleTypes[1], 1) );
 | 
|---|
| [d5ca1a] | 80 |   }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |   // there are no edges
 | 
|---|
 | 83 |   HomologyGraph::edges_t edges;
 | 
|---|
 | 84 |   {
 | 
|---|
 | 85 |     edges.insert( std::make_pair( FragmentEdge(_ParticleTypes[0], _ParticleTypes[1]), 1) );
 | 
|---|
 | 86 |   }
 | 
|---|
 | 87 | 
 | 
|---|
| [9c793c] | 88 |   return BindingModel(nodes, edges);
 | 
|---|
| [d5ca1a] | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
| [a82d33] | 91 | PairPotential_Morse::PairPotential_Morse() :
 | 
|---|
 | 92 |   EmpiricalPotential(),
 | 
|---|
| [d5ca1a] | 93 |   params(parameters_t(MAXPARAMS, 0.)),
 | 
|---|
| [9c793c] | 94 |   bindingmodel(BindingModel())
 | 
|---|
| [a82d33] | 95 | {
 | 
|---|
 | 96 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
 | 97 |   params[spring_constant] = 1.;
 | 
|---|
 | 98 |   params[equilibrium_distance] = 1.;
 | 
|---|
 | 99 |   params[dissociation_energy] = 0.1;
 | 
|---|
 | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
| [ed2551] | 102 | PairPotential_Morse::PairPotential_Morse(
 | 
|---|
 | 103 |     const ParticleTypes_t &_ParticleTypes
 | 
|---|
 | 104 |     ) :
 | 
|---|
| [fdd23a] | 105 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| [d5ca1a] | 106 |   params(parameters_t(MAXPARAMS, 0.)),
 | 
|---|
 | 107 |   bindingmodel(generateBindingModel(_ParticleTypes))
 | 
|---|
| [dbf8c8] | 108 | {
 | 
|---|
 | 109 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
 | 110 |   params[spring_constant] = 1.;
 | 
|---|
 | 111 |   params[equilibrium_distance] = 1.;
 | 
|---|
 | 112 |   params[dissociation_energy] = 0.1;
 | 
|---|
 | 113 | }
 | 
|---|
| [155cc2] | 114 | 
 | 
|---|
 | 115 | PairPotential_Morse::PairPotential_Morse(
 | 
|---|
| [ed2551] | 116 |     const ParticleTypes_t &_ParticleTypes,
 | 
|---|
| [155cc2] | 117 |     const double _spring_constant,
 | 
|---|
 | 118 |     const double _equilibrium_distance,
 | 
|---|
| [919c8a] | 119 |     const double _dissociation_energy) :
 | 
|---|
| [fdd23a] | 120 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| [d5ca1a] | 121 |   params(parameters_t(MAXPARAMS, 0.)),
 | 
|---|
 | 122 |   bindingmodel(generateBindingModel(_ParticleTypes))
 | 
|---|
| [155cc2] | 123 | {
 | 
|---|
 | 124 |   params[spring_constant] = _spring_constant;
 | 
|---|
 | 125 |   params[equilibrium_distance] = _equilibrium_distance;
 | 
|---|
 | 126 |   params[dissociation_energy] = _dissociation_energy;
 | 
|---|
 | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
| [086070] | 129 | void PairPotential_Morse::setParameters(const parameters_t &_params)
 | 
|---|
 | 130 | {
 | 
|---|
 | 131 |   const size_t paramsDim = _params.size();
 | 
|---|
 | 132 |   ASSERT( paramsDim <= getParameterDimension(),
 | 
|---|
 | 133 |       "PairPotential_Morse::setParameters() - we need not more than "
 | 
|---|
 | 134 |       +toString(getParameterDimension())+" parameters.");
 | 
|---|
 | 135 |   for(size_t i=0;i<paramsDim;++i)
 | 
|---|
 | 136 |     params[i] = _params[i];
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 | #ifndef NDEBUG
 | 
|---|
 | 139 |   parameters_t check_params(getParameters());
 | 
|---|
 | 140 |   check_params.resize(paramsDim); // truncate to same size
 | 
|---|
 | 141 |   ASSERT( check_params == _params,
 | 
|---|
 | 142 |       "PairPotential_Morse::setParameters() - failed, mismatch in to be set "
 | 
|---|
 | 143 |       +toString(_params)+" and set "+toString(check_params)+" params.");
 | 
|---|
 | 144 | #endif
 | 
|---|
 | 145 | }
 | 
|---|
 | 146 | 
 | 
|---|
| [155cc2] | 147 | PairPotential_Morse::results_t
 | 
|---|
 | 148 | PairPotential_Morse::operator()(
 | 
|---|
| [e1fe7e] | 149 |     const list_of_arguments_t &listarguments
 | 
|---|
| [155cc2] | 150 |     ) const
 | 
|---|
 | 151 | {
 | 
|---|
| [e1fe7e] | 152 |   result_t result = 0.;
 | 
|---|
 | 153 |   for(list_of_arguments_t::const_iterator iter = listarguments.begin();
 | 
|---|
 | 154 |       iter != listarguments.end(); ++iter) {
 | 
|---|
 | 155 |     const arguments_t &arguments = *iter;
 | 
|---|
 | 156 |     ASSERT( arguments.size() == 1,
 | 
|---|
 | 157 |         "PairPotential_Morse::operator() - requires exactly one argument.");
 | 
|---|
 | 158 |     ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 159 |         arguments, getParticleTypes()),
 | 
|---|
 | 160 |         "PairPotential_Morse::operator() - types don't match with ones in arguments.");
 | 
|---|
 | 161 |     const argument_t &r_ij = arguments[0];
 | 
|---|
 | 162 |     // Maple: f(r,D,k,R,c) := D * (1 - exp(-k*(r-R)))^(2)+c
 | 
|---|
 | 163 |     result +=
 | 
|---|
 | 164 |         params[dissociation_energy] * Helpers::pow( 1.
 | 
|---|
 | 165 |             - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])),2);
 | 
|---|
 | 166 |   }
 | 
|---|
| [155cc2] | 167 |   return std::vector<result_t>(1, result);
 | 
|---|
 | 168 | }
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 | PairPotential_Morse::derivative_components_t
 | 
|---|
 | 171 | PairPotential_Morse::derivative(
 | 
|---|
| [e1fe7e] | 172 |     const list_of_arguments_t &listarguments
 | 
|---|
| [155cc2] | 173 |     ) const
 | 
|---|
 | 174 | {
 | 
|---|
| [e1fe7e] | 175 |   result_t result = 0.;
 | 
|---|
 | 176 |   for(list_of_arguments_t::const_iterator iter = listarguments.begin();
 | 
|---|
 | 177 |       iter != listarguments.end(); ++iter) {
 | 
|---|
 | 178 |     const arguments_t &arguments = *iter;
 | 
|---|
 | 179 |     ASSERT( arguments.size() == 1,
 | 
|---|
 | 180 |         "PairPotential_Morse::operator() - requires exactly one argument.");
 | 
|---|
 | 181 |     ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 182 |         arguments, getParticleTypes()),
 | 
|---|
 | 183 |         "PairPotential_Morse::operator() - types don't match with ones in arguments.");
 | 
|---|
 | 184 |     const argument_t &r_ij = arguments[0];
 | 
|---|
 | 185 |     // Maple result: 2*D*(1-exp(-k*(r-R)))*k*exp(-k*(r-R))
 | 
|---|
 | 186 |     result +=
 | 
|---|
 | 187 |         2. * params[dissociation_energy]
 | 
|---|
 | 188 |         * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])))
 | 
|---|
 | 189 |         * (- params[spring_constant]) * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]));
 | 
|---|
 | 190 |   }
 | 
|---|
 | 191 |   return derivative_components_t(1, result);
 | 
|---|
| [155cc2] | 192 | }
 | 
|---|
 | 193 | 
 | 
|---|
 | 194 | PairPotential_Morse::results_t
 | 
|---|
 | 195 | PairPotential_Morse::parameter_derivative(
 | 
|---|
| [e1fe7e] | 196 |     const list_of_arguments_t &listarguments,
 | 
|---|
| [155cc2] | 197 |     const size_t index
 | 
|---|
 | 198 |     ) const
 | 
|---|
 | 199 | {
 | 
|---|
| [e1fe7e] | 200 |   result_t result = 0.;
 | 
|---|
 | 201 |   for(list_of_arguments_t::const_iterator iter = listarguments.begin();
 | 
|---|
 | 202 |       iter != listarguments.end(); ++iter) {
 | 
|---|
 | 203 |     const arguments_t &arguments = *iter;
 | 
|---|
 | 204 |     ASSERT( arguments.size() == 1,
 | 
|---|
 | 205 |         "PairPotential_Morse::parameter_derivative() - requires exactly one argument.");
 | 
|---|
 | 206 |     ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 207 |         arguments, getParticleTypes()),
 | 
|---|
 | 208 |         "PairPotential_Morse::operator() - types don't match with ones in arguments.");
 | 
|---|
 | 209 |     const argument_t &r_ij = arguments[0];
 | 
|---|
 | 210 |     switch (index) {
 | 
|---|
 | 211 |       case spring_constant:
 | 
|---|
 | 212 |       {
 | 
|---|
 | 213 |         // Maple result: -2*D*(1-exp(-k*(r-R)))*(-r+R)*exp(-k*(r-R))
 | 
|---|
 | 214 |         result +=
 | 
|---|
 | 215 |             - 2. * params[dissociation_energy]
 | 
|---|
 | 216 |             * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])))
 | 
|---|
 | 217 |             * (- r_ij.distance + params[equilibrium_distance])
 | 
|---|
 | 218 |             * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]))
 | 
|---|
 | 219 |             ;
 | 
|---|
 | 220 |         break;
 | 
|---|
 | 221 |       }
 | 
|---|
 | 222 |       case equilibrium_distance:
 | 
|---|
 | 223 |       {
 | 
|---|
 | 224 |         // Maple result: -2*D*(1-exp(-k*(r-R)))*k*exp(-k*(r-R))
 | 
|---|
 | 225 |         result +=
 | 
|---|
 | 226 |             - 2. * params[dissociation_energy]
 | 
|---|
 | 227 |             * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])))
 | 
|---|
 | 228 |             * params[spring_constant] * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]))
 | 
|---|
 | 229 |             ;
 | 
|---|
 | 230 |         break;
 | 
|---|
 | 231 |       }
 | 
|---|
 | 232 |       case dissociation_energy:
 | 
|---|
 | 233 |       {
 | 
|---|
 | 234 |         // Maple result: (1-exp(-k*(r-R)))^2
 | 
|---|
 | 235 |         result +=
 | 
|---|
 | 236 |             Helpers::pow(1.
 | 
|---|
 | 237 |                 - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])),2);
 | 
|---|
 | 238 |         break;
 | 
|---|
 | 239 |       }
 | 
|---|
 | 240 |       default:
 | 
|---|
 | 241 |         ASSERT(0, "PairPotential_Morse::parameter_derivative() - derivative to unknown parameter desired.");
 | 
|---|
 | 242 |         break;
 | 
|---|
| [155cc2] | 243 |     }
 | 
|---|
 | 244 |   }
 | 
|---|
| [e1fe7e] | 245 |   return results_t(1, result);
 | 
|---|
| [155cc2] | 246 | }
 | 
|---|
| [7b019a] | 247 | 
 | 
|---|
| [0f5d38] | 248 | FunctionModel::filter_t PairPotential_Morse::getSpecificFilter() const
 | 
|---|
 | 249 | {
 | 
|---|
 | 250 |   FunctionModel::filter_t returnfunction =
 | 
|---|
| [51e0e3] | 251 |       boost::bind(&Extractors::filterArgumentsByParticleTypes,
 | 
|---|
| [e60558] | 252 |           _2, _1,
 | 
|---|
| [67044a] | 253 |           boost::cref(getParticleTypes()), boost::cref(getBindingModel()));
 | 
|---|
| [0f5d38] | 254 |   return returnfunction;
 | 
|---|
 | 255 | }
 | 
|---|
 | 256 | 
 | 
|---|
| [d52819] | 257 | void
 | 
|---|
 | 258 | PairPotential_Morse::setParametersToRandomInitialValues(
 | 
|---|
 | 259 |     const TrainingData &data)
 | 
|---|
 | 260 | {
 | 
|---|
| [0932c2] | 261 |   RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
 | 
|---|
 | 262 |   const double rng_min = random.min();
 | 
|---|
 | 263 |   const double rng_max = random.max();
 | 
|---|
 | 264 |   params[PairPotential_Morse::dissociation_energy] = 1e+0*(random()/(rng_max-rng_min));// 0.5;
 | 
|---|
 | 265 |   params[PairPotential_Morse::spring_constant] = 1e+0*(random()/(rng_max-rng_min));// 1.;
 | 
|---|
 | 266 |   params[PairPotential_Morse::equilibrium_distance] =  3e+0*(random()/(rng_max-rng_min));//2.9;
 | 
|---|
| [d52819] | 267 | }
 | 
|---|
 | 268 | 
 | 
|---|