[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"
|
---|
[b760bc3] | 52 | #include "Potentials/ParticleTypeCheckers.hpp"
|
---|
[6bb72a] | 53 |
|
---|
[7b019a] | 54 | class Fragment;
|
---|
| 55 |
|
---|
[ed2551] | 56 | // static definitions
|
---|
| 57 | const PairPotential_Harmonic::ParameterNames_t
|
---|
| 58 | PairPotential_Harmonic::ParameterNames =
|
---|
| 59 | boost::assign::list_of<std::string>
|
---|
| 60 | ("spring_constant")
|
---|
| 61 | ("equilibrium_distance")
|
---|
| 62 | ;
|
---|
| 63 | const std::string PairPotential_Harmonic::potential_token("harmonic_bond");
|
---|
| 64 |
|
---|
| 65 | PairPotential_Harmonic::PairPotential_Harmonic(
|
---|
| 66 | const ParticleTypes_t &_ParticleTypes) :
|
---|
[fdd23a] | 67 | EmpiricalPotential(_ParticleTypes),
|
---|
[1dca9a] | 68 | params(parameters_t(MAXPARAMS, 0.))
|
---|
[dbf8c8] | 69 | {
|
---|
| 70 | // have some decent defaults for parameter_derivative checking
|
---|
| 71 | params[spring_constant] = 1.;
|
---|
| 72 | params[equilibrium_distance] = 1.;
|
---|
| 73 | }
|
---|
[1dca9a] | 74 |
|
---|
| 75 | PairPotential_Harmonic::PairPotential_Harmonic(
|
---|
[ed2551] | 76 | const ParticleTypes_t &_ParticleTypes,
|
---|
[1dca9a] | 77 | const double _spring_constant,
|
---|
[065a16] | 78 | const double _equilibrium_distance) :
|
---|
[fdd23a] | 79 | EmpiricalPotential(_ParticleTypes),
|
---|
[ed2551] | 80 | params(parameters_t(MAXPARAMS, 0.))
|
---|
[1dca9a] | 81 | {
|
---|
| 82 | params[spring_constant] = _spring_constant;
|
---|
| 83 | params[equilibrium_distance] = _equilibrium_distance;
|
---|
| 84 | }
|
---|
[086070] | 85 |
|
---|
| 86 | void PairPotential_Harmonic::setParameters(const parameters_t &_params)
|
---|
| 87 | {
|
---|
| 88 | const size_t paramsDim = _params.size();
|
---|
| 89 | ASSERT( paramsDim <= getParameterDimension(),
|
---|
| 90 | "PairPotential_Harmonic::setParameters() - we need not more than "
|
---|
| 91 | +toString(getParameterDimension())+" parameters.");
|
---|
| 92 | for(size_t i=0;i<paramsDim;++i)
|
---|
| 93 | params[i] = _params[i];
|
---|
| 94 |
|
---|
| 95 | #ifndef NDEBUG
|
---|
| 96 | parameters_t check_params(getParameters());
|
---|
| 97 | check_params.resize(paramsDim); // truncate to same size
|
---|
| 98 | ASSERT( check_params == _params,
|
---|
| 99 | "PairPotential_Harmonic::setParameters() - failed, mismatch in to be set "
|
---|
| 100 | +toString(_params)+" and set "+toString(check_params)+" params.");
|
---|
| 101 | #endif
|
---|
| 102 | }
|
---|
[1dca9a] | 103 |
|
---|
[4f82f8] | 104 | PairPotential_Harmonic::results_t
|
---|
[6bb72a] | 105 | PairPotential_Harmonic::operator()(
|
---|
| 106 | const arguments_t &arguments
|
---|
| 107 | ) const
|
---|
| 108 | {
|
---|
| 109 | ASSERT( arguments.size() == 1,
|
---|
| 110 | "PairPotential_Harmonic::operator() - requires exactly one argument.");
|
---|
[e352cb] | 111 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
[b760bc3] | 112 | arguments, getParticleTypes()),
|
---|
| 113 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
[6bb72a] | 114 | const argument_t &r_ij = arguments[0];
|
---|
[1dca9a] | 115 | const result_t result =
|
---|
| 116 | params[spring_constant]
|
---|
[065a16] | 117 | * Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
|
---|
[4f82f8] | 118 | return std::vector<result_t>(1, result);
|
---|
[6bb72a] | 119 | }
|
---|
| 120 |
|
---|
[4f82f8] | 121 | PairPotential_Harmonic::derivative_components_t
|
---|
[6bb72a] | 122 | PairPotential_Harmonic::derivative(
|
---|
| 123 | const arguments_t &arguments
|
---|
| 124 | ) const
|
---|
| 125 | {
|
---|
| 126 | ASSERT( arguments.size() == 1,
|
---|
| 127 | "PairPotential_Harmonic::operator() - requires exactly one argument.");
|
---|
[e352cb] | 128 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
[b760bc3] | 129 | arguments, getParticleTypes()),
|
---|
| 130 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
[6bb72a] | 131 | derivative_components_t result;
|
---|
| 132 | const argument_t &r_ij = arguments[0];
|
---|
[1dca9a] | 133 | result.push_back( 2. * params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]) );
|
---|
[6bb72a] | 134 | ASSERT( result.size() == 1,
|
---|
| 135 | "PairPotential_Harmonic::operator() - we did not create exactly one component.");
|
---|
| 136 | return result;
|
---|
| 137 | }
|
---|
[4f82f8] | 138 |
|
---|
[5b5724] | 139 | PairPotential_Harmonic::results_t
|
---|
| 140 | PairPotential_Harmonic::parameter_derivative(
|
---|
| 141 | const arguments_t &arguments,
|
---|
| 142 | const size_t index
|
---|
| 143 | ) const
|
---|
| 144 | {
|
---|
| 145 | ASSERT( arguments.size() == 1,
|
---|
| 146 | "PairPotential_Harmonic::parameter_derivative() - requires exactly one argument.");
|
---|
[e352cb] | 147 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
[b760bc3] | 148 | arguments, getParticleTypes()),
|
---|
| 149 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
[5b5724] | 150 | const argument_t &r_ij = arguments[0];
|
---|
| 151 | switch (index) {
|
---|
| 152 | case spring_constant:
|
---|
| 153 | {
|
---|
| 154 | const result_t result =
|
---|
| 155 | Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
|
---|
| 156 | return std::vector<result_t>(1, result);
|
---|
| 157 | break;
|
---|
| 158 | }
|
---|
| 159 | case equilibrium_distance:
|
---|
| 160 | {
|
---|
| 161 | const result_t result =
|
---|
| 162 | -2. * params[spring_constant]
|
---|
| 163 | * ( r_ij.distance - params[equilibrium_distance]);
|
---|
| 164 | return std::vector<result_t>(1, result);
|
---|
| 165 | break;
|
---|
| 166 | }
|
---|
| 167 | default:
|
---|
[065a16] | 168 | ASSERT(0, "PairPotential_Harmonic::parameter_derivative() - derivative to unknown parameter desired.");
|
---|
[5b5724] | 169 | break;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | return PairPotential_Harmonic::results_t(1, 0.);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[7b019a] | 175 | FunctionModel::extractor_t
|
---|
[da2d5c] | 176 | PairPotential_Harmonic::getFragmentSpecificExtractor() const
|
---|
[7b019a] | 177 | {
|
---|
[da2d5c] | 178 | Fragment::charges_t charges;
|
---|
| 179 | charges.resize(getParticleTypes().size());
|
---|
| 180 | std::transform(getParticleTypes().begin(), getParticleTypes().end(),
|
---|
| 181 | charges.begin(), boost::lambda::_1);
|
---|
[7b019a] | 182 | FunctionModel::extractor_t returnfunction =
|
---|
| 183 | boost::bind(&Extractors::gatherDistancesFromFragment,
|
---|
| 184 | boost::bind(&Fragment::getPositions, _1),
|
---|
| 185 | boost::bind(&Fragment::getCharges, _1),
|
---|
[da2d5c] | 186 | charges,
|
---|
[7b019a] | 187 | _2);
|
---|
| 188 | return returnfunction;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[d52819] | 191 | void
|
---|
| 192 | PairPotential_Harmonic::setParametersToRandomInitialValues(
|
---|
| 193 | const TrainingData &data)
|
---|
| 194 | {
|
---|
| 195 | params[PairPotential_Harmonic::equilibrium_distance] = 3e+0*rand()/(double)RAND_MAX + .5;// 1.;
|
---|
| 196 | params[PairPotential_Harmonic::spring_constant] = 1e+0*rand()/(double)RAND_MAX;// 0.2;
|
---|
| 197 | }
|
---|
| 198 |
|
---|