| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 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 | 
 | 
|---|
| 38 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "PairPotential_Morse.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
 | 
|---|
| 43 | #include <boost/bind.hpp>
 | 
|---|
| 44 | #include <boost/lambda/lambda.hpp>
 | 
|---|
| 45 | #include <cmath>
 | 
|---|
| 46 | #include <string>
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #include "FunctionApproximation/Extractors.hpp"
 | 
|---|
| 51 | #include "FunctionApproximation/TrainingData.hpp"
 | 
|---|
| 52 | #include "Potentials/helpers.hpp"
 | 
|---|
| 53 | #include "Potentials/InternalCoordinates/TwoBody_Length.hpp"
 | 
|---|
| 54 | #include "Potentials/ParticleTypeCheckers.hpp"
 | 
|---|
| 55 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
 | 
|---|
| 56 | #include "RandomNumbers/RandomNumberGenerator.hpp"
 | 
|---|
| 57 | 
 | 
|---|
| 58 | class Fragment;
 | 
|---|
| 59 | 
 | 
|---|
| 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");
 | 
|---|
| 69 | Coordinator::ptr PairPotential_Morse::coordinator( /* Memory::ignore( */ new TwoBody_Length() /* ) */ );
 | 
|---|
| 70 | 
 | 
|---|
| 71 | static BindingModel generateBindingModel(const EmpiricalPotential::ParticleTypes_t &_ParticleTypes)
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   // fill nodes
 | 
|---|
| 74 |   BindingModel::vector_nodes_t nodes;
 | 
|---|
| 75 |   {
 | 
|---|
| 76 |     ASSERT( _ParticleTypes.size() == (size_t)2,
 | 
|---|
| 77 |         "generateBindingModel() - PairPotential_Morse needs two types.");
 | 
|---|
| 78 |     nodes.push_back( FragmentNode(_ParticleTypes[0], 1) );
 | 
|---|
| 79 |     nodes.push_back( FragmentNode(_ParticleTypes[1], 1) );
 | 
|---|
| 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 | 
 | 
|---|
| 88 |   return BindingModel(nodes, edges);
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | PairPotential_Morse::PairPotential_Morse() :
 | 
|---|
| 92 |   EmpiricalPotential(),
 | 
|---|
| 93 |   params(parameters_t(MAXPARAMS, 0.)),
 | 
|---|
| 94 |   bindingmodel(BindingModel())
 | 
|---|
| 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 | 
 | 
|---|
| 102 | PairPotential_Morse::PairPotential_Morse(
 | 
|---|
| 103 |     const ParticleTypes_t &_ParticleTypes
 | 
|---|
| 104 |     ) :
 | 
|---|
| 105 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| 106 |   params(parameters_t(MAXPARAMS, 0.)),
 | 
|---|
| 107 |   bindingmodel(generateBindingModel(_ParticleTypes))
 | 
|---|
| 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 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | PairPotential_Morse::PairPotential_Morse(
 | 
|---|
| 116 |     const ParticleTypes_t &_ParticleTypes,
 | 
|---|
| 117 |     const double _spring_constant,
 | 
|---|
| 118 |     const double _equilibrium_distance,
 | 
|---|
| 119 |     const double _dissociation_energy) :
 | 
|---|
| 120 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| 121 |   params(parameters_t(MAXPARAMS, 0.)),
 | 
|---|
| 122 |   bindingmodel(generateBindingModel(_ParticleTypes))
 | 
|---|
| 123 | {
 | 
|---|
| 124 |   params[spring_constant] = _spring_constant;
 | 
|---|
| 125 |   params[equilibrium_distance] = _equilibrium_distance;
 | 
|---|
| 126 |   params[dissociation_energy] = _dissociation_energy;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 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 | 
 | 
|---|
| 147 | PairPotential_Morse::results_t
 | 
|---|
| 148 | PairPotential_Morse::operator()(
 | 
|---|
| 149 |     const list_of_arguments_t &listarguments
 | 
|---|
| 150 |     ) const
 | 
|---|
| 151 | {
 | 
|---|
| 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 |   }
 | 
|---|
| 167 |   return std::vector<result_t>(1, result);
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | PairPotential_Morse::derivative_components_t
 | 
|---|
| 171 | PairPotential_Morse::derivative(
 | 
|---|
| 172 |     const list_of_arguments_t &listarguments
 | 
|---|
| 173 |     ) const
 | 
|---|
| 174 | {
 | 
|---|
| 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);
 | 
|---|
| 192 | }
 | 
|---|
| 193 | 
 | 
|---|
| 194 | PairPotential_Morse::results_t
 | 
|---|
| 195 | PairPotential_Morse::parameter_derivative(
 | 
|---|
| 196 |     const list_of_arguments_t &listarguments,
 | 
|---|
| 197 |     const size_t index
 | 
|---|
| 198 |     ) const
 | 
|---|
| 199 | {
 | 
|---|
| 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;
 | 
|---|
| 243 |     }
 | 
|---|
| 244 |   }
 | 
|---|
| 245 |   return results_t(1, result);
 | 
|---|
| 246 | }
 | 
|---|
| 247 | 
 | 
|---|
| 248 | FunctionModel::filter_t PairPotential_Morse::getSpecificFilter() const
 | 
|---|
| 249 | {
 | 
|---|
| 250 |   FunctionModel::filter_t returnfunction =
 | 
|---|
| 251 |       boost::bind(&Extractors::filterArgumentsByParticleTypes,
 | 
|---|
| 252 |           _2, _1,
 | 
|---|
| 253 |           boost::cref(getParticleTypes()), boost::cref(getBindingModel()));
 | 
|---|
| 254 |   return returnfunction;
 | 
|---|
| 255 | }
 | 
|---|
| 256 | 
 | 
|---|
| 257 | void
 | 
|---|
| 258 | PairPotential_Morse::setParametersToRandomInitialValues(
 | 
|---|
| 259 |     const TrainingData &data)
 | 
|---|
| 260 | {
 | 
|---|
| 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;
 | 
|---|
| 267 | }
 | 
|---|
| 268 | 
 | 
|---|