[6bb72a] | 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.
|
---|
[6bb72a] | 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_Harmonic.cpp
|
---|
| 27 | *
|
---|
| 28 | * Created on: Sep 26, 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_Harmonic.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 |
|
---|
[6bb72a] | 47 | #include "CodePatterns/Assert.hpp"
|
---|
| 48 |
|
---|
[7b019a] | 49 | #include "FunctionApproximation/Extractors.hpp"
|
---|
[d52819] | 50 | #include "FunctionApproximation/TrainingData.hpp"
|
---|
[6bb72a] | 51 | #include "Potentials/helpers.hpp"
|
---|
[94453f1] | 52 | #include "Potentials/InternalCoordinates/TwoBody_Length.hpp"
|
---|
[b760bc3] | 53 | #include "Potentials/ParticleTypeCheckers.hpp"
|
---|
[0932c2] | 54 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
|
---|
| 55 | #include "RandomNumbers/RandomNumberGenerator.hpp"
|
---|
[6bb72a] | 56 |
|
---|
[7b019a] | 57 | class Fragment;
|
---|
| 58 |
|
---|
[ed2551] | 59 | // static definitions
|
---|
| 60 | const PairPotential_Harmonic::ParameterNames_t
|
---|
| 61 | PairPotential_Harmonic::ParameterNames =
|
---|
| 62 | boost::assign::list_of<std::string>
|
---|
| 63 | ("spring_constant")
|
---|
| 64 | ("equilibrium_distance")
|
---|
| 65 | ;
|
---|
| 66 | const std::string PairPotential_Harmonic::potential_token("harmonic_bond");
|
---|
[7d320c] | 67 | Coordinator::ptr PairPotential_Harmonic::coordinator(Memory::ignore(new TwoBody_Length()));
|
---|
[ed2551] | 68 |
|
---|
[d5ca1a] | 69 | static HomologyGraph generateBindingModel(const EmpiricalPotential::ParticleTypes_t &_ParticleTypes)
|
---|
| 70 | {
|
---|
| 71 | // fill nodes
|
---|
| 72 | HomologyGraph::nodes_t nodes;
|
---|
| 73 | {
|
---|
| 74 | ASSERT( _ParticleTypes.size() == (size_t)2,
|
---|
| 75 | "generateBindingModel() - PairPotential_Harmonic needs two types.");
|
---|
| 76 | std::pair<HomologyGraph::nodes_t::iterator, bool > inserter;
|
---|
| 77 | inserter = nodes.insert( std::make_pair(FragmentNode(_ParticleTypes[0], 1), 1) );
|
---|
| 78 | if (!inserter.second)
|
---|
| 79 | ++(inserter.first->second);
|
---|
| 80 | inserter = nodes.insert( std::make_pair(FragmentNode(_ParticleTypes[1], 1), 1) );
|
---|
| 81 | if (!inserter.second)
|
---|
| 82 | ++(inserter.first->second);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // there are no edges
|
---|
| 86 | HomologyGraph::edges_t edges;
|
---|
| 87 | {
|
---|
| 88 | edges.insert( std::make_pair( FragmentEdge(_ParticleTypes[0], _ParticleTypes[1]), 1) );
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | return HomologyGraph(nodes, edges);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[a82d33] | 94 | PairPotential_Harmonic::PairPotential_Harmonic() :
|
---|
| 95 | EmpiricalPotential(),
|
---|
[d5ca1a] | 96 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
| 97 | bindingmodel(HomologyGraph())
|
---|
[a82d33] | 98 | {
|
---|
| 99 | // have some decent defaults for parameter_derivative checking
|
---|
| 100 | params[spring_constant] = 1.;
|
---|
| 101 | params[equilibrium_distance] = 1.;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[ed2551] | 104 | PairPotential_Harmonic::PairPotential_Harmonic(
|
---|
| 105 | const ParticleTypes_t &_ParticleTypes) :
|
---|
[fdd23a] | 106 | EmpiricalPotential(_ParticleTypes),
|
---|
[d5ca1a] | 107 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
| 108 | bindingmodel(generateBindingModel(_ParticleTypes))
|
---|
[dbf8c8] | 109 | {
|
---|
| 110 | // have some decent defaults for parameter_derivative checking
|
---|
| 111 | params[spring_constant] = 1.;
|
---|
| 112 | params[equilibrium_distance] = 1.;
|
---|
| 113 | }
|
---|
[1dca9a] | 114 |
|
---|
| 115 | PairPotential_Harmonic::PairPotential_Harmonic(
|
---|
[ed2551] | 116 | const ParticleTypes_t &_ParticleTypes,
|
---|
[1dca9a] | 117 | const double _spring_constant,
|
---|
[065a16] | 118 | const double _equilibrium_distance) :
|
---|
[fdd23a] | 119 | EmpiricalPotential(_ParticleTypes),
|
---|
[d5ca1a] | 120 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
| 121 | bindingmodel(generateBindingModel(_ParticleTypes))
|
---|
[1dca9a] | 122 | {
|
---|
| 123 | params[spring_constant] = _spring_constant;
|
---|
| 124 | params[equilibrium_distance] = _equilibrium_distance;
|
---|
| 125 | }
|
---|
[086070] | 126 |
|
---|
| 127 | void PairPotential_Harmonic::setParameters(const parameters_t &_params)
|
---|
| 128 | {
|
---|
| 129 | const size_t paramsDim = _params.size();
|
---|
| 130 | ASSERT( paramsDim <= getParameterDimension(),
|
---|
| 131 | "PairPotential_Harmonic::setParameters() - we need not more than "
|
---|
| 132 | +toString(getParameterDimension())+" parameters.");
|
---|
| 133 | for(size_t i=0;i<paramsDim;++i)
|
---|
| 134 | params[i] = _params[i];
|
---|
| 135 |
|
---|
| 136 | #ifndef NDEBUG
|
---|
| 137 | parameters_t check_params(getParameters());
|
---|
| 138 | check_params.resize(paramsDim); // truncate to same size
|
---|
| 139 | ASSERT( check_params == _params,
|
---|
| 140 | "PairPotential_Harmonic::setParameters() - failed, mismatch in to be set "
|
---|
| 141 | +toString(_params)+" and set "+toString(check_params)+" params.");
|
---|
| 142 | #endif
|
---|
| 143 | }
|
---|
[1dca9a] | 144 |
|
---|
[4f82f8] | 145 | PairPotential_Harmonic::results_t
|
---|
[6bb72a] | 146 | PairPotential_Harmonic::operator()(
|
---|
[e1fe7e] | 147 | const list_of_arguments_t &listarguments
|
---|
[6bb72a] | 148 | ) const
|
---|
| 149 | {
|
---|
[e1fe7e] | 150 | result_t result = 0.;
|
---|
| 151 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 152 | iter != listarguments.end(); ++iter) {
|
---|
| 153 | const arguments_t &arguments = *iter;
|
---|
| 154 | ASSERT( arguments.size() == 1,
|
---|
| 155 | "PairPotential_Harmonic::operator() - requires exactly one argument.");
|
---|
| 156 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 157 | arguments, getParticleTypes()),
|
---|
| 158 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
| 159 | const argument_t &r_ij = arguments[0];
|
---|
| 160 | result +=
|
---|
| 161 | params[spring_constant]
|
---|
| 162 | * Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
|
---|
| 163 | }
|
---|
| 164 | return results_t(1, result);
|
---|
[6bb72a] | 165 | }
|
---|
| 166 |
|
---|
[4f82f8] | 167 | PairPotential_Harmonic::derivative_components_t
|
---|
[6bb72a] | 168 | PairPotential_Harmonic::derivative(
|
---|
[e1fe7e] | 169 | const list_of_arguments_t &listarguments
|
---|
[6bb72a] | 170 | ) const
|
---|
| 171 | {
|
---|
[e1fe7e] | 172 | result_t result = 0.;
|
---|
| 173 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 174 | iter != listarguments.end(); ++iter) {
|
---|
| 175 | const arguments_t &arguments = *iter;
|
---|
| 176 | ASSERT( arguments.size() == 1,
|
---|
| 177 | "PairPotential_Harmonic::operator() - requires exactly one argument.");
|
---|
| 178 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 179 | arguments, getParticleTypes()),
|
---|
| 180 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
| 181 | const argument_t &r_ij = arguments[0];
|
---|
| 182 | result +=
|
---|
| 183 | 2. * params[spring_constant] *
|
---|
| 184 | ( r_ij.distance - params[equilibrium_distance]);
|
---|
| 185 | }
|
---|
| 186 | return derivative_components_t(1, result);
|
---|
[6bb72a] | 187 | }
|
---|
[4f82f8] | 188 |
|
---|
[5b5724] | 189 | PairPotential_Harmonic::results_t
|
---|
| 190 | PairPotential_Harmonic::parameter_derivative(
|
---|
[e1fe7e] | 191 | const list_of_arguments_t &listarguments,
|
---|
[5b5724] | 192 | const size_t index
|
---|
| 193 | ) const
|
---|
| 194 | {
|
---|
[e1fe7e] | 195 | result_t result = 0.;
|
---|
| 196 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 197 | iter != listarguments.end(); ++iter) {
|
---|
| 198 | const arguments_t &arguments = *iter;
|
---|
| 199 | ASSERT( arguments.size() == 1,
|
---|
| 200 | "PairPotential_Harmonic::parameter_derivative() - requires exactly one argument.");
|
---|
| 201 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 202 | arguments, getParticleTypes()),
|
---|
| 203 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
| 204 | const argument_t &r_ij = arguments[0];
|
---|
| 205 | switch (index) {
|
---|
| 206 | case spring_constant:
|
---|
| 207 | {
|
---|
| 208 | result +=
|
---|
| 209 | Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
|
---|
| 210 | break;
|
---|
| 211 | }
|
---|
| 212 | case equilibrium_distance:
|
---|
| 213 | {
|
---|
| 214 | result +=
|
---|
| 215 | -2. * params[spring_constant]
|
---|
| 216 | * ( r_ij.distance - params[equilibrium_distance]);
|
---|
| 217 | break;
|
---|
| 218 | }
|
---|
| 219 | default:
|
---|
| 220 | ASSERT(0, "PairPotential_Harmonic::parameter_derivative() - derivative to unknown parameter desired.");
|
---|
| 221 | break;
|
---|
[5b5724] | 222 | }
|
---|
| 223 | }
|
---|
[e1fe7e] | 224 | return results_t(1, result);
|
---|
[5b5724] | 225 | }
|
---|
| 226 |
|
---|
[0f5d38] | 227 | FunctionModel::filter_t PairPotential_Harmonic::getSpecificFilter() const
|
---|
| 228 | {
|
---|
| 229 | FunctionModel::filter_t returnfunction =
|
---|
[51e0e3] | 230 | boost::bind(&Extractors::filterArgumentsByParticleTypes,
|
---|
| 231 | _1,
|
---|
| 232 | getParticleTypes());
|
---|
[0f5d38] | 233 | return returnfunction;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[d52819] | 236 | void
|
---|
| 237 | PairPotential_Harmonic::setParametersToRandomInitialValues(
|
---|
| 238 | const TrainingData &data)
|
---|
| 239 | {
|
---|
[0932c2] | 240 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
|
---|
| 241 | const double rng_min = random.min();
|
---|
| 242 | const double rng_max = random.max();
|
---|
| 243 | params[PairPotential_Harmonic::equilibrium_distance] = 3e+0*(random()/(rng_max-rng_min)) + .5;// 1.;
|
---|
| 244 | params[PairPotential_Harmonic::spring_constant] = 1e+0*(random()/(rng_max-rng_min));// 0.2;
|
---|
[d52819] | 245 | }
|
---|
| 246 |
|
---|