| [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"
 | 
|---|
| [0932c2] | 53 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
 | 
|---|
 | 54 | #include "RandomNumbers/RandomNumberGenerator.hpp"
 | 
|---|
| [e2037e] | 55 | 
 | 
|---|
 | 56 | class Fragment;
 | 
|---|
 | 57 | 
 | 
|---|
| [0932c2] | 58 | // static definitions1
 | 
|---|
| [e2037e] | 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");
 | 
|---|
| [94453f1] | 66 | Coordinator::ptr FourBodyPotential_Torsion::coordinator(new FourBody_TorsionAngle());
 | 
|---|
| [e2037e] | 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 arguments_t &arguments
 | 
|---|
 | 143 |     ) const
 | 
|---|
 | 144 | {
 | 
|---|
 | 145 |   ASSERT( arguments.size() == getSpecificArgumentCount(),
 | 
|---|
 | 146 |       "FourBodyPotential_Torsion::operator() - requires exactly three arguments.");
 | 
|---|
 | 147 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 148 |       arguments, getParticleTypes()),
 | 
|---|
 | 149 |       "FourBodyPotential_Torsion::operator() - types don't match with ones in arguments.");
 | 
|---|
 | 150 |   const argument_t &r_ij = arguments[0]; // 01
 | 
|---|
 | 151 |   const argument_t &r_ik = arguments[1]; // 02
 | 
|---|
 | 152 |   const argument_t &r_il = arguments[2]; // 03
 | 
|---|
 | 153 |   const argument_t &r_jk = arguments[3]; // 12
 | 
|---|
 | 154 |   const argument_t &r_jl = arguments[4]; // 13
 | 
|---|
 | 155 |   const argument_t &r_kl = arguments[5]; // 23
 | 
|---|
 | 156 |   const result_t result =
 | 
|---|
 | 157 |       params[spring_constant]
 | 
|---|
 | 158 |              * 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 );
 | 
|---|
 | 159 |   return std::vector<result_t>(1, result);
 | 
|---|
 | 160 | }
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 | FourBodyPotential_Torsion::derivative_components_t
 | 
|---|
 | 163 | FourBodyPotential_Torsion::derivative(
 | 
|---|
 | 164 |     const arguments_t &arguments
 | 
|---|
 | 165 |     ) const
 | 
|---|
 | 166 | {
 | 
|---|
 | 167 |   ASSERT( arguments.size() == getSpecificArgumentCount(),
 | 
|---|
 | 168 |       "FourBodyPotential_Torsion::operator() - requires exactly three arguments.");
 | 
|---|
 | 169 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 170 |       arguments, getParticleTypes()),
 | 
|---|
 | 171 |       "FourBodyPotential_Torsion::operator() - types don't match with ones in arguments.");
 | 
|---|
 | 172 |   derivative_components_t result;
 | 
|---|
 | 173 |   const argument_t &r_ij = arguments[0]; // 01
 | 
|---|
 | 174 |   const argument_t &r_ik = arguments[1]; // 02
 | 
|---|
 | 175 |   const argument_t &r_il = arguments[2]; // 03
 | 
|---|
 | 176 |   const argument_t &r_jk = arguments[3]; // 12
 | 
|---|
 | 177 |   const argument_t &r_jl = arguments[4]; // 13
 | 
|---|
 | 178 |   const argument_t &r_kl = arguments[5]; // 23
 | 
|---|
 | 179 |   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]) );
 | 
|---|
 | 180 |   ASSERT( result.size() == 1,
 | 
|---|
 | 181 |       "FourBodyPotential_Torsion::operator() - we did not create exactly one component.");
 | 
|---|
 | 182 |   return result;
 | 
|---|
 | 183 | }
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 | FourBodyPotential_Torsion::results_t
 | 
|---|
 | 186 | FourBodyPotential_Torsion::parameter_derivative(
 | 
|---|
 | 187 |     const arguments_t &arguments,
 | 
|---|
 | 188 |     const size_t index
 | 
|---|
 | 189 |     ) const
 | 
|---|
 | 190 | {
 | 
|---|
 | 191 |   ASSERT( arguments.size() == getSpecificArgumentCount(),
 | 
|---|
 | 192 |       "FourBodyPotential_Torsion::parameter_derivative() - requires exactly three arguments.");
 | 
|---|
 | 193 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
 | 194 |       arguments, getParticleTypes()),
 | 
|---|
 | 195 |       "FourBodyPotential_Torsion::operator() - types don't match with ones in arguments.");
 | 
|---|
 | 196 |   const argument_t &r_ij = arguments[0]; // 01
 | 
|---|
 | 197 |   const argument_t &r_ik = arguments[1]; // 02
 | 
|---|
 | 198 |   const argument_t &r_il = arguments[2]; // 03
 | 
|---|
 | 199 |   const argument_t &r_jk = arguments[3]; // 12
 | 
|---|
 | 200 |   const argument_t &r_jl = arguments[4]; // 13
 | 
|---|
 | 201 |   const argument_t &r_kl = arguments[5]; // 23
 | 
|---|
 | 202 |   switch (index) {
 | 
|---|
 | 203 |     case spring_constant:
 | 
|---|
 | 204 |     {
 | 
|---|
 | 205 |       const result_t result =
 | 
|---|
 | 206 |                  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 );
 | 
|---|
 | 207 |       return std::vector<result_t>(1, result);
 | 
|---|
 | 208 |       break;
 | 
|---|
 | 209 |     }
 | 
|---|
 | 210 |     case equilibrium_distance:
 | 
|---|
 | 211 |     {
 | 
|---|
 | 212 |       const result_t result =
 | 
|---|
 | 213 |           -2. * params[spring_constant]
 | 
|---|
 | 214 |                  * ( function_theta(r_ij.distance, r_ik.distance, r_il.distance, r_jk.distance, r_jl.distance, r_kl.distance) - params[equilibrium_distance]);
 | 
|---|
 | 215 |       return std::vector<result_t>(1, result);
 | 
|---|
 | 216 |       break;
 | 
|---|
 | 217 |     }
 | 
|---|
 | 218 |     default:
 | 
|---|
 | 219 |       ASSERT(0, "FourBodyPotential_Torsion::parameter_derivative() - derivative to unknown parameter desired.");
 | 
|---|
 | 220 |       break;
 | 
|---|
 | 221 |   }
 | 
|---|
| [a2a2f7] | 222 |   return std::vector<result_t>(1);
 | 
|---|
| [e2037e] | 223 | }
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 | FunctionModel::extractor_t
 | 
|---|
 | 226 | FourBodyPotential_Torsion::getSpecificExtractor() const
 | 
|---|
 | 227 | {
 | 
|---|
 | 228 |   Fragment::charges_t charges;
 | 
|---|
 | 229 |   charges.resize(getParticleTypes().size());
 | 
|---|
 | 230 |   std::transform(getParticleTypes().begin(), getParticleTypes().end(),
 | 
|---|
 | 231 |       charges.begin(), boost::lambda::_1);
 | 
|---|
 | 232 |   FunctionModel::extractor_t returnfunction =
 | 
|---|
 | 233 |       boost::bind(&Extractors::gatherDistancesFromFragment,
 | 
|---|
 | 234 |           boost::bind(&Fragment::getPositions, _1),
 | 
|---|
 | 235 |           boost::bind(&Fragment::getCharges, _1),
 | 
|---|
 | 236 |           charges,
 | 
|---|
 | 237 |           _2);
 | 
|---|
 | 238 |   return returnfunction;
 | 
|---|
 | 239 | }
 | 
|---|
 | 240 | 
 | 
|---|
 | 241 | FunctionModel::filter_t
 | 
|---|
 | 242 | FourBodyPotential_Torsion::getSpecificFilter() const
 | 
|---|
 | 243 | {
 | 
|---|
 | 244 |   FunctionModel::filter_t returnfunction =
 | 
|---|
 | 245 |       boost::bind(&Extractors::reorderArgumentsByParticleTypes,
 | 
|---|
 | 246 |         boost::bind(&Extractors::filterArgumentsByParticleTypes,
 | 
|---|
 | 247 |             _1,
 | 
|---|
 | 248 |             getParticleTypes()),
 | 
|---|
 | 249 |         getParticleTypes()
 | 
|---|
 | 250 |       );
 | 
|---|
 | 251 |   return returnfunction;
 | 
|---|
 | 252 | }
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 | void
 | 
|---|
 | 255 | FourBodyPotential_Torsion::setParametersToRandomInitialValues(
 | 
|---|
 | 256 |     const TrainingData &data)
 | 
|---|
 | 257 | {
 | 
|---|
| [0932c2] | 258 |   RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
 | 
|---|
 | 259 |   const double rng_min = random.min();
 | 
|---|
 | 260 |   const double rng_max = random.max();
 | 
|---|
 | 261 |   params[FourBodyPotential_Torsion::spring_constant] = 2.*(random()/(rng_max-rng_min));
 | 
|---|
 | 262 |   params[FourBodyPotential_Torsion::equilibrium_distance] = 2.*(random()/(rng_max-rng_min));
 | 
|---|
| [e2037e] | 263 | }
 | 
|---|
 | 264 | 
 | 
|---|