| 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 | 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 |  | 
|---|
| 78 | PairPotential_Harmonic::PairPotential_Harmonic( | 
|---|
| 79 | const ParticleTypes_t &_ParticleTypes) : | 
|---|
| 80 | EmpiricalPotential(_ParticleTypes), | 
|---|
| 81 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| 82 | { | 
|---|
| 83 | // have some decent defaults for parameter_derivative checking | 
|---|
| 84 | params[spring_constant] = 1.; | 
|---|
| 85 | params[equilibrium_distance] = 1.; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | PairPotential_Harmonic::PairPotential_Harmonic( | 
|---|
| 89 | const ParticleTypes_t &_ParticleTypes, | 
|---|
| 90 | const double _spring_constant, | 
|---|
| 91 | const double _equilibrium_distance) : | 
|---|
| 92 | EmpiricalPotential(_ParticleTypes), | 
|---|
| 93 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| 94 | { | 
|---|
| 95 | params[spring_constant] = _spring_constant; | 
|---|
| 96 | params[equilibrium_distance] = _equilibrium_distance; | 
|---|
| 97 | } | 
|---|
| 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 | } | 
|---|
| 116 |  | 
|---|
| 117 | PairPotential_Harmonic::results_t | 
|---|
| 118 | PairPotential_Harmonic::operator()( | 
|---|
| 119 | const list_of_arguments_t &listarguments | 
|---|
| 120 | ) const | 
|---|
| 121 | { | 
|---|
| 122 | result_t result = 0.; | 
|---|
| 123 | for(list_of_arguments_t::const_iterator iter = listarguments.begin(); | 
|---|
| 124 | iter != listarguments.end(); ++iter) { | 
|---|
| 125 | const arguments_t &arguments = *iter; | 
|---|
| 126 | ASSERT( arguments.size() == 1, | 
|---|
| 127 | "PairPotential_Harmonic::operator() - requires exactly one argument."); | 
|---|
| 128 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
| 129 | arguments, getParticleTypes()), | 
|---|
| 130 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments."); | 
|---|
| 131 | const argument_t &r_ij = arguments[0]; | 
|---|
| 132 | result += | 
|---|
| 133 | params[spring_constant] | 
|---|
| 134 | * Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 ); | 
|---|
| 135 | } | 
|---|
| 136 | return results_t(1, result); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | PairPotential_Harmonic::derivative_components_t | 
|---|
| 140 | PairPotential_Harmonic::derivative( | 
|---|
| 141 | const list_of_arguments_t &listarguments | 
|---|
| 142 | ) const | 
|---|
| 143 | { | 
|---|
| 144 | result_t result = 0.; | 
|---|
| 145 | for(list_of_arguments_t::const_iterator iter = listarguments.begin(); | 
|---|
| 146 | iter != listarguments.end(); ++iter) { | 
|---|
| 147 | const arguments_t &arguments = *iter; | 
|---|
| 148 | ASSERT( arguments.size() == 1, | 
|---|
| 149 | "PairPotential_Harmonic::operator() - requires exactly one argument."); | 
|---|
| 150 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes( | 
|---|
| 151 | arguments, getParticleTypes()), | 
|---|
| 152 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments."); | 
|---|
| 153 | const argument_t &r_ij = arguments[0]; | 
|---|
| 154 | result += | 
|---|
| 155 | 2. * params[spring_constant] * | 
|---|
| 156 | ( r_ij.distance - params[equilibrium_distance]); | 
|---|
| 157 | } | 
|---|
| 158 | return derivative_components_t(1, result); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | PairPotential_Harmonic::results_t | 
|---|
| 162 | PairPotential_Harmonic::parameter_derivative( | 
|---|
| 163 | const list_of_arguments_t &listarguments, | 
|---|
| 164 | const size_t index | 
|---|
| 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::parameter_derivative() - 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 | switch (index) { | 
|---|
| 178 | case spring_constant: | 
|---|
| 179 | { | 
|---|
| 180 | result += | 
|---|
| 181 | Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 ); | 
|---|
| 182 | break; | 
|---|
| 183 | } | 
|---|
| 184 | case equilibrium_distance: | 
|---|
| 185 | { | 
|---|
| 186 | result += | 
|---|
| 187 | -2. * params[spring_constant] | 
|---|
| 188 | * ( r_ij.distance - params[equilibrium_distance]); | 
|---|
| 189 | break; | 
|---|
| 190 | } | 
|---|
| 191 | default: | 
|---|
| 192 | ASSERT(0, "PairPotential_Harmonic::parameter_derivative() - derivative to unknown parameter desired."); | 
|---|
| 193 | break; | 
|---|
| 194 | } | 
|---|
| 195 | } | 
|---|
| 196 | return results_t(1, result); | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | FunctionModel::filter_t PairPotential_Harmonic::getSpecificFilter() const | 
|---|
| 200 | { | 
|---|
| 201 | FunctionModel::filter_t returnfunction = | 
|---|
| 202 | boost::bind(&Extractors::filterArgumentsByParticleTypes, | 
|---|
| 203 | _1, | 
|---|
| 204 | getParticleTypes()); | 
|---|
| 205 | return returnfunction; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | void | 
|---|
| 209 | PairPotential_Harmonic::setParametersToRandomInitialValues( | 
|---|
| 210 | const TrainingData &data) | 
|---|
| 211 | { | 
|---|
| 212 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator(); | 
|---|
| 213 | const double rng_min = random.min(); | 
|---|
| 214 | const double rng_max = random.max(); | 
|---|
| 215 | params[PairPotential_Harmonic::equilibrium_distance] = 3e+0*(random()/(rng_max-rng_min)) + .5;// 1.; | 
|---|
| 216 | params[PairPotential_Harmonic::spring_constant] = 1e+0*(random()/(rng_max-rng_min));// 0.2; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|