| 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 "CodePatterns/Assert.hpp" | 
|---|
| 42 | #include "CodePatterns/Log.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include "Potentials/helpers.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | SaturationPotential::SaturationPotential( | 
|---|
| 47 | boost::function< std::vector<arguments_t>(const argument_t &, const double)> &_triplefunction) : | 
|---|
| 48 | energy_offset(0.), | 
|---|
| 49 | triplefunction(_triplefunction), | 
|---|
| 50 | saturation_cutoff(2.5) | 
|---|
| 51 | {} | 
|---|
| 52 |  | 
|---|
| 53 | SaturationPotential::SaturationPotential( | 
|---|
| 54 | const double _morse_spring_constant, | 
|---|
| 55 | const double _morse_equilibrium_distance, | 
|---|
| 56 | const double _morse_dissociation_energy, | 
|---|
| 57 | const double _angle_spring_constant, | 
|---|
| 58 | const double _angle_equilibrium_distance, | 
|---|
| 59 | const double _all_energy_offset, | 
|---|
| 60 | boost::function< std::vector<arguments_t>(const argument_t &, const double)> &_triplefunction) : | 
|---|
| 61 | energy_offset(_all_energy_offset), | 
|---|
| 62 | triplefunction(_triplefunction), | 
|---|
| 63 | saturation_cutoff(2.5) | 
|---|
| 64 | { | 
|---|
| 65 | parameters_t morse_params(morse.getParameterDimension()); | 
|---|
| 66 | morse_params[PairPotential_Morse::spring_constant] = _morse_spring_constant; | 
|---|
| 67 | morse_params[PairPotential_Morse::equilibrium_distance] = _morse_equilibrium_distance; | 
|---|
| 68 | morse_params[PairPotential_Morse::dissociation_energy] = _morse_dissociation_energy; | 
|---|
| 69 | morse_params[PairPotential_Morse::energy_offset] = 0.; | 
|---|
| 70 | morse.setParameters(morse_params); | 
|---|
| 71 | parameters_t angle_params(angle.getParameterDimension()); | 
|---|
| 72 | angle_params[PairPotential_Angle::spring_constant] = _angle_spring_constant; | 
|---|
| 73 | angle_params[PairPotential_Angle::equilibrium_distance] = _angle_equilibrium_distance; | 
|---|
| 74 | angle_params[PairPotential_Angle::energy_offset] = 0.; | 
|---|
| 75 | angle.setParameters(angle_params); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void SaturationPotential::setParameters(const parameters_t &_params) | 
|---|
| 79 | { | 
|---|
| 80 | const size_t paramsDim = _params.size(); | 
|---|
| 81 | ASSERT( paramsDim <= getParameterDimension(), | 
|---|
| 82 | "SaturationPotential::setParameters() - we need not more than " | 
|---|
| 83 | +toString(getParameterDimension())+" parameters."); | 
|---|
| 84 | //    LOG(1, "INFO: Setting new SaturationPotential params: " << _params); | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 | // offsets | 
|---|
| 88 | if (paramsDim > all_energy_offset) | 
|---|
| 89 | energy_offset = _params[all_energy_offset]; | 
|---|
| 90 |  | 
|---|
| 91 | // Morse | 
|---|
| 92 | { | 
|---|
| 93 | parameters_t morse_params(morse.getParameters()); | 
|---|
| 94 | if (paramsDim > morse_spring_constant) | 
|---|
| 95 | morse_params[PairPotential_Morse::spring_constant] = _params[morse_spring_constant]; | 
|---|
| 96 | if (paramsDim > morse_equilibrium_distance) | 
|---|
| 97 | morse_params[PairPotential_Morse::equilibrium_distance] = _params[morse_equilibrium_distance]; | 
|---|
| 98 | if (paramsDim > morse_dissociation_energy) | 
|---|
| 99 | morse_params[PairPotential_Morse::dissociation_energy] = _params[morse_dissociation_energy]; | 
|---|
| 100 | morse_params[PairPotential_Morse::energy_offset] = 0.; | 
|---|
| 101 | morse.setParameters(morse_params); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | // Angle | 
|---|
| 105 | { | 
|---|
| 106 | parameters_t angle_params(angle.getParameters()); | 
|---|
| 107 | if (paramsDim > angle_spring_constant) | 
|---|
| 108 | angle_params[PairPotential_Angle::spring_constant] = _params[angle_spring_constant]; | 
|---|
| 109 | if (paramsDim > angle_equilibrium_distance) | 
|---|
| 110 | angle_params[PairPotential_Angle::equilibrium_distance] = _params[angle_equilibrium_distance]; | 
|---|
| 111 | angle_params[PairPotential_Angle::energy_offset] = 0.; | 
|---|
| 112 | angle.setParameters(angle_params); | 
|---|
| 113 | } | 
|---|
| 114 | #ifndef NDEBUG | 
|---|
| 115 | parameters_t check_params(getParameters()); | 
|---|
| 116 | check_params.resize(paramsDim); // truncate to same size | 
|---|
| 117 | ASSERT( check_params == _params, | 
|---|
| 118 | "SaturationPotential::setParameters() - failed, mismatch in to be set " | 
|---|
| 119 | +toString(_params)+" and set "+toString(check_params)+" params."); | 
|---|
| 120 | #endif | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | SaturationPotential::parameters_t SaturationPotential::getParameters() const | 
|---|
| 124 | { | 
|---|
| 125 | parameters_t params(getParameterDimension()); | 
|---|
| 126 | const parameters_t morse_params = morse.getParameters(); | 
|---|
| 127 | const parameters_t angle_params = angle.getParameters(); | 
|---|
| 128 |  | 
|---|
| 129 | params[all_energy_offset] = energy_offset; | 
|---|
| 130 |  | 
|---|
| 131 | params[morse_spring_constant] = morse_params[PairPotential_Morse::spring_constant]; | 
|---|
| 132 | params[morse_equilibrium_distance] = morse_params[PairPotential_Morse::equilibrium_distance]; | 
|---|
| 133 | params[morse_dissociation_energy] = morse_params[PairPotential_Morse::dissociation_energy]; | 
|---|
| 134 |  | 
|---|
| 135 | params[angle_spring_constant] = angle_params[PairPotential_Angle::spring_constant]; | 
|---|
| 136 | params[angle_equilibrium_distance] = angle_params[PairPotential_Angle::equilibrium_distance]; | 
|---|
| 137 | return params; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | SaturationPotential::results_t | 
|---|
| 141 | SaturationPotential::operator()( | 
|---|
| 142 | const arguments_t &arguments | 
|---|
| 143 | ) const | 
|---|
| 144 | { | 
|---|
| 145 | double result = 0.; | 
|---|
| 146 | for(arguments_t::const_iterator argiter = arguments.begin(); | 
|---|
| 147 | argiter != arguments.end(); | 
|---|
| 148 | ++argiter) { | 
|---|
| 149 | const argument_t &r_ij = *argiter; | 
|---|
| 150 | arguments_t args(1, r_ij); | 
|---|
| 151 | if ((r_ij.indices.first == 0)) { // || (r_ij.indices.second == 0)) { | 
|---|
| 152 | result += morse(args)[0]; | 
|---|
| 153 | if (result != result) | 
|---|
| 154 | ELOG(1, "result is NAN."); | 
|---|
| 155 | std::vector<arguments_t> triples = triplefunction(r_ij, saturation_cutoff); | 
|---|
| 156 | for (std::vector<arguments_t>::const_iterator iter = triples.begin(); | 
|---|
| 157 | iter != triples.end(); ++iter) { | 
|---|
| 158 | ASSERT( iter->size() == 2, | 
|---|
| 159 | "ManyBodyPotential_Tersoff::function_derivative_c() - the triples result must contain exactly two distances."); | 
|---|
| 160 | const argument_t &r_ik = (*iter)[0]; | 
|---|
| 161 | const argument_t &r_jk = (*iter)[1]; | 
|---|
| 162 | arguments_t args; | 
|---|
| 163 | args.push_back(r_ij); | 
|---|
| 164 | args.push_back(r_ik); | 
|---|
| 165 | args.push_back(r_jk); | 
|---|
| 166 | result += .5 * angle(args)[0]; | 
|---|
| 167 | if (result != result) | 
|---|
| 168 | ELOG(1, "result is NAN."); | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 | } | 
|---|
| 172 | return std::vector<result_t>(1, energy_offset + result); | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | SaturationPotential::derivative_components_t | 
|---|
| 176 | SaturationPotential::derivative( | 
|---|
| 177 | const arguments_t &arguments | 
|---|
| 178 | ) const | 
|---|
| 179 | { | 
|---|
| 180 | ASSERT( 0, | 
|---|
| 181 | "SaturationPotential::operator() - not implemented."); | 
|---|
| 182 | derivative_components_t result; | 
|---|
| 183 | return result; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | SaturationPotential::results_t | 
|---|
| 187 | SaturationPotential::parameter_derivative( | 
|---|
| 188 | const arguments_t &arguments, | 
|---|
| 189 | const size_t index | 
|---|
| 190 | ) const | 
|---|
| 191 | { | 
|---|
| 192 | double result = 0.; | 
|---|
| 193 | for(arguments_t::const_iterator argiter = arguments.begin(); | 
|---|
| 194 | argiter != arguments.end(); | 
|---|
| 195 | ++argiter) { | 
|---|
| 196 | const argument_t &r_ij = *argiter; | 
|---|
| 197 | arguments_t args(1, r_ij); | 
|---|
| 198 | if ((r_ij.indices.first == 0)) { // || (r_ij.indices.second == 0)) { | 
|---|
| 199 | switch (index) { | 
|---|
| 200 | case morse_spring_constant: | 
|---|
| 201 | { | 
|---|
| 202 | result += morse.parameter_derivative(args, PairPotential_Morse::spring_constant)[0]; | 
|---|
| 203 | break; | 
|---|
| 204 | } | 
|---|
| 205 | case morse_equilibrium_distance: | 
|---|
| 206 | { | 
|---|
| 207 | result += morse.parameter_derivative(args, PairPotential_Morse::equilibrium_distance)[0]; | 
|---|
| 208 | break; | 
|---|
| 209 | } | 
|---|
| 210 | case morse_dissociation_energy: | 
|---|
| 211 | { | 
|---|
| 212 | result += morse.parameter_derivative(args, PairPotential_Morse::dissociation_energy)[0]; | 
|---|
| 213 | break; | 
|---|
| 214 | } | 
|---|
| 215 | default: | 
|---|
| 216 | { | 
|---|
| 217 | std::vector<arguments_t> triples = triplefunction(r_ij, saturation_cutoff); | 
|---|
| 218 | for (std::vector<arguments_t>::const_iterator iter = triples.begin(); | 
|---|
| 219 | iter != triples.end(); ++iter) { | 
|---|
| 220 | ASSERT( iter->size() == 2, | 
|---|
| 221 | "ManyBodyPotential_Tersoff::function_derivative_c() - the triples result must contain exactly two distances."); | 
|---|
| 222 | const argument_t &r_ik = (*iter)[0]; | 
|---|
| 223 | const argument_t &r_jk = (*iter)[1]; | 
|---|
| 224 | arguments_t args; | 
|---|
| 225 | args.push_back(r_ij); | 
|---|
| 226 | args.push_back(r_ik); | 
|---|
| 227 | args.push_back(r_jk); | 
|---|
| 228 | switch (index) { | 
|---|
| 229 | case angle_spring_constant: | 
|---|
| 230 | { | 
|---|
| 231 | result += .5*angle.parameter_derivative(args, PairPotential_Angle::spring_constant)[0]; | 
|---|
| 232 | break; | 
|---|
| 233 | } | 
|---|
| 234 | case angle_equilibrium_distance: | 
|---|
| 235 | { | 
|---|
| 236 | result += .5*angle.parameter_derivative(args, PairPotential_Angle::equilibrium_distance)[0]; | 
|---|
| 237 | break; | 
|---|
| 238 | } | 
|---|
| 239 | default: | 
|---|
| 240 | break; | 
|---|
| 241 | } | 
|---|
| 242 | } | 
|---|
| 243 | } | 
|---|
| 244 | } | 
|---|
| 245 | } | 
|---|
| 246 | } | 
|---|
| 247 | if (index == all_energy_offset) | 
|---|
| 248 | result = 1.; | 
|---|
| 249 | return SaturationPotential::results_t(1, result); | 
|---|
| 250 | } | 
|---|