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