| [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" | 
|---|
| [155cc2] | 55 |  | 
|---|
| [7b019a] | 56 | class Fragment; | 
|---|
|  | 57 |  | 
|---|
| [ed2551] | 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"); | 
|---|
| [94453f1] | 67 | Coordinator::ptr PairPotential_Morse::coordinator(new TwoBody_Length()); | 
|---|
| [ed2551] | 68 |  | 
|---|
| [a82d33] | 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 |  | 
|---|
| [ed2551] | 79 | PairPotential_Morse::PairPotential_Morse( | 
|---|
|  | 80 | const ParticleTypes_t &_ParticleTypes | 
|---|
|  | 81 | ) : | 
|---|
| [fdd23a] | 82 | EmpiricalPotential(_ParticleTypes), | 
|---|
| [155cc2] | 83 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| [dbf8c8] | 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 | } | 
|---|
| [155cc2] | 90 |  | 
|---|
|  | 91 | PairPotential_Morse::PairPotential_Morse( | 
|---|
| [ed2551] | 92 | const ParticleTypes_t &_ParticleTypes, | 
|---|
| [155cc2] | 93 | const double _spring_constant, | 
|---|
|  | 94 | const double _equilibrium_distance, | 
|---|
| [919c8a] | 95 | const double _dissociation_energy) : | 
|---|
| [fdd23a] | 96 | EmpiricalPotential(_ParticleTypes), | 
|---|
| [ed2551] | 97 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| [155cc2] | 98 | { | 
|---|
|  | 99 | params[spring_constant] = _spring_constant; | 
|---|
|  | 100 | params[equilibrium_distance] = _equilibrium_distance; | 
|---|
|  | 101 | params[dissociation_energy] = _dissociation_energy; | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
| [086070] | 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 |  | 
|---|
| [155cc2] | 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."); | 
|---|
| [b760bc3] | 129 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
|  | 130 | arguments, getParticleTypes()), | 
|---|
|  | 131 | "PairPotential_Morse::operator() - types don't match with ones in arguments."); | 
|---|
| [155cc2] | 132 | const argument_t &r_ij = arguments[0]; | 
|---|
| [b70b53] | 133 | // Maple: f(r,D,k,R,c) := D * (1 - exp(-k*(r-R)))^(2)+c | 
|---|
| [155cc2] | 134 | const result_t result = | 
|---|
|  | 135 | params[dissociation_energy] * Helpers::pow( 1. | 
|---|
| [919c8a] | 136 | - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])),2); | 
|---|
| [155cc2] | 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."); | 
|---|
| [b760bc3] | 147 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
|  | 148 | arguments, getParticleTypes()), | 
|---|
|  | 149 | "PairPotential_Morse::operator() - types don't match with ones in arguments."); | 
|---|
| [155cc2] | 150 | derivative_components_t result; | 
|---|
|  | 151 | const argument_t &r_ij = arguments[0]; | 
|---|
| [b70b53] | 152 | // Maple result: 2*D*(1-exp(-k*(r-R)))*k*exp(-k*(r-R)) | 
|---|
| [155cc2] | 153 | result.push_back( | 
|---|
|  | 154 | 2. * params[dissociation_energy] | 
|---|
|  | 155 | * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]))) | 
|---|
| [b70b53] | 156 | * (- params[spring_constant]) * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])) | 
|---|
| [155cc2] | 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."); | 
|---|
| [b760bc3] | 171 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
|  | 172 | arguments, getParticleTypes()), | 
|---|
|  | 173 | "PairPotential_Morse::operator() - types don't match with ones in arguments."); | 
|---|
| [155cc2] | 174 | const argument_t &r_ij = arguments[0]; | 
|---|
|  | 175 | switch (index) { | 
|---|
|  | 176 | case spring_constant: | 
|---|
|  | 177 | { | 
|---|
| [b70b53] | 178 | // Maple result: -2*D*(1-exp(-k*(r-R)))*(-r+R)*exp(-k*(r-R)) | 
|---|
| [155cc2] | 179 | const result_t result = | 
|---|
| [b70b53] | 180 | - 2. * params[dissociation_energy] | 
|---|
| [155cc2] | 181 | * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]))) | 
|---|
| [b70b53] | 182 | * (- r_ij.distance + params[equilibrium_distance]) | 
|---|
|  | 183 | * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])) | 
|---|
| [155cc2] | 184 | ; | 
|---|
|  | 185 | return std::vector<result_t>(1, result); | 
|---|
|  | 186 | break; | 
|---|
|  | 187 | } | 
|---|
|  | 188 | case equilibrium_distance: | 
|---|
|  | 189 | { | 
|---|
| [b70b53] | 190 | // Maple result: -2*D*(1-exp(-k*(r-R)))*k*exp(-k*(r-R)) | 
|---|
| [155cc2] | 191 | const result_t result = | 
|---|
| [b70b53] | 192 | - 2. * params[dissociation_energy] | 
|---|
| [155cc2] | 193 | * ( 1. - exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]))) | 
|---|
| [b70b53] | 194 | * params[spring_constant] * exp( - params[spring_constant] * ( r_ij.distance - params[equilibrium_distance])) | 
|---|
| [155cc2] | 195 | ; | 
|---|
|  | 196 | return std::vector<result_t>(1, result); | 
|---|
|  | 197 | break; | 
|---|
|  | 198 | } | 
|---|
|  | 199 | case dissociation_energy: | 
|---|
|  | 200 | { | 
|---|
| [b70b53] | 201 | // Maple result: (1-exp(-k*(r-R)))^2 | 
|---|
| [155cc2] | 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: | 
|---|
| [919c8a] | 209 | ASSERT(0, "PairPotential_Morse::parameter_derivative() - derivative to unknown parameter desired."); | 
|---|
| [155cc2] | 210 | break; | 
|---|
|  | 211 | } | 
|---|
| [76cbd0] | 212 | return std::vector<result_t>(1, 0.); | 
|---|
| [155cc2] | 213 | } | 
|---|
| [7b019a] | 214 |  | 
|---|
|  | 215 | FunctionModel::extractor_t | 
|---|
| [f0025d] | 216 | PairPotential_Morse::getSpecificExtractor() const | 
|---|
| [7b019a] | 217 | { | 
|---|
| [da2d5c] | 218 | Fragment::charges_t charges; | 
|---|
|  | 219 | charges.resize(getParticleTypes().size()); | 
|---|
|  | 220 | std::transform(getParticleTypes().begin(), getParticleTypes().end(), | 
|---|
|  | 221 | charges.begin(), boost::lambda::_1); | 
|---|
| [7b019a] | 222 | FunctionModel::extractor_t returnfunction = | 
|---|
|  | 223 | boost::bind(&Extractors::gatherDistancesFromFragment, | 
|---|
|  | 224 | boost::bind(&Fragment::getPositions, _1), | 
|---|
|  | 225 | boost::bind(&Fragment::getCharges, _1), | 
|---|
| [da2d5c] | 226 | charges, | 
|---|
| [7b019a] | 227 | _2); | 
|---|
|  | 228 | return returnfunction; | 
|---|
|  | 229 | } | 
|---|
|  | 230 |  | 
|---|
| [0f5d38] | 231 | FunctionModel::filter_t PairPotential_Morse::getSpecificFilter() const | 
|---|
|  | 232 | { | 
|---|
|  | 233 | FunctionModel::filter_t returnfunction = | 
|---|
| [51e0e3] | 234 | boost::bind(&Extractors::filterArgumentsByParticleTypes, | 
|---|
|  | 235 | _1, | 
|---|
|  | 236 | getParticleTypes()); | 
|---|
| [0f5d38] | 237 | return returnfunction; | 
|---|
|  | 238 | } | 
|---|
|  | 239 |  | 
|---|
| [d52819] | 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 |  | 
|---|