| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 5 | * Please see the COPYING file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | * | 
|---|
| 7 | * | 
|---|
| 8 | *   This file is part of MoleCuilder. | 
|---|
| 9 | * | 
|---|
| 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 13 | *    (at your option) any later version. | 
|---|
| 14 | * | 
|---|
| 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | *    GNU General Public License for more details. | 
|---|
| 19 | * | 
|---|
| 20 | *    You should have received a copy of the GNU General Public License | 
|---|
| 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | /* | 
|---|
| 25 | * FourBodyPotential_Torsion.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: Jul 08, 2013 | 
|---|
| 28 | *      Author: heber | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | // include config.h | 
|---|
| 33 | #ifdef HAVE_CONFIG_H | 
|---|
| 34 | #include <config.h> | 
|---|
| 35 | #endif | 
|---|
| 36 |  | 
|---|
| 37 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include "FourBodyPotential_Torsion.hpp" | 
|---|
| 40 |  | 
|---|
| 41 | #include <boost/assign/list_of.hpp> // for 'map_list_of()' | 
|---|
| 42 | #include <boost/bind.hpp> | 
|---|
| 43 | #include <boost/lambda/lambda.hpp> | 
|---|
| 44 | #include <string> | 
|---|
| 45 |  | 
|---|
| 46 | #include "CodePatterns/Assert.hpp" | 
|---|
| 47 |  | 
|---|
| 48 | #include "FunctionApproximation/Extractors.hpp" | 
|---|
| 49 | #include "FunctionApproximation/TrainingData.hpp" | 
|---|
| 50 | #include "Potentials/helpers.hpp" | 
|---|
| 51 | #include "Potentials/InternalCoordinates/FourBody_TorsionAngle.hpp" | 
|---|
| 52 | #include "Potentials/ParticleTypeCheckers.hpp" | 
|---|
| 53 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp" | 
|---|
| 54 | #include "RandomNumbers/RandomNumberGenerator.hpp" | 
|---|
| 55 |  | 
|---|
| 56 | class Fragment; | 
|---|
| 57 |  | 
|---|
| 58 | // static definitions1 | 
|---|
| 59 | const FourBodyPotential_Torsion::ParameterNames_t | 
|---|
| 60 | FourBodyPotential_Torsion::ParameterNames = | 
|---|
| 61 | boost::assign::list_of<std::string> | 
|---|
| 62 | ("spring_constant") | 
|---|
| 63 | ("equilibrium_distance") | 
|---|
| 64 | ; | 
|---|
| 65 | const std::string FourBodyPotential_Torsion::potential_token("torsion"); | 
|---|
| 66 | Coordinator::ptr FourBodyPotential_Torsion::coordinator(new FourBody_TorsionAngle()); | 
|---|
| 67 |  | 
|---|
| 68 | FourBodyPotential_Torsion::FourBodyPotential_Torsion() : | 
|---|
| 69 | EmpiricalPotential(), | 
|---|
| 70 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| 71 | { | 
|---|
| 72 | // have some decent defaults for parameter_derivative checking | 
|---|
| 73 | params[spring_constant] = 1.; | 
|---|
| 74 | params[equilibrium_distance] = 0.1; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | FourBodyPotential_Torsion::FourBodyPotential_Torsion( | 
|---|
| 78 | const ParticleTypes_t &_ParticleTypes | 
|---|
| 79 | ) : | 
|---|
| 80 | EmpiricalPotential(_ParticleTypes), | 
|---|
| 81 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| 82 | { | 
|---|
| 83 | // have some decent defaults for parameter_derivative checking | 
|---|
| 84 | params[spring_constant] = 1.; | 
|---|
| 85 | params[equilibrium_distance] = 0.1; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | FourBodyPotential_Torsion::FourBodyPotential_Torsion( | 
|---|
| 89 | const ParticleTypes_t &_ParticleTypes, | 
|---|
| 90 | const double _spring_constant, | 
|---|
| 91 | const double _equilibrium_distance) : | 
|---|
| 92 | EmpiricalPotential(_ParticleTypes), | 
|---|
| 93 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| 94 | { | 
|---|
| 95 | params[spring_constant] = _spring_constant; | 
|---|
| 96 | params[equilibrium_distance] = _equilibrium_distance; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void FourBodyPotential_Torsion::setParameters(const parameters_t &_params) | 
|---|
| 100 | { | 
|---|
| 101 | const size_t paramsDim = _params.size(); | 
|---|
| 102 | ASSERT( paramsDim <= getParameterDimension(), | 
|---|
| 103 | "FourBodyPotential_Torsion::setParameters() - we need not more than " | 
|---|
| 104 | +toString(getParameterDimension())+" parameters."); | 
|---|
| 105 | for(size_t i=0;i<paramsDim;++i) | 
|---|
| 106 | params[i] = _params[i]; | 
|---|
| 107 |  | 
|---|
| 108 | #ifndef NDEBUG | 
|---|
| 109 | parameters_t check_params(getParameters()); | 
|---|
| 110 | check_params.resize(paramsDim); // truncate to same size | 
|---|
| 111 | ASSERT( check_params == _params, | 
|---|
| 112 | "FourBodyPotential_Torsion::setParameters() - failed, mismatch in to be set " | 
|---|
| 113 | +toString(_params)+" and set "+toString(check_params)+" params."); | 
|---|
| 114 | #endif | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | FourBodyPotential_Torsion::result_t | 
|---|
| 118 | FourBodyPotential_Torsion::function_theta( | 
|---|
| 119 | const double &r_ij, | 
|---|
| 120 | const double &r_ik, | 
|---|
| 121 | const double &r_il, | 
|---|
| 122 | const double &r_jk, | 
|---|
| 123 | const double &r_jl, | 
|---|
| 124 | const double &r_kl | 
|---|
| 125 | ) const | 
|---|
| 126 | { | 
|---|
| 127 | //  Info info(__func__); | 
|---|
| 128 | const double h_1 = .5*sqrt(2.*(Helpers::pow(r_ij,2)+Helpers::pow(r_ik,2))-Helpers::pow(r_jk,2)); | 
|---|
| 129 | const double h_2 = .5*sqrt(2.*(Helpers::pow(r_jl,2)+Helpers::pow(r_kl,2))-Helpers::pow(r_jk,2)); | 
|---|
| 130 | const double angle = Helpers::pow(h_1,2) + Helpers::pow(h_2,2) - Helpers::pow(r_il,2); | 
|---|
| 131 | const double divisor = 2.* h_1 * h_2; | 
|---|
| 132 |  | 
|---|
| 133 | //  LOG(2, "DEBUG: cos(theta)= " << angle/divisor); | 
|---|
| 134 | if (divisor == 0.) | 
|---|
| 135 | return 0.; | 
|---|
| 136 | else | 
|---|
| 137 | return angle/divisor; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | FourBodyPotential_Torsion::results_t | 
|---|
| 141 | FourBodyPotential_Torsion::operator()( | 
|---|
| 142 | const list_of_arguments_t &listarguments | 
|---|
| 143 | ) const | 
|---|
| 144 | { | 
|---|
| 145 | result_t result = 0.; | 
|---|
| 146 | for(list_of_arguments_t::const_iterator iter = listarguments.begin(); | 
|---|
| 147 | iter != listarguments.end(); ++iter) { | 
|---|
| 148 | const arguments_t &arguments = *iter; | 
|---|
| 149 | ASSERT( arguments.size() == getSpecificArgumentCount(), | 
|---|
| 150 | "FourBodyPotential_Torsion::operator() - requires exactly three arguments."); | 
|---|
| 151 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
| 152 | arguments, getParticleTypes()), | 
|---|
| 153 | "FourBodyPotential_Torsion::operator() - types don't match with ones in arguments."); | 
|---|
| 154 | const argument_t &r_ij = arguments[0]; // 01 | 
|---|
| 155 | const argument_t &r_ik = arguments[1]; // 02 | 
|---|
| 156 | const argument_t &r_il = arguments[2]; // 03 | 
|---|
| 157 | const argument_t &r_jk = arguments[3]; // 12 | 
|---|
| 158 | const argument_t &r_jl = arguments[4]; // 13 | 
|---|
| 159 | const argument_t &r_kl = arguments[5]; // 23 | 
|---|
| 160 | result += | 
|---|
| 161 | params[spring_constant] | 
|---|
| 162 | * Helpers::pow( function_theta(r_ij.distance, r_ik.distance, r_il.distance, r_jk.distance, r_jl.distance, r_kl.distance) | 
|---|
| 163 | - params[equilibrium_distance], 2 ); | 
|---|
| 164 | } | 
|---|
| 165 | return results_t(1, result); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | FourBodyPotential_Torsion::derivative_components_t | 
|---|
| 169 | FourBodyPotential_Torsion::derivative( | 
|---|
| 170 | const list_of_arguments_t &listarguments | 
|---|
| 171 | ) const | 
|---|
| 172 | { | 
|---|
| 173 | result_t result = 0.; | 
|---|
| 174 | for(list_of_arguments_t::const_iterator iter = listarguments.begin(); | 
|---|
| 175 | iter != listarguments.end(); ++iter) { | 
|---|
| 176 | const arguments_t &arguments = *iter; | 
|---|
| 177 | ASSERT( arguments.size() == getSpecificArgumentCount(), | 
|---|
| 178 | "FourBodyPotential_Torsion::operator() - requires exactly three arguments."); | 
|---|
| 179 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
| 180 | arguments, getParticleTypes()), | 
|---|
| 181 | "FourBodyPotential_Torsion::operator() - types don't match with ones in arguments."); | 
|---|
| 182 | const argument_t &r_ij = arguments[0]; // 01 | 
|---|
| 183 | const argument_t &r_ik = arguments[1]; // 02 | 
|---|
| 184 | const argument_t &r_il = arguments[2]; // 03 | 
|---|
| 185 | const argument_t &r_jk = arguments[3]; // 12 | 
|---|
| 186 | const argument_t &r_jl = arguments[4]; // 13 | 
|---|
| 187 | const argument_t &r_kl = arguments[5]; // 23 | 
|---|
| 188 | result += | 
|---|
| 189 | 2. * params[spring_constant] * | 
|---|
| 190 | ( function_theta(r_ij.distance, r_ik.distance, r_il.distance, r_jk.distance, r_jl.distance, r_kl.distance) | 
|---|
| 191 | - params[equilibrium_distance]); | 
|---|
| 192 | } | 
|---|
| 193 | return derivative_components_t(1, result); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | FourBodyPotential_Torsion::results_t | 
|---|
| 197 | FourBodyPotential_Torsion::parameter_derivative( | 
|---|
| 198 | const list_of_arguments_t &listarguments, | 
|---|
| 199 | const size_t index | 
|---|
| 200 | ) const | 
|---|
| 201 | { | 
|---|
| 202 | result_t result = 0.; | 
|---|
| 203 | for(list_of_arguments_t::const_iterator iter = listarguments.begin(); | 
|---|
| 204 | iter != listarguments.end(); ++iter) { | 
|---|
| 205 | const arguments_t &arguments = *iter; | 
|---|
| 206 | ASSERT( arguments.size() == getSpecificArgumentCount(), | 
|---|
| 207 | "FourBodyPotential_Torsion::parameter_derivative() - requires exactly three arguments."); | 
|---|
| 208 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
| 209 | arguments, getParticleTypes()), | 
|---|
| 210 | "FourBodyPotential_Torsion::operator() - types don't match with ones in arguments."); | 
|---|
| 211 | const argument_t &r_ij = arguments[0]; // 01 | 
|---|
| 212 | const argument_t &r_ik = arguments[1]; // 02 | 
|---|
| 213 | const argument_t &r_il = arguments[2]; // 03 | 
|---|
| 214 | const argument_t &r_jk = arguments[3]; // 12 | 
|---|
| 215 | const argument_t &r_jl = arguments[4]; // 13 | 
|---|
| 216 | const argument_t &r_kl = arguments[5]; // 23 | 
|---|
| 217 | switch (index) { | 
|---|
| 218 | case spring_constant: | 
|---|
| 219 | { | 
|---|
| 220 | result += | 
|---|
| 221 | Helpers::pow( function_theta(r_ij.distance, r_ik.distance, r_il.distance, r_jk.distance, r_jl.distance, r_kl.distance) - params[equilibrium_distance], 2 ); | 
|---|
| 222 | break; | 
|---|
| 223 | } | 
|---|
| 224 | case equilibrium_distance: | 
|---|
| 225 | { | 
|---|
| 226 | result += | 
|---|
| 227 | -2. * params[spring_constant] | 
|---|
| 228 | * ( function_theta(r_ij.distance, r_ik.distance, r_il.distance, r_jk.distance, r_jl.distance, r_kl.distance) - params[equilibrium_distance]); | 
|---|
| 229 | break; | 
|---|
| 230 | } | 
|---|
| 231 | default: | 
|---|
| 232 | ASSERT(0, "FourBodyPotential_Torsion::parameter_derivative() - derivative to unknown parameter desired."); | 
|---|
| 233 | break; | 
|---|
| 234 | } | 
|---|
| 235 | } | 
|---|
| 236 | return results_t(1, result); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | FunctionModel::filter_t | 
|---|
| 240 | FourBodyPotential_Torsion::getSpecificFilter() const | 
|---|
| 241 | { | 
|---|
| 242 | FunctionModel::filter_t returnfunction = | 
|---|
| 243 | boost::bind(&Extractors::reorderArgumentsByParticleTypes, | 
|---|
| 244 | boost::bind(&Extractors::filterArgumentsByParticleTypes, | 
|---|
| 245 | _1, | 
|---|
| 246 | getParticleTypes()), | 
|---|
| 247 | getParticleTypes() | 
|---|
| 248 | ); | 
|---|
| 249 | return returnfunction; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | void | 
|---|
| 253 | FourBodyPotential_Torsion::setParametersToRandomInitialValues( | 
|---|
| 254 | const TrainingData &data) | 
|---|
| 255 | { | 
|---|
| 256 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator(); | 
|---|
| 257 | const double rng_min = random.min(); | 
|---|
| 258 | const double rng_max = random.max(); | 
|---|
| 259 | params[FourBodyPotential_Torsion::spring_constant] = 2.*(random()/(rng_max-rng_min)); | 
|---|
| 260 | params[FourBodyPotential_Torsion::equilibrium_distance] = 2.*(random()/(rng_max-rng_min)); | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|