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