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