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