| [a63187] | 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. | 
|---|
| [a63187] | 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 | /* | 
|---|
| [484e2a] | 26 | * ThreeBodyPotential_Angle.cpp | 
|---|
| [a63187] | 27 | * | 
|---|
|  | 28 | *  Created on: Oct 11, 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 |  | 
|---|
| [484e2a] | 40 | #include "ThreeBodyPotential_Angle.hpp" | 
|---|
| [a63187] | 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> | 
|---|
| [ed2551] | 45 | #include <string> | 
|---|
|  | 46 |  | 
|---|
| [a63187] | 47 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 48 |  | 
|---|
| [7b019a] | 49 | #include "FunctionApproximation/Extractors.hpp" | 
|---|
| [d52819] | 50 | #include "FunctionApproximation/TrainingData.hpp" | 
|---|
| [a63187] | 51 | #include "Potentials/helpers.hpp" | 
|---|
| [94453f1] | 52 | #include "Potentials/InternalCoordinates/ThreeBody_Angle.hpp" | 
|---|
| [b760bc3] | 53 | #include "Potentials/ParticleTypeCheckers.hpp" | 
|---|
| [0932c2] | 54 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp" | 
|---|
|  | 55 | #include "RandomNumbers/RandomNumberGenerator.hpp" | 
|---|
| [a63187] | 56 |  | 
|---|
| [7b019a] | 57 | class Fragment; | 
|---|
|  | 58 |  | 
|---|
| [ed2551] | 59 | // static definitions | 
|---|
| [484e2a] | 60 | const ThreeBodyPotential_Angle::ParameterNames_t | 
|---|
|  | 61 | ThreeBodyPotential_Angle::ParameterNames = | 
|---|
| [ed2551] | 62 | boost::assign::list_of<std::string> | 
|---|
|  | 63 | ("spring_constant") | 
|---|
|  | 64 | ("equilibrium_distance") | 
|---|
|  | 65 | ; | 
|---|
| [484e2a] | 66 | const std::string ThreeBodyPotential_Angle::potential_token("harmonic_angle"); | 
|---|
| [94453f1] | 67 | Coordinator::ptr ThreeBodyPotential_Angle::coordinator(new ThreeBody_Angle()); | 
|---|
| [ed2551] | 68 |  | 
|---|
| [484e2a] | 69 | ThreeBodyPotential_Angle::ThreeBodyPotential_Angle() : | 
|---|
| [a82d33] | 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] = 0.1; | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
| [484e2a] | 78 | ThreeBodyPotential_Angle::ThreeBodyPotential_Angle( | 
|---|
| [ed2551] | 79 | const ParticleTypes_t &_ParticleTypes | 
|---|
|  | 80 | ) : | 
|---|
| [fdd23a] | 81 | EmpiricalPotential(_ParticleTypes), | 
|---|
| [a63187] | 82 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| [dbf8c8] | 83 | { | 
|---|
|  | 84 | // have some decent defaults for parameter_derivative checking | 
|---|
|  | 85 | params[spring_constant] = 1.; | 
|---|
|  | 86 | params[equilibrium_distance] = 0.1; | 
|---|
|  | 87 | } | 
|---|
| [a63187] | 88 |  | 
|---|
| [484e2a] | 89 | ThreeBodyPotential_Angle::ThreeBodyPotential_Angle( | 
|---|
| [ed2551] | 90 | const ParticleTypes_t &_ParticleTypes, | 
|---|
| [a63187] | 91 | const double _spring_constant, | 
|---|
| [1e242a] | 92 | const double _equilibrium_distance) : | 
|---|
| [fdd23a] | 93 | EmpiricalPotential(_ParticleTypes), | 
|---|
| [ed2551] | 94 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| [a63187] | 95 | { | 
|---|
|  | 96 | params[spring_constant] = _spring_constant; | 
|---|
|  | 97 | params[equilibrium_distance] = _equilibrium_distance; | 
|---|
|  | 98 | } | 
|---|
|  | 99 |  | 
|---|
| [484e2a] | 100 | void ThreeBodyPotential_Angle::setParameters(const parameters_t &_params) | 
|---|
| [086070] | 101 | { | 
|---|
|  | 102 | const size_t paramsDim = _params.size(); | 
|---|
|  | 103 | ASSERT( paramsDim <= getParameterDimension(), | 
|---|
| [484e2a] | 104 | "ThreeBodyPotential_Angle::setParameters() - we need not more than " | 
|---|
| [086070] | 105 | +toString(getParameterDimension())+" parameters."); | 
|---|
|  | 106 | for(size_t i=0;i<paramsDim;++i) | 
|---|
|  | 107 | params[i] = _params[i]; | 
|---|
|  | 108 |  | 
|---|
|  | 109 | #ifndef NDEBUG | 
|---|
|  | 110 | parameters_t check_params(getParameters()); | 
|---|
|  | 111 | check_params.resize(paramsDim); // truncate to same size | 
|---|
|  | 112 | ASSERT( check_params == _params, | 
|---|
| [484e2a] | 113 | "ThreeBodyPotential_Angle::setParameters() - failed, mismatch in to be set " | 
|---|
| [086070] | 114 | +toString(_params)+" and set "+toString(check_params)+" params."); | 
|---|
|  | 115 | #endif | 
|---|
|  | 116 | } | 
|---|
|  | 117 |  | 
|---|
| [484e2a] | 118 | ThreeBodyPotential_Angle::result_t | 
|---|
|  | 119 | ThreeBodyPotential_Angle::function_theta( | 
|---|
| [a63187] | 120 | const double &r_ij, | 
|---|
| [bbc422] | 121 | const double &r_jk, | 
|---|
|  | 122 | const double &r_ik | 
|---|
| [a63187] | 123 | ) const | 
|---|
|  | 124 | { | 
|---|
|  | 125 | //  Info info(__func__); | 
|---|
| [bbc422] | 126 | const double angle = Helpers::pow(r_ij,2) + Helpers::pow(r_jk,2) - Helpers::pow(r_ik,2); | 
|---|
|  | 127 | const double divisor = 2.* r_ij * r_jk; | 
|---|
| [a63187] | 128 |  | 
|---|
|  | 129 | //  LOG(2, "DEBUG: cos(theta)= " << angle/divisor); | 
|---|
|  | 130 | if (divisor == 0.) | 
|---|
|  | 131 | return 0.; | 
|---|
|  | 132 | else | 
|---|
|  | 133 | return angle/divisor; | 
|---|
|  | 134 | } | 
|---|
|  | 135 |  | 
|---|
| [484e2a] | 136 | ThreeBodyPotential_Angle::results_t | 
|---|
|  | 137 | ThreeBodyPotential_Angle::operator()( | 
|---|
| [a63187] | 138 | const arguments_t &arguments | 
|---|
|  | 139 | ) const | 
|---|
|  | 140 | { | 
|---|
|  | 141 | ASSERT( arguments.size() == 3, | 
|---|
| [484e2a] | 142 | "ThreeBodyPotential_Angle::operator() - requires exactly three arguments."); | 
|---|
| [bbc422] | 143 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
| [b760bc3] | 144 | arguments, getParticleTypes()), | 
|---|
| [484e2a] | 145 | "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments."); | 
|---|
| [dbcc47] | 146 | const argument_t &r_ij = arguments[0]; // 01 | 
|---|
| [bbc422] | 147 | const argument_t &r_jk = arguments[2]; // 12 | 
|---|
|  | 148 | const argument_t &r_ik = arguments[1]; // 02 | 
|---|
| [a63187] | 149 | const result_t result = | 
|---|
|  | 150 | params[spring_constant] | 
|---|
| [1e242a] | 151 | * Helpers::pow( function_theta(r_ij.distance, r_jk.distance, r_ik.distance) - params[equilibrium_distance], 2 ); | 
|---|
| [a63187] | 152 | return std::vector<result_t>(1, result); | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
| [484e2a] | 155 | ThreeBodyPotential_Angle::derivative_components_t | 
|---|
|  | 156 | ThreeBodyPotential_Angle::derivative( | 
|---|
| [a63187] | 157 | const arguments_t &arguments | 
|---|
|  | 158 | ) const | 
|---|
|  | 159 | { | 
|---|
|  | 160 | ASSERT( arguments.size() == 3, | 
|---|
| [484e2a] | 161 | "ThreeBodyPotential_Angle::operator() - requires exactly three arguments."); | 
|---|
| [bbc422] | 162 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
| [b760bc3] | 163 | arguments, getParticleTypes()), | 
|---|
| [484e2a] | 164 | "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments."); | 
|---|
| [a63187] | 165 | derivative_components_t result; | 
|---|
| [dbcc47] | 166 | const argument_t &r_ij = arguments[0]; //01 | 
|---|
| [bbc422] | 167 | const argument_t &r_jk = arguments[2]; //12 | 
|---|
|  | 168 | const argument_t &r_ik = arguments[1]; //02 | 
|---|
|  | 169 | result.push_back( 2. * params[spring_constant] * ( function_theta(r_ij.distance, r_jk.distance, r_ik.distance) - params[equilibrium_distance]) ); | 
|---|
| [a63187] | 170 | ASSERT( result.size() == 1, | 
|---|
| [484e2a] | 171 | "ThreeBodyPotential_Angle::operator() - we did not create exactly one component."); | 
|---|
| [a63187] | 172 | return result; | 
|---|
|  | 173 | } | 
|---|
|  | 174 |  | 
|---|
| [484e2a] | 175 | ThreeBodyPotential_Angle::results_t | 
|---|
|  | 176 | ThreeBodyPotential_Angle::parameter_derivative( | 
|---|
| [a63187] | 177 | const arguments_t &arguments, | 
|---|
|  | 178 | const size_t index | 
|---|
|  | 179 | ) const | 
|---|
|  | 180 | { | 
|---|
|  | 181 | ASSERT( arguments.size() == 3, | 
|---|
| [484e2a] | 182 | "ThreeBodyPotential_Angle::parameter_derivative() - requires exactly three arguments."); | 
|---|
| [bbc422] | 183 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
| [b760bc3] | 184 | arguments, getParticleTypes()), | 
|---|
| [484e2a] | 185 | "ThreeBodyPotential_Angle::operator() - types don't match with ones in arguments."); | 
|---|
| [dbcc47] | 186 | const argument_t &r_ij = arguments[0]; //01 | 
|---|
| [bbc422] | 187 | const argument_t &r_jk = arguments[2]; //12 | 
|---|
|  | 188 | const argument_t &r_ik = arguments[1]; //02 | 
|---|
| [a63187] | 189 | switch (index) { | 
|---|
|  | 190 | case spring_constant: | 
|---|
|  | 191 | { | 
|---|
|  | 192 | const result_t result = | 
|---|
| [bbc422] | 193 | Helpers::pow( function_theta(r_ij.distance, r_jk.distance, r_ik.distance) - params[equilibrium_distance], 2 ); | 
|---|
| [a63187] | 194 | return std::vector<result_t>(1, result); | 
|---|
|  | 195 | break; | 
|---|
|  | 196 | } | 
|---|
|  | 197 | case equilibrium_distance: | 
|---|
|  | 198 | { | 
|---|
|  | 199 | const result_t result = | 
|---|
|  | 200 | -2. * params[spring_constant] | 
|---|
| [bbc422] | 201 | * ( function_theta(r_ij.distance, r_jk.distance, r_ik.distance) - params[equilibrium_distance]); | 
|---|
| [a63187] | 202 | return std::vector<result_t>(1, result); | 
|---|
|  | 203 | break; | 
|---|
|  | 204 | } | 
|---|
|  | 205 | default: | 
|---|
| [484e2a] | 206 | ASSERT(0, "ThreeBodyPotential_Angle::parameter_derivative() - derivative to unknown parameter desired."); | 
|---|
| [a63187] | 207 | break; | 
|---|
|  | 208 | } | 
|---|
| [a2a2f7] | 209 | return std::vector<result_t>(1); | 
|---|
| [a63187] | 210 | } | 
|---|
| [7b019a] | 211 |  | 
|---|
|  | 212 | FunctionModel::extractor_t | 
|---|
| [484e2a] | 213 | ThreeBodyPotential_Angle::getSpecificExtractor() const | 
|---|
| [7b019a] | 214 | { | 
|---|
| [da2d5c] | 215 | Fragment::charges_t charges; | 
|---|
|  | 216 | charges.resize(getParticleTypes().size()); | 
|---|
|  | 217 | std::transform(getParticleTypes().begin(), getParticleTypes().end(), | 
|---|
|  | 218 | charges.begin(), boost::lambda::_1); | 
|---|
| [7b019a] | 219 | FunctionModel::extractor_t returnfunction = | 
|---|
|  | 220 | boost::bind(&Extractors::gatherDistancesFromFragment, | 
|---|
|  | 221 | boost::bind(&Fragment::getPositions, _1), | 
|---|
|  | 222 | boost::bind(&Fragment::getCharges, _1), | 
|---|
| [da2d5c] | 223 | charges, | 
|---|
| [7b019a] | 224 | _2); | 
|---|
|  | 225 | return returnfunction; | 
|---|
|  | 226 | } | 
|---|
|  | 227 |  | 
|---|
| [484e2a] | 228 | FunctionModel::filter_t ThreeBodyPotential_Angle::getSpecificFilter() const | 
|---|
| [0f5d38] | 229 | { | 
|---|
|  | 230 | FunctionModel::filter_t returnfunction = | 
|---|
|  | 231 | boost::bind(&Extractors::reorderArgumentsByParticleTypes, | 
|---|
| [51e0e3] | 232 | boost::bind(&Extractors::filterArgumentsByParticleTypes, | 
|---|
|  | 233 | _1, | 
|---|
|  | 234 | getParticleTypes()), | 
|---|
|  | 235 | getParticleTypes() | 
|---|
| [0f5d38] | 236 | ); | 
|---|
|  | 237 | return returnfunction; | 
|---|
|  | 238 | } | 
|---|
|  | 239 |  | 
|---|
| [d52819] | 240 | void | 
|---|
| [484e2a] | 241 | ThreeBodyPotential_Angle::setParametersToRandomInitialValues( | 
|---|
| [d52819] | 242 | const TrainingData &data) | 
|---|
|  | 243 | { | 
|---|
| [0932c2] | 244 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator(); | 
|---|
|  | 245 | const double rng_min = random.min(); | 
|---|
|  | 246 | const double rng_max = random.max(); | 
|---|
|  | 247 | params[ThreeBodyPotential_Angle::spring_constant] = 1e+0*(random()/(rng_max-rng_min));// 0.2; | 
|---|
|  | 248 | params[ThreeBodyPotential_Angle::equilibrium_distance] = -0.3;//2e+0*(random()/(rng_max-rng_min)) - 1.;// 1.; | 
|---|
| [d52819] | 249 | } | 
|---|
|  | 250 |  | 
|---|