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