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