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