| 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 | * SaturationPotential.cpp
|
|---|
| 26 | *
|
|---|
| 27 | * Created on: Oct 11, 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 "SaturationPotential.hpp"
|
|---|
| 40 |
|
|---|
| 41 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
|
|---|
| 42 | #include <iostream>
|
|---|
| 43 | #include <string>
|
|---|
| 44 |
|
|---|
| 45 | #include "CodePatterns/Assert.hpp"
|
|---|
| 46 | #include "CodePatterns/Log.hpp"
|
|---|
| 47 |
|
|---|
| 48 | #include "Potentials/helpers.hpp"
|
|---|
| 49 |
|
|---|
| 50 | // static definitions
|
|---|
| 51 | const SaturationPotential::ParameterNames_t
|
|---|
| 52 | SaturationPotential::ParameterNames =
|
|---|
| 53 | boost::assign::list_of<std::string>
|
|---|
| 54 | ("all_energy_offset")
|
|---|
| 55 | ("")
|
|---|
| 56 | ("")
|
|---|
| 57 | ("")
|
|---|
| 58 | ("")
|
|---|
| 59 | ("")
|
|---|
| 60 | ;
|
|---|
| 61 | const std::string SaturationPotential::potential_token("saturation");
|
|---|
| 62 |
|
|---|
| 63 | SaturationPotential::SaturationPotential(
|
|---|
| 64 | const ParticleTypes_t &_ParticleTypes,
|
|---|
| 65 | const double _saturation_cutoff,
|
|---|
| 66 | boost::function< std::vector<arguments_t>(const argument_t &, const double)> &_triplefunction) :
|
|---|
| 67 | SerializablePotential(_ParticleTypes),
|
|---|
| 68 | morse(_ParticleTypes),
|
|---|
| 69 | angle(_ParticleTypes),
|
|---|
| 70 | energy_offset(0.),
|
|---|
| 71 | triplefunction(_triplefunction),
|
|---|
| 72 | saturation_cutoff(_saturation_cutoff)
|
|---|
| 73 | {}
|
|---|
| 74 |
|
|---|
| 75 | SaturationPotential::SaturationPotential(
|
|---|
| 76 | const ParticleTypes_t &_ParticleTypes,
|
|---|
| 77 | const double _morse_spring_constant,
|
|---|
| 78 | const double _morse_equilibrium_distance,
|
|---|
| 79 | const double _morse_dissociation_energy,
|
|---|
| 80 | const double _angle_spring_constant,
|
|---|
| 81 | const double _angle_equilibrium_distance,
|
|---|
| 82 | const double _all_energy_offset,
|
|---|
| 83 | const double _saturation_cutoff,
|
|---|
| 84 | boost::function< std::vector<arguments_t>(const argument_t &, const double)> &_triplefunction) :
|
|---|
| 85 | SerializablePotential(_ParticleTypes),
|
|---|
| 86 | morse(_ParticleTypes),
|
|---|
| 87 | angle(_ParticleTypes),
|
|---|
| 88 | energy_offset(_all_energy_offset),
|
|---|
| 89 | triplefunction(_triplefunction),
|
|---|
| 90 | saturation_cutoff(_saturation_cutoff)
|
|---|
| 91 | {
|
|---|
| 92 | parameters_t morse_params(morse.getParameterDimension());
|
|---|
| 93 | morse_params[PairPotential_Morse::spring_constant] = _morse_spring_constant;
|
|---|
| 94 | morse_params[PairPotential_Morse::equilibrium_distance] = _morse_equilibrium_distance;
|
|---|
| 95 | morse_params[PairPotential_Morse::dissociation_energy] = _morse_dissociation_energy;
|
|---|
| 96 | morse_params[PairPotential_Morse::energy_offset] = 0.;
|
|---|
| 97 | morse.setParameters(morse_params);
|
|---|
| 98 | parameters_t angle_params(angle.getParameterDimension());
|
|---|
| 99 | angle_params[PairPotential_Angle::spring_constant] = _angle_spring_constant;
|
|---|
| 100 | angle_params[PairPotential_Angle::equilibrium_distance] = _angle_equilibrium_distance;
|
|---|
| 101 | angle_params[PairPotential_Angle::energy_offset] = 0.;
|
|---|
| 102 | angle.setParameters(angle_params);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | void SaturationPotential::setParameters(const parameters_t &_params)
|
|---|
| 106 | {
|
|---|
| 107 | const size_t paramsDim = _params.size();
|
|---|
| 108 | ASSERT( paramsDim <= getParameterDimension(),
|
|---|
| 109 | "SaturationPotential::setParameters() - we need not more than "
|
|---|
| 110 | +toString(getParameterDimension())+" parameters.");
|
|---|
| 111 | // LOG(1, "INFO: Setting new SaturationPotential params: " << _params);
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | // offsets
|
|---|
| 115 | if (paramsDim > all_energy_offset)
|
|---|
| 116 | energy_offset = _params[all_energy_offset];
|
|---|
| 117 |
|
|---|
| 118 | // Morse
|
|---|
| 119 | {
|
|---|
| 120 | parameters_t morse_params(morse.getParameters());
|
|---|
| 121 | if (paramsDim > morse_spring_constant)
|
|---|
| 122 | morse_params[PairPotential_Morse::spring_constant] = _params[morse_spring_constant];
|
|---|
| 123 | if (paramsDim > morse_equilibrium_distance)
|
|---|
| 124 | morse_params[PairPotential_Morse::equilibrium_distance] = _params[morse_equilibrium_distance];
|
|---|
| 125 | if (paramsDim > morse_dissociation_energy)
|
|---|
| 126 | morse_params[PairPotential_Morse::dissociation_energy] = _params[morse_dissociation_energy];
|
|---|
| 127 | morse_params[PairPotential_Morse::energy_offset] = 0.;
|
|---|
| 128 | morse.setParameters(morse_params);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | // Angle
|
|---|
| 132 | {
|
|---|
| 133 | parameters_t angle_params(angle.getParameters());
|
|---|
| 134 | if (paramsDim > angle_spring_constant)
|
|---|
| 135 | angle_params[PairPotential_Angle::spring_constant] = _params[angle_spring_constant];
|
|---|
| 136 | if (paramsDim > angle_equilibrium_distance)
|
|---|
| 137 | angle_params[PairPotential_Angle::equilibrium_distance] = _params[angle_equilibrium_distance];
|
|---|
| 138 | angle_params[PairPotential_Angle::energy_offset] = 0.;
|
|---|
| 139 | angle.setParameters(angle_params);
|
|---|
| 140 | }
|
|---|
| 141 | #ifndef NDEBUG
|
|---|
| 142 | parameters_t check_params(getParameters());
|
|---|
| 143 | check_params.resize(paramsDim); // truncate to same size
|
|---|
| 144 | ASSERT( check_params == _params,
|
|---|
| 145 | "SaturationPotential::setParameters() - failed, mismatch in to be set "
|
|---|
| 146 | +toString(_params)+" and set "+toString(check_params)+" params.");
|
|---|
| 147 | #endif
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | SaturationPotential::parameters_t SaturationPotential::getParameters() const
|
|---|
| 151 | {
|
|---|
| 152 | parameters_t params(getParameterDimension());
|
|---|
| 153 | const parameters_t morse_params = morse.getParameters();
|
|---|
| 154 | const parameters_t angle_params = angle.getParameters();
|
|---|
| 155 |
|
|---|
| 156 | params[all_energy_offset] = energy_offset;
|
|---|
| 157 |
|
|---|
| 158 | params[morse_spring_constant] = morse_params[PairPotential_Morse::spring_constant];
|
|---|
| 159 | params[morse_equilibrium_distance] = morse_params[PairPotential_Morse::equilibrium_distance];
|
|---|
| 160 | params[morse_dissociation_energy] = morse_params[PairPotential_Morse::dissociation_energy];
|
|---|
| 161 |
|
|---|
| 162 | params[angle_spring_constant] = angle_params[PairPotential_Angle::spring_constant];
|
|---|
| 163 | params[angle_equilibrium_distance] = angle_params[PairPotential_Angle::equilibrium_distance];
|
|---|
| 164 | return params;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | SaturationPotential::results_t
|
|---|
| 168 | SaturationPotential::operator()(
|
|---|
| 169 | const arguments_t &arguments
|
|---|
| 170 | ) const
|
|---|
| 171 | {
|
|---|
| 172 | double result = 0.;
|
|---|
| 173 | for(arguments_t::const_iterator argiter = arguments.begin();
|
|---|
| 174 | argiter != arguments.end();
|
|---|
| 175 | ++argiter) {
|
|---|
| 176 | const argument_t &r_ij = *argiter;
|
|---|
| 177 | if ((r_ij.indices.first == 0)) { // first item must be the non-hydrogen
|
|---|
| 178 | arguments_t args(1, r_ij);
|
|---|
| 179 |
|
|---|
| 180 | // Morse contribution
|
|---|
| 181 | result += morse(args)[0];
|
|---|
| 182 | if (result != result)
|
|---|
| 183 | ELOG(1, "result is NAN.");
|
|---|
| 184 |
|
|---|
| 185 | // Angle contribution
|
|---|
| 186 | std::vector<arguments_t> triples = triplefunction(r_ij, saturation_cutoff);
|
|---|
| 187 | args.resize(3, r_ij);
|
|---|
| 188 | for (std::vector<arguments_t>::const_iterator iter = triples.begin();
|
|---|
| 189 | iter != triples.end(); ++iter) {
|
|---|
| 190 | ASSERT( iter->size() == 2,
|
|---|
| 191 | "SaturationPotential::function_derivative_c() - the triples result must contain exactly two distances.");
|
|---|
| 192 | const argument_t &r_ik = (*iter)[0];
|
|---|
| 193 | const argument_t &r_jk = (*iter)[1];
|
|---|
| 194 | args[1] = r_ik;
|
|---|
| 195 | args[2] = r_jk;
|
|---|
| 196 | result += .5*angle(args)[0]; // as we have all distances we get both jk and kj
|
|---|
| 197 | if (result != result)
|
|---|
| 198 | ELOG(1, "result is NAN.");
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 | return std::vector<result_t>(1, energy_offset + result);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | SaturationPotential::derivative_components_t
|
|---|
| 206 | SaturationPotential::derivative(
|
|---|
| 207 | const arguments_t &arguments
|
|---|
| 208 | ) const
|
|---|
| 209 | {
|
|---|
| 210 | ASSERT( 0,
|
|---|
| 211 | "SaturationPotential::operator() - not implemented.");
|
|---|
| 212 | derivative_components_t result;
|
|---|
| 213 | return result;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | SaturationPotential::results_t
|
|---|
| 217 | SaturationPotential::parameter_derivative(
|
|---|
| 218 | const arguments_t &arguments,
|
|---|
| 219 | const size_t index
|
|---|
| 220 | ) const
|
|---|
| 221 | {
|
|---|
| 222 | double result = 0.;
|
|---|
| 223 | if (index == all_energy_offset) {
|
|---|
| 224 | result = 1.;
|
|---|
| 225 | } else {
|
|---|
| 226 | for(arguments_t::const_iterator argiter = arguments.begin();
|
|---|
| 227 | argiter != arguments.end();
|
|---|
| 228 | ++argiter) {
|
|---|
| 229 | const argument_t &r_ij = *argiter;
|
|---|
| 230 | if ((r_ij.indices.first == 0)) { // first item must be the non-hydrogen
|
|---|
| 231 | arguments_t args(1, r_ij);
|
|---|
| 232 | switch (index) {
|
|---|
| 233 | case morse_spring_constant:
|
|---|
| 234 | {
|
|---|
| 235 | result += morse.parameter_derivative(args, PairPotential_Morse::spring_constant)[0];
|
|---|
| 236 | break;
|
|---|
| 237 | }
|
|---|
| 238 | case morse_equilibrium_distance:
|
|---|
| 239 | {
|
|---|
| 240 | result += morse.parameter_derivative(args, PairPotential_Morse::equilibrium_distance)[0];
|
|---|
| 241 | break;
|
|---|
| 242 | }
|
|---|
| 243 | case morse_dissociation_energy:
|
|---|
| 244 | {
|
|---|
| 245 | result += morse.parameter_derivative(args, PairPotential_Morse::dissociation_energy)[0];
|
|---|
| 246 | break;
|
|---|
| 247 | }
|
|---|
| 248 | default:
|
|---|
| 249 | {
|
|---|
| 250 | args.resize(3, r_ij);
|
|---|
| 251 | std::vector<arguments_t> triples = triplefunction(r_ij, saturation_cutoff);
|
|---|
| 252 | for (std::vector<arguments_t>::const_iterator iter = triples.begin();
|
|---|
| 253 | iter != triples.end(); ++iter) {
|
|---|
| 254 | ASSERT( iter->size() == 2,
|
|---|
| 255 | "SaturationPotential::parameter_derivative() - the triples result must contain exactly two distances.");
|
|---|
| 256 | const argument_t &r_ik = (*iter)[0];
|
|---|
| 257 | ASSERT( r_ik.indices.first == r_ij.indices.first,
|
|---|
| 258 | "SaturationPotential::parameter_derivative() - i not same in ij, ik.");
|
|---|
| 259 | const argument_t &r_jk = (*iter)[1];
|
|---|
| 260 | ASSERT( r_jk.indices.first == r_ij.indices.second,
|
|---|
| 261 | "SaturationPotential::parameter_derivative() - j not same in ij, jk.");
|
|---|
| 262 | ASSERT( r_ik.indices.second == r_jk.indices.second,
|
|---|
| 263 | "SaturationPotential::parameter_derivative() - k not same in ik, jk.");
|
|---|
| 264 | args[1] = r_ik;
|
|---|
| 265 | args[2] = r_jk;
|
|---|
| 266 | switch (index) { // .5 due to we have all distances we get both jk and kj
|
|---|
| 267 | case angle_spring_constant:
|
|---|
| 268 | {
|
|---|
| 269 | result += .5*angle.parameter_derivative(args, PairPotential_Angle::spring_constant)[0];
|
|---|
| 270 | break;
|
|---|
| 271 | }
|
|---|
| 272 | case angle_equilibrium_distance:
|
|---|
| 273 | {
|
|---|
| 274 | result += .5*angle.parameter_derivative(args, PairPotential_Angle::equilibrium_distance)[0];
|
|---|
| 275 | break;
|
|---|
| 276 | }
|
|---|
| 277 | default:
|
|---|
| 278 | break;
|
|---|
| 279 | }
|
|---|
| 280 | }
|
|---|
| 281 | break;
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 | }
|
|---|
| 286 | }
|
|---|
| 287 | return SaturationPotential::results_t(1, result);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | std::ostream& operator<<(std::ostream &ost, const SaturationPotential &potential)
|
|---|
| 291 | {
|
|---|
| 292 | ost << potential.morse;
|
|---|
| 293 | ost << potential.angle;
|
|---|
| 294 | return ost;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | std::istream& operator>>(std::istream &ist, SaturationPotential &potential)
|
|---|
| 298 | {
|
|---|
| 299 | ist >> potential.morse;
|
|---|
| 300 | ist >> potential.angle;
|
|---|
| 301 | return ist;
|
|---|
| 302 | }
|
|---|