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