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