| [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 | 
 | 
|---|
 | 38 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 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");
 | 
|---|
| [94453f1] | 69 | Coordinator::ptr PairPotential_Morse::coordinator(new TwoBody_Length());
 | 
|---|
| [ed2551] | 70 | 
 | 
|---|
| [a82d33] | 71 | PairPotential_Morse::PairPotential_Morse() :
 | 
|---|
 | 72 |   EmpiricalPotential(),
 | 
|---|
 | 73 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
 | 74 | {
 | 
|---|
 | 75 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
 | 76 |   params[spring_constant] = 1.;
 | 
|---|
 | 77 |   params[equilibrium_distance] = 1.;
 | 
|---|
 | 78 |   params[dissociation_energy] = 0.1;
 | 
|---|
 | 79 | }
 | 
|---|
 | 80 | 
 | 
|---|
| [ed2551] | 81 | PairPotential_Morse::PairPotential_Morse(
 | 
|---|
 | 82 |     const ParticleTypes_t &_ParticleTypes
 | 
|---|
 | 83 |     ) :
 | 
|---|
| [fdd23a] | 84 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| [155cc2] | 85 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| [dbf8c8] | 86 | {
 | 
|---|
 | 87 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
 | 88 |   params[spring_constant] = 1.;
 | 
|---|
 | 89 |   params[equilibrium_distance] = 1.;
 | 
|---|
 | 90 |   params[dissociation_energy] = 0.1;
 | 
|---|
 | 91 | }
 | 
|---|
| [155cc2] | 92 | 
 | 
|---|
 | 93 | PairPotential_Morse::PairPotential_Morse(
 | 
|---|
| [ed2551] | 94 |     const ParticleTypes_t &_ParticleTypes,
 | 
|---|
| [155cc2] | 95 |     const double _spring_constant,
 | 
|---|
 | 96 |     const double _equilibrium_distance,
 | 
|---|
| [919c8a] | 97 |     const double _dissociation_energy) :
 | 
|---|
| [fdd23a] | 98 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| [ed2551] | 99 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| [155cc2] | 100 | {
 | 
|---|
 | 101 |   params[spring_constant] = _spring_constant;
 | 
|---|
 | 102 |   params[equilibrium_distance] = _equilibrium_distance;
 | 
|---|
 | 103 |   params[dissociation_energy] = _dissociation_energy;
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
| [086070] | 106 | void PairPotential_Morse::setParameters(const parameters_t &_params)
 | 
|---|
 | 107 | {
 | 
|---|
 | 108 |   const size_t paramsDim = _params.size();
 | 
|---|
 | 109 |   ASSERT( paramsDim <= getParameterDimension(),
 | 
|---|
 | 110 |       "PairPotential_Morse::setParameters() - we need not more than "
 | 
|---|
 | 111 |       +toString(getParameterDimension())+" parameters.");
 | 
|---|
 | 112 |   for(size_t i=0;i<paramsDim;++i)
 | 
|---|
 | 113 |     params[i] = _params[i];
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 | #ifndef NDEBUG
 | 
|---|
 | 116 |   parameters_t check_params(getParameters());
 | 
|---|
 | 117 |   check_params.resize(paramsDim); // truncate to same size
 | 
|---|
 | 118 |   ASSERT( check_params == _params,
 | 
|---|
 | 119 |       "PairPotential_Morse::setParameters() - failed, mismatch in to be set "
 | 
|---|
 | 120 |       +toString(_params)+" and set "+toString(check_params)+" params.");
 | 
|---|
 | 121 | #endif
 | 
|---|
 | 122 | }
 | 
|---|
 | 123 | 
 | 
|---|
| [155cc2] | 124 | PairPotential_Morse::results_t
 | 
|---|
 | 125 | PairPotential_Morse::operator()(
 | 
|---|
 | 126 |     const arguments_t &arguments
 | 
|---|
 | 127 |     ) const
 | 
|---|
 | 128 | {
 | 
|---|
 | 129 |   ASSERT( arguments.size() == 1,
 | 
|---|
 | 130 |       "PairPotential_Morse::operator() - requires exactly one argument.");
 | 
|---|
| [b760bc3] | 131 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 132 |       arguments, getParticleTypes()),
 | 
|---|
 | 133 |       "PairPotential_Morse::operator() - types don't match with ones in arguments.");
 | 
|---|
| [155cc2] | 134 |   const argument_t &r_ij = arguments[0];
 | 
|---|
| [b70b53] | 135 |   // Maple: f(r,D,k,R,c) := D * (1 - exp(-k*(r-R)))^(2)+c
 | 
|---|
| [155cc2] | 136 |   const result_t result =
 | 
|---|
 | 137 |       params[dissociation_energy] * Helpers::pow( 1.
 | 
|---|
| [919c8a] | 138 |           - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])),2);
 | 
|---|
| [155cc2] | 139 |   return std::vector<result_t>(1, result);
 | 
|---|
 | 140 | }
 | 
|---|
 | 141 | 
 | 
|---|
 | 142 | PairPotential_Morse::derivative_components_t
 | 
|---|
 | 143 | PairPotential_Morse::derivative(
 | 
|---|
 | 144 |     const arguments_t &arguments
 | 
|---|
 | 145 |     ) const
 | 
|---|
 | 146 | {
 | 
|---|
 | 147 |   ASSERT( arguments.size() == 1,
 | 
|---|
 | 148 |       "PairPotential_Morse::operator() - requires exactly one argument.");
 | 
|---|
| [b760bc3] | 149 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 150 |       arguments, getParticleTypes()),
 | 
|---|
 | 151 |       "PairPotential_Morse::operator() - types don't match with ones in arguments.");
 | 
|---|
| [155cc2] | 152 |   derivative_components_t result;
 | 
|---|
 | 153 |   const argument_t &r_ij = arguments[0];
 | 
|---|
| [b70b53] | 154 |   // Maple result: 2*D*(1-exp(-k*(r-R)))*k*exp(-k*(r-R))
 | 
|---|
| [155cc2] | 155 |   result.push_back(
 | 
|---|
 | 156 |       2. * params[dissociation_energy]
 | 
|---|
 | 157 |       * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])))
 | 
|---|
| [b70b53] | 158 |       * (- params[spring_constant]) * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]))
 | 
|---|
| [155cc2] | 159 |   );
 | 
|---|
 | 160 |   ASSERT( result.size() == 1,
 | 
|---|
 | 161 |       "PairPotential_Morse::operator() - we did not create exactly one component.");
 | 
|---|
 | 162 |   return result;
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 | PairPotential_Morse::results_t
 | 
|---|
 | 166 | PairPotential_Morse::parameter_derivative(
 | 
|---|
 | 167 |     const arguments_t &arguments,
 | 
|---|
 | 168 |     const size_t index
 | 
|---|
 | 169 |     ) const
 | 
|---|
 | 170 | {
 | 
|---|
 | 171 |   ASSERT( arguments.size() == 1,
 | 
|---|
 | 172 |       "PairPotential_Morse::parameter_derivative() - requires exactly one argument.");
 | 
|---|
| [b760bc3] | 173 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 174 |       arguments, getParticleTypes()),
 | 
|---|
 | 175 |       "PairPotential_Morse::operator() - types don't match with ones in arguments.");
 | 
|---|
| [155cc2] | 176 |   const argument_t &r_ij = arguments[0];
 | 
|---|
 | 177 |   switch (index) {
 | 
|---|
 | 178 |     case spring_constant:
 | 
|---|
 | 179 |     {
 | 
|---|
| [b70b53] | 180 |       // Maple result: -2*D*(1-exp(-k*(r-R)))*(-r+R)*exp(-k*(r-R))
 | 
|---|
| [155cc2] | 181 |       const result_t result =
 | 
|---|
| [b70b53] | 182 |           - 2. * params[dissociation_energy]
 | 
|---|
| [155cc2] | 183 |           * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])))
 | 
|---|
| [b70b53] | 184 |           * (- r_ij.distance + params[equilibrium_distance])
 | 
|---|
 | 185 |           * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]))
 | 
|---|
| [155cc2] | 186 |           ;
 | 
|---|
 | 187 |       return std::vector<result_t>(1, result);
 | 
|---|
 | 188 |       break;
 | 
|---|
 | 189 |     }
 | 
|---|
 | 190 |     case equilibrium_distance:
 | 
|---|
 | 191 |     {
 | 
|---|
| [b70b53] | 192 |       // Maple result: -2*D*(1-exp(-k*(r-R)))*k*exp(-k*(r-R))
 | 
|---|
| [155cc2] | 193 |       const result_t result =
 | 
|---|
| [b70b53] | 194 |           - 2. * params[dissociation_energy]
 | 
|---|
| [155cc2] | 195 |           * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])))
 | 
|---|
| [b70b53] | 196 |           * params[spring_constant] * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]))
 | 
|---|
| [155cc2] | 197 |           ;
 | 
|---|
 | 198 |       return std::vector<result_t>(1, result);
 | 
|---|
 | 199 |       break;
 | 
|---|
 | 200 |     }
 | 
|---|
 | 201 |     case dissociation_energy:
 | 
|---|
 | 202 |     {
 | 
|---|
| [b70b53] | 203 |       // Maple result: (1-exp(-k*(r-R)))^2
 | 
|---|
| [155cc2] | 204 |       const result_t result =
 | 
|---|
 | 205 |           Helpers::pow(1.
 | 
|---|
 | 206 |               - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])),2);
 | 
|---|
 | 207 |       return std::vector<result_t>(1, result);
 | 
|---|
 | 208 |       break;
 | 
|---|
 | 209 |     }
 | 
|---|
 | 210 |     default:
 | 
|---|
| [919c8a] | 211 |       ASSERT(0, "PairPotential_Morse::parameter_derivative() - derivative to unknown parameter desired.");
 | 
|---|
| [155cc2] | 212 |       break;
 | 
|---|
 | 213 |   }
 | 
|---|
| [76cbd0] | 214 |   return std::vector<result_t>(1, 0.);
 | 
|---|
| [155cc2] | 215 | }
 | 
|---|
| [7b019a] | 216 | 
 | 
|---|
 | 217 | FunctionModel::extractor_t
 | 
|---|
| [f0025d] | 218 | PairPotential_Morse::getSpecificExtractor() const
 | 
|---|
| [7b019a] | 219 | {
 | 
|---|
| [da2d5c] | 220 |   Fragment::charges_t charges;
 | 
|---|
 | 221 |   charges.resize(getParticleTypes().size());
 | 
|---|
 | 222 |   std::transform(getParticleTypes().begin(), getParticleTypes().end(),
 | 
|---|
 | 223 |       charges.begin(), boost::lambda::_1);
 | 
|---|
| [7b019a] | 224 |   FunctionModel::extractor_t returnfunction =
 | 
|---|
 | 225 |       boost::bind(&Extractors::gatherDistancesFromFragment,
 | 
|---|
 | 226 |           boost::bind(&Fragment::getPositions, _1),
 | 
|---|
 | 227 |           boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| [da2d5c] | 228 |           charges,
 | 
|---|
| [7b019a] | 229 |           _2);
 | 
|---|
 | 230 |   return returnfunction;
 | 
|---|
 | 231 | }
 | 
|---|
 | 232 | 
 | 
|---|
| [0f5d38] | 233 | FunctionModel::filter_t PairPotential_Morse::getSpecificFilter() const
 | 
|---|
 | 234 | {
 | 
|---|
 | 235 |   FunctionModel::filter_t returnfunction =
 | 
|---|
| [51e0e3] | 236 |       boost::bind(&Extractors::filterArgumentsByParticleTypes,
 | 
|---|
 | 237 |           _1,
 | 
|---|
 | 238 |           getParticleTypes());
 | 
|---|
| [0f5d38] | 239 |   return returnfunction;
 | 
|---|
 | 240 | }
 | 
|---|
 | 241 | 
 | 
|---|
| [d52819] | 242 | void
 | 
|---|
 | 243 | PairPotential_Morse::setParametersToRandomInitialValues(
 | 
|---|
 | 244 |     const TrainingData &data)
 | 
|---|
 | 245 | {
 | 
|---|
| [0932c2] | 246 |   RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
 | 
|---|
 | 247 |   const double rng_min = random.min();
 | 
|---|
 | 248 |   const double rng_max = random.max();
 | 
|---|
 | 249 |   params[PairPotential_Morse::dissociation_energy] = 1e+0*(random()/(rng_max-rng_min));// 0.5;
 | 
|---|
 | 250 |   params[PairPotential_Morse::spring_constant] = 1e+0*(random()/(rng_max-rng_min));// 1.;
 | 
|---|
 | 251 |   params[PairPotential_Morse::equilibrium_distance] =  3e+0*(random()/(rng_max-rng_min));//2.9;
 | 
|---|
| [d52819] | 252 | }
 | 
|---|
 | 253 | 
 | 
|---|