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