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