| 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_Harmonic.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: Sep 26, 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_Harmonic.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
 | 
|---|
| 42 | #include <boost/bind.hpp>
 | 
|---|
| 43 | #include <string>
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #include "FunctionApproximation/Extractors.hpp"
 | 
|---|
| 48 | #include "FunctionApproximation/TrainingData.hpp"
 | 
|---|
| 49 | #include "Potentials/helpers.hpp"
 | 
|---|
| 50 | #include "Potentials/ParticleTypeCheckers.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | class Fragment;
 | 
|---|
| 53 | 
 | 
|---|
| 54 | // static definitions
 | 
|---|
| 55 | const PairPotential_Harmonic::ParameterNames_t
 | 
|---|
| 56 | PairPotential_Harmonic::ParameterNames =
 | 
|---|
| 57 |       boost::assign::list_of<std::string>
 | 
|---|
| 58 |       ("spring_constant")
 | 
|---|
| 59 |       ("equilibrium_distance")
 | 
|---|
| 60 |       ("") //energy_offset
 | 
|---|
| 61 |     ;
 | 
|---|
| 62 | const std::string PairPotential_Harmonic::potential_token("harmonic_bond");
 | 
|---|
| 63 | 
 | 
|---|
| 64 | PairPotential_Harmonic::PairPotential_Harmonic(
 | 
|---|
| 65 |     const ParticleTypes_t &_ParticleTypes) :
 | 
|---|
| 66 |     SerializablePotential(_ParticleTypes),
 | 
|---|
| 67 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
| 70 |   params[spring_constant] = 1.;
 | 
|---|
| 71 |   params[equilibrium_distance] = 1.;
 | 
|---|
| 72 |   params[energy_offset] = 0.1;
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | PairPotential_Harmonic::PairPotential_Harmonic(
 | 
|---|
| 76 |     const ParticleTypes_t &_ParticleTypes,
 | 
|---|
| 77 |     const double _spring_constant,
 | 
|---|
| 78 |     const double _equilibrium_distance,
 | 
|---|
| 79 |     const double _energy_offset) :
 | 
|---|
| 80 |   SerializablePotential(_ParticleTypes),
 | 
|---|
| 81 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| 82 | {
 | 
|---|
| 83 |   params[spring_constant] = _spring_constant;
 | 
|---|
| 84 |   params[equilibrium_distance] = _equilibrium_distance;
 | 
|---|
| 85 |   params[energy_offset] = _energy_offset;
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | void PairPotential_Harmonic::setParameters(const parameters_t &_params)
 | 
|---|
| 89 | {
 | 
|---|
| 90 |   const size_t paramsDim = _params.size();
 | 
|---|
| 91 |   ASSERT( paramsDim <= getParameterDimension(),
 | 
|---|
| 92 |       "PairPotential_Harmonic::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_Harmonic::setParameters() - failed, mismatch in to be set "
 | 
|---|
| 102 |       +toString(_params)+" and set "+toString(check_params)+" params.");
 | 
|---|
| 103 | #endif
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | PairPotential_Harmonic::results_t
 | 
|---|
| 107 | PairPotential_Harmonic::operator()(
 | 
|---|
| 108 |     const arguments_t &arguments
 | 
|---|
| 109 |     ) const
 | 
|---|
| 110 | {
 | 
|---|
| 111 |   ASSERT( arguments.size() == 1,
 | 
|---|
| 112 |       "PairPotential_Harmonic::operator() - requires exactly one argument.");
 | 
|---|
| 113 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypesStrictOrdering(
 | 
|---|
| 114 |       arguments, getParticleTypes()),
 | 
|---|
| 115 |       "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
 | 
|---|
| 116 |   const argument_t &r_ij = arguments[0];
 | 
|---|
| 117 |   const result_t result =
 | 
|---|
| 118 |       params[spring_constant]
 | 
|---|
| 119 |              * Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 )
 | 
|---|
| 120 |             + params[energy_offset];
 | 
|---|
| 121 |   return std::vector<result_t>(1, result);
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | PairPotential_Harmonic::derivative_components_t
 | 
|---|
| 125 | PairPotential_Harmonic::derivative(
 | 
|---|
| 126 |     const arguments_t &arguments
 | 
|---|
| 127 |     ) const
 | 
|---|
| 128 | {
 | 
|---|
| 129 |   ASSERT( arguments.size() == 1,
 | 
|---|
| 130 |       "PairPotential_Harmonic::operator() - requires exactly one argument.");
 | 
|---|
| 131 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypesStrictOrdering(
 | 
|---|
| 132 |       arguments, getParticleTypes()),
 | 
|---|
| 133 |       "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
 | 
|---|
| 134 |   derivative_components_t result;
 | 
|---|
| 135 |   const argument_t &r_ij = arguments[0];
 | 
|---|
| 136 |   result.push_back( 2. * params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]) );
 | 
|---|
| 137 |   ASSERT( result.size() == 1,
 | 
|---|
| 138 |       "PairPotential_Harmonic::operator() - we did not create exactly one component.");
 | 
|---|
| 139 |   return result;
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | PairPotential_Harmonic::results_t
 | 
|---|
| 143 | PairPotential_Harmonic::parameter_derivative(
 | 
|---|
| 144 |     const arguments_t &arguments,
 | 
|---|
| 145 |     const size_t index
 | 
|---|
| 146 |     ) const
 | 
|---|
| 147 | {
 | 
|---|
| 148 |   ASSERT( arguments.size() == 1,
 | 
|---|
| 149 |       "PairPotential_Harmonic::parameter_derivative() - requires exactly one argument.");
 | 
|---|
| 150 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypesStrictOrdering(
 | 
|---|
| 151 |       arguments, getParticleTypes()),
 | 
|---|
| 152 |       "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
 | 
|---|
| 153 |   const argument_t &r_ij = arguments[0];
 | 
|---|
| 154 |   switch (index) {
 | 
|---|
| 155 |     case spring_constant:
 | 
|---|
| 156 |     {
 | 
|---|
| 157 |       const result_t result =
 | 
|---|
| 158 |                  Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
 | 
|---|
| 159 |       return std::vector<result_t>(1, result);
 | 
|---|
| 160 |       break;
 | 
|---|
| 161 |     }
 | 
|---|
| 162 |     case equilibrium_distance:
 | 
|---|
| 163 |     {
 | 
|---|
| 164 |       const result_t result =
 | 
|---|
| 165 |           -2. * params[spring_constant]
 | 
|---|
| 166 |                  * ( r_ij.distance - params[equilibrium_distance]);
 | 
|---|
| 167 |       return std::vector<result_t>(1, result);
 | 
|---|
| 168 |       break;
 | 
|---|
| 169 |     }
 | 
|---|
| 170 |     case energy_offset:
 | 
|---|
| 171 |     {
 | 
|---|
| 172 |       const result_t result = +1.;
 | 
|---|
| 173 |       return std::vector<result_t>(1, result);
 | 
|---|
| 174 |       break;
 | 
|---|
| 175 |     }
 | 
|---|
| 176 |     default:
 | 
|---|
| 177 |       break;
 | 
|---|
| 178 |   }
 | 
|---|
| 179 | 
 | 
|---|
| 180 |   return PairPotential_Harmonic::results_t(1, 0.);
 | 
|---|
| 181 | }
 | 
|---|
| 182 | 
 | 
|---|
| 183 | FunctionModel::extractor_t
 | 
|---|
| 184 | PairPotential_Harmonic::getFragmentSpecificExtractor(
 | 
|---|
| 185 |     const charges_t &charges) const
 | 
|---|
| 186 | {
 | 
|---|
| 187 |   ASSERT(charges.size() == (size_t)2,
 | 
|---|
| 188 |       "PairPotential_Harmonic::getFragmentSpecificExtractor() - requires 2 charges.");
 | 
|---|
| 189 |   FunctionModel::extractor_t returnfunction =
 | 
|---|
| 190 |       boost::bind(&Extractors::gatherDistancesFromFragment,
 | 
|---|
| 191 |           boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 192 |           boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 193 |           boost::cref(charges),
 | 
|---|
| 194 |           _2);
 | 
|---|
| 195 |   return returnfunction;
 | 
|---|
| 196 | }
 | 
|---|
| 197 | 
 | 
|---|
| 198 | void
 | 
|---|
| 199 | PairPotential_Harmonic::setParametersToRandomInitialValues(
 | 
|---|
| 200 |     const TrainingData &data)
 | 
|---|
| 201 | {
 | 
|---|
| 202 |   params[PairPotential_Harmonic::energy_offset] =
 | 
|---|
| 203 |       data.getTrainingOutputAverage()[0];// -1.;
 | 
|---|
| 204 |   params[PairPotential_Harmonic::equilibrium_distance] = 3e+0*rand()/(double)RAND_MAX + .5;// 1.;
 | 
|---|
| 205 |   params[PairPotential_Harmonic::spring_constant] = 1e+0*rand()/(double)RAND_MAX;// 0.2;
 | 
|---|
| 206 | }
 | 
|---|
| 207 | 
 | 
|---|