| [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");
 | 
|---|
| [94453f1] | 67 | Coordinator::ptr PairPotential_Harmonic::coordinator(new TwoBody_Length());
 | 
|---|
| [ed2551] | 68 | 
 | 
|---|
| [a82d33] | 69 | PairPotential_Harmonic::PairPotential_Harmonic() :
 | 
|---|
 | 70 |   EmpiricalPotential(),
 | 
|---|
 | 71 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
 | 72 | {
 | 
|---|
 | 73 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
 | 74 |   params[spring_constant] = 1.;
 | 
|---|
 | 75 |   params[equilibrium_distance] = 1.;
 | 
|---|
 | 76 | }
 | 
|---|
 | 77 | 
 | 
|---|
| [ed2551] | 78 | PairPotential_Harmonic::PairPotential_Harmonic(
 | 
|---|
 | 79 |     const ParticleTypes_t &_ParticleTypes) :
 | 
|---|
| [fdd23a] | 80 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| [1dca9a] | 81 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| [dbf8c8] | 82 | {
 | 
|---|
 | 83 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
 | 84 |   params[spring_constant] = 1.;
 | 
|---|
 | 85 |   params[equilibrium_distance] = 1.;
 | 
|---|
 | 86 | }
 | 
|---|
| [1dca9a] | 87 | 
 | 
|---|
 | 88 | PairPotential_Harmonic::PairPotential_Harmonic(
 | 
|---|
| [ed2551] | 89 |     const ParticleTypes_t &_ParticleTypes,
 | 
|---|
| [1dca9a] | 90 |     const double _spring_constant,
 | 
|---|
| [065a16] | 91 |     const double _equilibrium_distance) :
 | 
|---|
| [fdd23a] | 92 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| [ed2551] | 93 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| [1dca9a] | 94 | {
 | 
|---|
 | 95 |   params[spring_constant] = _spring_constant;
 | 
|---|
 | 96 |   params[equilibrium_distance] = _equilibrium_distance;
 | 
|---|
 | 97 | }
 | 
|---|
| [086070] | 98 | 
 | 
|---|
 | 99 | void PairPotential_Harmonic::setParameters(const parameters_t &_params)
 | 
|---|
 | 100 | {
 | 
|---|
 | 101 |   const size_t paramsDim = _params.size();
 | 
|---|
 | 102 |   ASSERT( paramsDim <= getParameterDimension(),
 | 
|---|
 | 103 |       "PairPotential_Harmonic::setParameters() - we need not more than "
 | 
|---|
 | 104 |       +toString(getParameterDimension())+" parameters.");
 | 
|---|
 | 105 |   for(size_t i=0;i<paramsDim;++i)
 | 
|---|
 | 106 |     params[i] = _params[i];
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | #ifndef NDEBUG
 | 
|---|
 | 109 |   parameters_t check_params(getParameters());
 | 
|---|
 | 110 |   check_params.resize(paramsDim); // truncate to same size
 | 
|---|
 | 111 |   ASSERT( check_params == _params,
 | 
|---|
 | 112 |       "PairPotential_Harmonic::setParameters() - failed, mismatch in to be set "
 | 
|---|
 | 113 |       +toString(_params)+" and set "+toString(check_params)+" params.");
 | 
|---|
 | 114 | #endif
 | 
|---|
 | 115 | }
 | 
|---|
| [1dca9a] | 116 | 
 | 
|---|
| [4f82f8] | 117 | PairPotential_Harmonic::results_t
 | 
|---|
| [6bb72a] | 118 | PairPotential_Harmonic::operator()(
 | 
|---|
 | 119 |     const arguments_t &arguments
 | 
|---|
 | 120 |     ) const
 | 
|---|
 | 121 | {
 | 
|---|
 | 122 |   ASSERT( arguments.size() == 1,
 | 
|---|
 | 123 |       "PairPotential_Harmonic::operator() - requires exactly one argument.");
 | 
|---|
| [e352cb] | 124 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
| [b760bc3] | 125 |       arguments, getParticleTypes()),
 | 
|---|
 | 126 |       "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
 | 
|---|
| [6bb72a] | 127 |   const argument_t &r_ij = arguments[0];
 | 
|---|
| [1dca9a] | 128 |   const result_t result =
 | 
|---|
 | 129 |       params[spring_constant]
 | 
|---|
| [065a16] | 130 |              * Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
 | 
|---|
| [4f82f8] | 131 |   return std::vector<result_t>(1, result);
 | 
|---|
| [6bb72a] | 132 | }
 | 
|---|
 | 133 | 
 | 
|---|
| [4f82f8] | 134 | PairPotential_Harmonic::derivative_components_t
 | 
|---|
| [6bb72a] | 135 | PairPotential_Harmonic::derivative(
 | 
|---|
 | 136 |     const arguments_t &arguments
 | 
|---|
 | 137 |     ) const
 | 
|---|
 | 138 | {
 | 
|---|
 | 139 |   ASSERT( arguments.size() == 1,
 | 
|---|
 | 140 |       "PairPotential_Harmonic::operator() - requires exactly one argument.");
 | 
|---|
| [e352cb] | 141 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
| [b760bc3] | 142 |       arguments, getParticleTypes()),
 | 
|---|
 | 143 |       "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
 | 
|---|
| [6bb72a] | 144 |   derivative_components_t result;
 | 
|---|
 | 145 |   const argument_t &r_ij = arguments[0];
 | 
|---|
| [1dca9a] | 146 |   result.push_back( 2. * params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]) );
 | 
|---|
| [6bb72a] | 147 |   ASSERT( result.size() == 1,
 | 
|---|
 | 148 |       "PairPotential_Harmonic::operator() - we did not create exactly one component.");
 | 
|---|
 | 149 |   return result;
 | 
|---|
 | 150 | }
 | 
|---|
| [4f82f8] | 151 | 
 | 
|---|
| [5b5724] | 152 | PairPotential_Harmonic::results_t
 | 
|---|
 | 153 | PairPotential_Harmonic::parameter_derivative(
 | 
|---|
 | 154 |     const arguments_t &arguments,
 | 
|---|
 | 155 |     const size_t index
 | 
|---|
 | 156 |     ) const
 | 
|---|
 | 157 | {
 | 
|---|
 | 158 |   ASSERT( arguments.size() == 1,
 | 
|---|
 | 159 |       "PairPotential_Harmonic::parameter_derivative() - requires exactly one argument.");
 | 
|---|
| [e352cb] | 160 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
| [b760bc3] | 161 |       arguments, getParticleTypes()),
 | 
|---|
 | 162 |       "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
 | 
|---|
| [5b5724] | 163 |   const argument_t &r_ij = arguments[0];
 | 
|---|
 | 164 |   switch (index) {
 | 
|---|
 | 165 |     case spring_constant:
 | 
|---|
 | 166 |     {
 | 
|---|
 | 167 |       const result_t result =
 | 
|---|
 | 168 |                  Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
 | 
|---|
 | 169 |       return std::vector<result_t>(1, result);
 | 
|---|
 | 170 |       break;
 | 
|---|
 | 171 |     }
 | 
|---|
 | 172 |     case equilibrium_distance:
 | 
|---|
 | 173 |     {
 | 
|---|
 | 174 |       const result_t result =
 | 
|---|
 | 175 |           -2. * params[spring_constant]
 | 
|---|
 | 176 |                  * ( r_ij.distance - params[equilibrium_distance]);
 | 
|---|
 | 177 |       return std::vector<result_t>(1, result);
 | 
|---|
 | 178 |       break;
 | 
|---|
 | 179 |     }
 | 
|---|
 | 180 |     default:
 | 
|---|
| [065a16] | 181 |       ASSERT(0, "PairPotential_Harmonic::parameter_derivative() - derivative to unknown parameter desired.");
 | 
|---|
| [5b5724] | 182 |       break;
 | 
|---|
 | 183 |   }
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 |   return PairPotential_Harmonic::results_t(1, 0.);
 | 
|---|
 | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|
| [7b019a] | 188 | FunctionModel::extractor_t
 | 
|---|
| [f0025d] | 189 | PairPotential_Harmonic::getSpecificExtractor() const
 | 
|---|
| [7b019a] | 190 | {
 | 
|---|
| [da2d5c] | 191 |   Fragment::charges_t charges;
 | 
|---|
 | 192 |   charges.resize(getParticleTypes().size());
 | 
|---|
 | 193 |   std::transform(getParticleTypes().begin(), getParticleTypes().end(),
 | 
|---|
 | 194 |       charges.begin(), boost::lambda::_1);
 | 
|---|
| [7b019a] | 195 |   FunctionModel::extractor_t returnfunction =
 | 
|---|
 | 196 |       boost::bind(&Extractors::gatherDistancesFromFragment,
 | 
|---|
 | 197 |           boost::bind(&Fragment::getPositions, _1),
 | 
|---|
 | 198 |           boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| [da2d5c] | 199 |           charges,
 | 
|---|
| [7b019a] | 200 |           _2);
 | 
|---|
 | 201 |   return returnfunction;
 | 
|---|
 | 202 | }
 | 
|---|
 | 203 | 
 | 
|---|
| [0f5d38] | 204 | FunctionModel::filter_t PairPotential_Harmonic::getSpecificFilter() const
 | 
|---|
 | 205 | {
 | 
|---|
 | 206 |   FunctionModel::filter_t returnfunction =
 | 
|---|
| [51e0e3] | 207 |       boost::bind(&Extractors::filterArgumentsByParticleTypes,
 | 
|---|
 | 208 |           _1,
 | 
|---|
 | 209 |           getParticleTypes());
 | 
|---|
| [0f5d38] | 210 |   return returnfunction;
 | 
|---|
 | 211 | }
 | 
|---|
 | 212 | 
 | 
|---|
| [d52819] | 213 | void
 | 
|---|
 | 214 | PairPotential_Harmonic::setParametersToRandomInitialValues(
 | 
|---|
 | 215 |     const TrainingData &data)
 | 
|---|
 | 216 | {
 | 
|---|
| [0932c2] | 217 |   RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
 | 
|---|
 | 218 |   const double rng_min = random.min();
 | 
|---|
 | 219 |   const double rng_max = random.max();
 | 
|---|
 | 220 |   params[PairPotential_Harmonic::equilibrium_distance] = 3e+0*(random()/(rng_max-rng_min)) + .5;// 1.;
 | 
|---|
 | 221 |   params[PairPotential_Harmonic::spring_constant] = 1e+0*(random()/(rng_max-rng_min));// 0.2;
 | 
|---|
| [d52819] | 222 | }
 | 
|---|
 | 223 | 
 | 
|---|