| 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.hpp> | 
|---|
| 42 | #include <boost/assign/list_of.hpp> // for 'map_list_of()' | 
|---|
| 43 | #include <iostream> | 
|---|
| 44 | #include <string> | 
|---|
| 45 |  | 
|---|
| 46 | #include "CodePatterns/Assert.hpp" | 
|---|
| 47 | #include "CodePatterns/Log.hpp" | 
|---|
| 48 |  | 
|---|
| 49 | #include "FunctionApproximation/Extractors.hpp" | 
|---|
| 50 | #include "FunctionApproximation/TrainingData.hpp" | 
|---|
| 51 | #include "Potentials/helpers.hpp" | 
|---|
| 52 | #include "Potentials/ParticleTypeCheckers.hpp" | 
|---|
| 53 |  | 
|---|
| 54 | class Fragment; | 
|---|
| 55 |  | 
|---|
| 56 | using namespace boost::assign; | 
|---|
| 57 |  | 
|---|
| 58 | // static definitions | 
|---|
| 59 | const SaturationPotential::ParameterNames_t | 
|---|
| 60 | SaturationPotential::ParameterNames = | 
|---|
| 61 | boost::assign::list_of<std::string> | 
|---|
| 62 | ("all_energy_offset") | 
|---|
| 63 | ("") | 
|---|
| 64 | ("") | 
|---|
| 65 | ("") | 
|---|
| 66 | ("") | 
|---|
| 67 | ("") | 
|---|
| 68 | ; | 
|---|
| 69 | const std::string SaturationPotential::potential_token("saturation"); | 
|---|
| 70 |  | 
|---|
| 71 | SaturationPotential::SaturationPotential( | 
|---|
| 72 | const ParticleTypes_t &_ParticleTypes) : | 
|---|
| 73 | SerializablePotential(_ParticleTypes), | 
|---|
| 74 | morse(_ParticleTypes), | 
|---|
| 75 | angle(symmetrizeTypes(_ParticleTypes)), | 
|---|
| 76 | energy_offset(0.) | 
|---|
| 77 | { | 
|---|
| 78 | // have some decent defaults for parameter_derivative checking | 
|---|
| 79 | // Morse and Angle have their own defaults, offset is set | 
|---|
| 80 | ASSERT( _ParticleTypes.size() == (size_t)2, | 
|---|
| 81 | "SaturationPotential::SaturationPotential() - exactly two types must be given."); | 
|---|
| 82 | ASSERT( _ParticleTypes[1] == 1, | 
|---|
| 83 | "SaturationPotential::SaturationPotential() - second type must be hydrogen."); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | SaturationPotential::SaturationPotential( | 
|---|
| 87 | const ParticleTypes_t &_ParticleTypes, | 
|---|
| 88 | const double _all_energy_offset, | 
|---|
| 89 | const double _morse_spring_constant, | 
|---|
| 90 | const double _morse_equilibrium_distance, | 
|---|
| 91 | const double _morse_dissociation_energy, | 
|---|
| 92 | const double _angle_spring_constant, | 
|---|
| 93 | const double _angle_equilibrium_distance) : | 
|---|
| 94 | SerializablePotential(_ParticleTypes), | 
|---|
| 95 | morse(_ParticleTypes), | 
|---|
| 96 | angle(symmetrizeTypes(_ParticleTypes)), | 
|---|
| 97 | energy_offset(_all_energy_offset) | 
|---|
| 98 | { | 
|---|
| 99 | ASSERT( _ParticleTypes.size() == (size_t)2, | 
|---|
| 100 | "SaturationPotential::SaturationPotential() - exactly two types must be given."); | 
|---|
| 101 | ASSERT( _ParticleTypes[1] == 1, | 
|---|
| 102 | "SaturationPotential::SaturationPotential() - second type must be hydrogen."); | 
|---|
| 103 | parameters_t morse_params(morse.getParameterDimension()); | 
|---|
| 104 | morse_params[PairPotential_Morse::spring_constant] = _morse_spring_constant; | 
|---|
| 105 | morse_params[PairPotential_Morse::equilibrium_distance] = _morse_equilibrium_distance; | 
|---|
| 106 | morse_params[PairPotential_Morse::dissociation_energy] = _morse_dissociation_energy; | 
|---|
| 107 | morse_params[PairPotential_Morse::energy_offset] = 0.; | 
|---|
| 108 | morse.setParameters(morse_params); | 
|---|
| 109 | parameters_t angle_params(angle.getParameterDimension()); | 
|---|
| 110 | angle_params[PairPotential_Angle::spring_constant] = _angle_spring_constant; | 
|---|
| 111 | angle_params[PairPotential_Angle::equilibrium_distance] = _angle_equilibrium_distance; | 
|---|
| 112 | angle_params[PairPotential_Angle::energy_offset] = 0.; | 
|---|
| 113 | angle.setParameters(angle_params); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void SaturationPotential::setParameters(const parameters_t &_params) | 
|---|
| 117 | { | 
|---|
| 118 | const size_t paramsDim = _params.size(); | 
|---|
| 119 | ASSERT( paramsDim <= getParameterDimension(), | 
|---|
| 120 | "SaturationPotential::setParameters() - we need not more than " | 
|---|
| 121 | +toString(getParameterDimension())+" parameters."); | 
|---|
| 122 | //    LOG(1, "INFO: Setting new SaturationPotential params: " << _params); | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 | // offsets | 
|---|
| 126 | if (paramsDim > all_energy_offset) | 
|---|
| 127 | energy_offset = _params[all_energy_offset]; | 
|---|
| 128 |  | 
|---|
| 129 | // Morse | 
|---|
| 130 | { | 
|---|
| 131 | parameters_t morse_params(morse.getParameters()); | 
|---|
| 132 | if (paramsDim > morse_spring_constant) | 
|---|
| 133 | morse_params[PairPotential_Morse::spring_constant] = _params[morse_spring_constant]; | 
|---|
| 134 | if (paramsDim > morse_equilibrium_distance) | 
|---|
| 135 | morse_params[PairPotential_Morse::equilibrium_distance] = _params[morse_equilibrium_distance]; | 
|---|
| 136 | if (paramsDim > morse_dissociation_energy) | 
|---|
| 137 | morse_params[PairPotential_Morse::dissociation_energy] = _params[morse_dissociation_energy]; | 
|---|
| 138 | morse_params[PairPotential_Morse::energy_offset] = 0.; | 
|---|
| 139 | morse.setParameters(morse_params); | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | // Angle | 
|---|
| 143 | { | 
|---|
| 144 | parameters_t angle_params(angle.getParameters()); | 
|---|
| 145 | if (paramsDim > angle_spring_constant) | 
|---|
| 146 | angle_params[PairPotential_Angle::spring_constant] = _params[angle_spring_constant]; | 
|---|
| 147 | if (paramsDim > angle_equilibrium_distance) | 
|---|
| 148 | angle_params[PairPotential_Angle::equilibrium_distance] = _params[angle_equilibrium_distance]; | 
|---|
| 149 | angle_params[PairPotential_Angle::energy_offset] = 0.; | 
|---|
| 150 | angle.setParameters(angle_params); | 
|---|
| 151 | } | 
|---|
| 152 | #ifndef NDEBUG | 
|---|
| 153 | parameters_t check_params(getParameters()); | 
|---|
| 154 | check_params.resize(paramsDim); // truncate to same size | 
|---|
| 155 | ASSERT( check_params == _params, | 
|---|
| 156 | "SaturationPotential::setParameters() - failed, mismatch in to be set " | 
|---|
| 157 | +toString(_params)+" and set "+toString(check_params)+" params."); | 
|---|
| 158 | #endif | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | SaturationPotential::parameters_t SaturationPotential::getParameters() const | 
|---|
| 162 | { | 
|---|
| 163 | parameters_t params(getParameterDimension()); | 
|---|
| 164 | const parameters_t morse_params = morse.getParameters(); | 
|---|
| 165 | const parameters_t angle_params = angle.getParameters(); | 
|---|
| 166 |  | 
|---|
| 167 | params[all_energy_offset] = energy_offset; | 
|---|
| 168 |  | 
|---|
| 169 | params[morse_spring_constant] = morse_params[PairPotential_Morse::spring_constant]; | 
|---|
| 170 | params[morse_equilibrium_distance] = morse_params[PairPotential_Morse::equilibrium_distance]; | 
|---|
| 171 | params[morse_dissociation_energy] = morse_params[PairPotential_Morse::dissociation_energy]; | 
|---|
| 172 |  | 
|---|
| 173 | params[angle_spring_constant] = angle_params[PairPotential_Angle::spring_constant]; | 
|---|
| 174 | params[angle_equilibrium_distance] = angle_params[PairPotential_Angle::equilibrium_distance]; | 
|---|
| 175 | return params; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | SaturationPotential::results_t | 
|---|
| 179 | SaturationPotential::operator()( | 
|---|
| 180 | const arguments_t &arguments | 
|---|
| 181 | ) const | 
|---|
| 182 | { | 
|---|
| 183 | double result = 0.; | 
|---|
| 184 | const ParticleTypes_t &morse_types = morse.getParticleTypes(); | 
|---|
| 185 | for(arguments_t::const_iterator argiter = arguments.begin(); | 
|---|
| 186 | argiter != arguments.end(); | 
|---|
| 187 | ++argiter) { | 
|---|
| 188 | const argument_t &r_ij = *argiter; | 
|---|
| 189 | if (((r_ij.types.first == morse_types[0]) && (r_ij.types.second == morse_types[1])) | 
|---|
| 190 | || ((r_ij.types.first == morse_types[1]) && (r_ij.types.second == morse_types[0]))) { | 
|---|
| 191 | arguments_t args(1, r_ij); | 
|---|
| 192 |  | 
|---|
| 193 | // Morse contribution | 
|---|
| 194 | const double tmp = morse(args)[0]; | 
|---|
| 195 | //      LOG(2, "DEBUG: Morse yields " << tmp << " for << " << r_ij << "."); | 
|---|
| 196 | result += tmp; | 
|---|
| 197 | if (result != result) | 
|---|
| 198 | ELOG(1, "result is NAN."); | 
|---|
| 199 | } | 
|---|
| 200 | } | 
|---|
| 201 | { | 
|---|
| 202 | // Angle contribution | 
|---|
| 203 | const double tmp = angle(arguments)[0];  // as we have all distances we get both jk and kj | 
|---|
| 204 | //    LOG(2, "DEBUG: angle yields " << tmp << " for << " << arguments << "."); | 
|---|
| 205 | result += tmp; | 
|---|
| 206 | if (result != result) | 
|---|
| 207 | ELOG(1, "result is NAN."); | 
|---|
| 208 | } | 
|---|
| 209 | return std::vector<result_t>(1, energy_offset + result); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | SaturationPotential::derivative_components_t | 
|---|
| 213 | SaturationPotential::derivative( | 
|---|
| 214 | const arguments_t &arguments | 
|---|
| 215 | ) const | 
|---|
| 216 | { | 
|---|
| 217 | ASSERT( 0, | 
|---|
| 218 | "SaturationPotential::operator() - not implemented."); | 
|---|
| 219 | derivative_components_t result; | 
|---|
| 220 | return result; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | SaturationPotential::results_t | 
|---|
| 224 | SaturationPotential::parameter_derivative( | 
|---|
| 225 | const arguments_t &arguments, | 
|---|
| 226 | const size_t index | 
|---|
| 227 | ) const | 
|---|
| 228 | { | 
|---|
| 229 | double result = 0.; | 
|---|
| 230 | switch (index) { | 
|---|
| 231 | case all_energy_offset: | 
|---|
| 232 | { | 
|---|
| 233 | result = 1.; | 
|---|
| 234 | break; | 
|---|
| 235 | } | 
|---|
| 236 | case morse_spring_constant: | 
|---|
| 237 | case morse_equilibrium_distance: | 
|---|
| 238 | case morse_dissociation_energy: | 
|---|
| 239 | { | 
|---|
| 240 | const ParticleTypes_t &morse_types = morse.getParticleTypes(); | 
|---|
| 241 | for(arguments_t::const_iterator argiter = arguments.begin(); | 
|---|
| 242 | argiter != arguments.end(); | 
|---|
| 243 | ++argiter) { | 
|---|
| 244 | const argument_t &r_ij = *argiter; | 
|---|
| 245 | if (((r_ij.types.first == morse_types[0]) && (r_ij.types.second == morse_types[1])) | 
|---|
| 246 | || ((r_ij.types.first == morse_types[1]) && (r_ij.types.second == morse_types[0]))) { | 
|---|
| 247 | arguments_t args(1, r_ij); | 
|---|
| 248 | switch (index) { | 
|---|
| 249 | case morse_spring_constant: | 
|---|
| 250 | result += morse.parameter_derivative(args, PairPotential_Morse::spring_constant)[0]; | 
|---|
| 251 | break; | 
|---|
| 252 | case morse_equilibrium_distance: | 
|---|
| 253 | result += morse.parameter_derivative(args, PairPotential_Morse::equilibrium_distance)[0]; | 
|---|
| 254 | break; | 
|---|
| 255 | case morse_dissociation_energy: | 
|---|
| 256 | result += morse.parameter_derivative(args, PairPotential_Morse::dissociation_energy)[0]; | 
|---|
| 257 | break; | 
|---|
| 258 | default: | 
|---|
| 259 | ASSERT( 0, "SaturationPotential::parameter_derivative() - impossible to get here."); | 
|---|
| 260 | break; | 
|---|
| 261 | } | 
|---|
| 262 | } | 
|---|
| 263 | } | 
|---|
| 264 | break; | 
|---|
| 265 | } | 
|---|
| 266 | case angle_spring_constant: | 
|---|
| 267 | { | 
|---|
| 268 | result = angle.parameter_derivative(arguments, PairPotential_Angle::spring_constant)[0]; | 
|---|
| 269 | break; | 
|---|
| 270 | } | 
|---|
| 271 | case angle_equilibrium_distance: | 
|---|
| 272 | { | 
|---|
| 273 | result = angle.parameter_derivative(arguments, PairPotential_Angle::equilibrium_distance)[0]; | 
|---|
| 274 | break; | 
|---|
| 275 | } | 
|---|
| 276 | default: | 
|---|
| 277 | ELOG(1, "SaturationPotential::parameter_derivative() - index " << index << " invalid."); | 
|---|
| 278 | break; | 
|---|
| 279 | } | 
|---|
| 280 | return SaturationPotential::results_t(1, result); | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | const SaturationPotential::ParticleTypes_t | 
|---|
| 284 | SaturationPotential::symmetrizeTypes(const ParticleTypes_t &_ParticleTypes) | 
|---|
| 285 | { | 
|---|
| 286 | ASSERT( _ParticleTypes.size() == (size_t)2, | 
|---|
| 287 | "SaturationPotential::symmetrizeTypes() - require initial _ParticleTypes with two elements."); | 
|---|
| 288 | //  // insert before couple | 
|---|
| 289 | //  ParticleTypes_t types(1, _ParticleTypes[1]); | 
|---|
| 290 | //  types.insert(types.end(), _ParticleTypes.begin(), _ParticleTypes.end()); | 
|---|
| 291 | // insert after the couple | 
|---|
| 292 | ParticleTypes_t types(_ParticleTypes); | 
|---|
| 293 | types.push_back( _ParticleTypes.back() ); | 
|---|
| 294 | ASSERT( types.size() == (size_t)3, | 
|---|
| 295 | "SaturationPotential::symmetrizeTypes() - failed to generate three types for angle."); | 
|---|
| 296 | return types; | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | std::ostream& operator<<(std::ostream &ost, const SaturationPotential &potential) | 
|---|
| 300 | { | 
|---|
| 301 | ost << potential.morse; | 
|---|
| 302 | ost << potential.angle; | 
|---|
| 303 | return ost; | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | std::istream& operator>>(std::istream &ist, SaturationPotential &potential) | 
|---|
| 307 | { | 
|---|
| 308 | ist >> potential.morse; | 
|---|
| 309 | ist >> potential.angle; | 
|---|
| 310 | return ist; | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | FunctionModel::extractor_t | 
|---|
| 314 | SaturationPotential::getFragmentSpecificExtractor( | 
|---|
| 315 | const charges_t &charges) const | 
|---|
| 316 | { | 
|---|
| 317 | ASSERT(charges.size() == (size_t)2, | 
|---|
| 318 | "SaturationPotential::getFragmentSpecificExtractor() - requires 2 charges."); | 
|---|
| 319 | FunctionModel::extractor_t returnfunction; | 
|---|
| 320 | if (charges[0] == charges[1]) { | 
|---|
| 321 | // In case both types are equal there is only a single pair of possible | 
|---|
| 322 | // type combinations. | 
|---|
| 323 | returnfunction = | 
|---|
| 324 | boost::bind(&Extractors::gatherAllDistancesFromFragment, | 
|---|
| 325 | boost::bind(&Fragment::getPositions, _1), | 
|---|
| 326 | boost::bind(&Fragment::getCharges, _1), | 
|---|
| 327 | boost::cref(charges), | 
|---|
| 328 | _2); | 
|---|
| 329 | } else { | 
|---|
| 330 | // we have to chain here a rather complex "tree" of functions | 
|---|
| 331 | // as we only have a couple of ParticleTypes but need to get | 
|---|
| 332 | // all possible three pairs of the set of the two types. | 
|---|
| 333 | // Finally, we also need to arrange them in correct order | 
|---|
| 334 | // (for PairPotentiale_Angle). | 
|---|
| 335 | charges_t firstpair(2, boost::cref(charges[0])); | 
|---|
| 336 | charges_t secondpair(2, boost::cref(charges[1])); | 
|---|
| 337 | const charges_t &thirdpair = charges; | 
|---|
| 338 | returnfunction = | 
|---|
| 339 | boost::bind(&Extractors::reorderArgumentsByParticleTypes, | 
|---|
| 340 | boost::bind(&Extractors::combineArguments, | 
|---|
| 341 | boost::bind(&Extractors::combineArguments, | 
|---|
| 342 | boost::bind(&Extractors::gatherAllDistancesFromFragment, | 
|---|
| 343 | boost::bind(&Fragment::getPositions, _1), | 
|---|
| 344 | boost::bind(&Fragment::getCharges, _1), | 
|---|
| 345 | firstpair,  // no crefs here as are temporaries! | 
|---|
| 346 | _2), | 
|---|
| 347 | boost::bind(&Extractors::gatherAllDistancesFromFragment, | 
|---|
| 348 | boost::bind(&Fragment::getPositions, _1), | 
|---|
| 349 | boost::bind(&Fragment::getCharges, _1), | 
|---|
| 350 | secondpair,  // no crefs here as are temporaries! | 
|---|
| 351 | _2) | 
|---|
| 352 | ), | 
|---|
| 353 | boost::bind(&Extractors::gatherAllDistancesFromFragment, | 
|---|
| 354 | boost::bind(&Fragment::getPositions, _1), | 
|---|
| 355 | boost::bind(&Fragment::getCharges, _1), | 
|---|
| 356 | boost::cref(thirdpair), // only the last one is no temporary | 
|---|
| 357 | _2) | 
|---|
| 358 | ), | 
|---|
| 359 | boost::cref(angle.getParticleTypes()) | 
|---|
| 360 | ); | 
|---|
| 361 | } | 
|---|
| 362 | return returnfunction; | 
|---|
| 363 | } | 
|---|
| 364 |  | 
|---|
| 365 | void | 
|---|
| 366 | SaturationPotential::setParametersToRandomInitialValues( | 
|---|
| 367 | const TrainingData &data) | 
|---|
| 368 | { | 
|---|
| 369 | energy_offset = data.getTrainingOutputAverage()[0]; | 
|---|
| 370 | morse.setParametersToRandomInitialValues(data); | 
|---|
| 371 | angle.setParametersToRandomInitialValues(data); | 
|---|
| 372 | } | 
|---|