| [6bb72a] | 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 | * PairPotential_Harmonic.cpp | 
|---|
|  | 26 | * | 
|---|
|  | 27 | *  Created on: Sep 26, 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 "PairPotential_Harmonic.hpp" | 
|---|
|  | 40 |  | 
|---|
| [ed2551] | 41 | #include <boost/assign/list_of.hpp> // for 'map_list_of()' | 
|---|
|  | 42 | #include <string> | 
|---|
|  | 43 |  | 
|---|
| [6bb72a] | 44 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 45 |  | 
|---|
|  | 46 | #include "Potentials/helpers.hpp" | 
|---|
| [b760bc3] | 47 | #include "Potentials/ParticleTypeCheckers.hpp" | 
|---|
| [6bb72a] | 48 |  | 
|---|
| [ed2551] | 49 | // static definitions | 
|---|
|  | 50 | const PairPotential_Harmonic::ParameterNames_t | 
|---|
|  | 51 | PairPotential_Harmonic::ParameterNames = | 
|---|
|  | 52 | boost::assign::list_of<std::string> | 
|---|
|  | 53 | ("spring_constant") | 
|---|
|  | 54 | ("equilibrium_distance") | 
|---|
|  | 55 | ("") //energy_offset | 
|---|
|  | 56 | ; | 
|---|
|  | 57 | const std::string PairPotential_Harmonic::potential_token("harmonic_bond"); | 
|---|
|  | 58 |  | 
|---|
|  | 59 | PairPotential_Harmonic::PairPotential_Harmonic( | 
|---|
|  | 60 | const ParticleTypes_t &_ParticleTypes) : | 
|---|
|  | 61 | SerializablePotential(_ParticleTypes), | 
|---|
| [1dca9a] | 62 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| [dbf8c8] | 63 | { | 
|---|
|  | 64 | // have some decent defaults for parameter_derivative checking | 
|---|
|  | 65 | params[spring_constant] = 1.; | 
|---|
|  | 66 | params[equilibrium_distance] = 1.; | 
|---|
|  | 67 | params[energy_offset] = 0.1; | 
|---|
|  | 68 | } | 
|---|
| [1dca9a] | 69 |  | 
|---|
|  | 70 | PairPotential_Harmonic::PairPotential_Harmonic( | 
|---|
| [ed2551] | 71 | const ParticleTypes_t &_ParticleTypes, | 
|---|
| [1dca9a] | 72 | const double _spring_constant, | 
|---|
|  | 73 | const double _equilibrium_distance, | 
|---|
|  | 74 | const double _energy_offset) : | 
|---|
| [ed2551] | 75 | SerializablePotential(_ParticleTypes), | 
|---|
|  | 76 | params(parameters_t(MAXPARAMS, 0.)) | 
|---|
| [1dca9a] | 77 | { | 
|---|
|  | 78 | params[spring_constant] = _spring_constant; | 
|---|
|  | 79 | params[equilibrium_distance] = _equilibrium_distance; | 
|---|
|  | 80 | params[energy_offset] = _energy_offset; | 
|---|
|  | 81 | } | 
|---|
| [086070] | 82 |  | 
|---|
|  | 83 | void PairPotential_Harmonic::setParameters(const parameters_t &_params) | 
|---|
|  | 84 | { | 
|---|
|  | 85 | const size_t paramsDim = _params.size(); | 
|---|
|  | 86 | ASSERT( paramsDim <= getParameterDimension(), | 
|---|
|  | 87 | "PairPotential_Harmonic::setParameters() - we need not more than " | 
|---|
|  | 88 | +toString(getParameterDimension())+" parameters."); | 
|---|
|  | 89 | for(size_t i=0;i<paramsDim;++i) | 
|---|
|  | 90 | params[i] = _params[i]; | 
|---|
|  | 91 |  | 
|---|
|  | 92 | #ifndef NDEBUG | 
|---|
|  | 93 | parameters_t check_params(getParameters()); | 
|---|
|  | 94 | check_params.resize(paramsDim); // truncate to same size | 
|---|
|  | 95 | ASSERT( check_params == _params, | 
|---|
|  | 96 | "PairPotential_Harmonic::setParameters() - failed, mismatch in to be set " | 
|---|
|  | 97 | +toString(_params)+" and set "+toString(check_params)+" params."); | 
|---|
|  | 98 | #endif | 
|---|
|  | 99 | } | 
|---|
| [1dca9a] | 100 |  | 
|---|
| [4f82f8] | 101 | PairPotential_Harmonic::results_t | 
|---|
| [6bb72a] | 102 | PairPotential_Harmonic::operator()( | 
|---|
|  | 103 | const arguments_t &arguments | 
|---|
|  | 104 | ) const | 
|---|
|  | 105 | { | 
|---|
|  | 106 | ASSERT( arguments.size() == 1, | 
|---|
|  | 107 | "PairPotential_Harmonic::operator() - requires exactly one argument."); | 
|---|
| [b760bc3] | 108 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypesStrictOrdering( | 
|---|
|  | 109 | arguments, getParticleTypes()), | 
|---|
|  | 110 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments."); | 
|---|
| [6bb72a] | 111 | const argument_t &r_ij = arguments[0]; | 
|---|
| [1dca9a] | 112 | const result_t result = | 
|---|
|  | 113 | params[spring_constant] | 
|---|
|  | 114 | * Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 ) | 
|---|
|  | 115 | + params[energy_offset]; | 
|---|
| [4f82f8] | 116 | return std::vector<result_t>(1, result); | 
|---|
| [6bb72a] | 117 | } | 
|---|
|  | 118 |  | 
|---|
| [4f82f8] | 119 | PairPotential_Harmonic::derivative_components_t | 
|---|
| [6bb72a] | 120 | PairPotential_Harmonic::derivative( | 
|---|
|  | 121 | const arguments_t &arguments | 
|---|
|  | 122 | ) const | 
|---|
|  | 123 | { | 
|---|
|  | 124 | ASSERT( arguments.size() == 1, | 
|---|
|  | 125 | "PairPotential_Harmonic::operator() - requires exactly one argument."); | 
|---|
| [b760bc3] | 126 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypesStrictOrdering( | 
|---|
|  | 127 | arguments, getParticleTypes()), | 
|---|
|  | 128 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments."); | 
|---|
| [6bb72a] | 129 | derivative_components_t result; | 
|---|
|  | 130 | const argument_t &r_ij = arguments[0]; | 
|---|
| [1dca9a] | 131 | result.push_back( 2. * params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]) ); | 
|---|
| [6bb72a] | 132 | ASSERT( result.size() == 1, | 
|---|
|  | 133 | "PairPotential_Harmonic::operator() - we did not create exactly one component."); | 
|---|
|  | 134 | return result; | 
|---|
|  | 135 | } | 
|---|
| [4f82f8] | 136 |  | 
|---|
| [5b5724] | 137 | PairPotential_Harmonic::results_t | 
|---|
|  | 138 | PairPotential_Harmonic::parameter_derivative( | 
|---|
|  | 139 | const arguments_t &arguments, | 
|---|
|  | 140 | const size_t index | 
|---|
|  | 141 | ) const | 
|---|
|  | 142 | { | 
|---|
|  | 143 | ASSERT( arguments.size() == 1, | 
|---|
|  | 144 | "PairPotential_Harmonic::parameter_derivative() - requires exactly one argument."); | 
|---|
| [b760bc3] | 145 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypesStrictOrdering( | 
|---|
|  | 146 | arguments, getParticleTypes()), | 
|---|
|  | 147 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments."); | 
|---|
| [5b5724] | 148 | const argument_t &r_ij = arguments[0]; | 
|---|
|  | 149 | switch (index) { | 
|---|
|  | 150 | case spring_constant: | 
|---|
|  | 151 | { | 
|---|
|  | 152 | const result_t result = | 
|---|
|  | 153 | Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 ); | 
|---|
|  | 154 | return std::vector<result_t>(1, result); | 
|---|
|  | 155 | break; | 
|---|
|  | 156 | } | 
|---|
|  | 157 | case equilibrium_distance: | 
|---|
|  | 158 | { | 
|---|
|  | 159 | const result_t result = | 
|---|
|  | 160 | -2. * params[spring_constant] | 
|---|
|  | 161 | * ( r_ij.distance - params[equilibrium_distance]); | 
|---|
|  | 162 | return std::vector<result_t>(1, result); | 
|---|
|  | 163 | break; | 
|---|
|  | 164 | } | 
|---|
|  | 165 | case energy_offset: | 
|---|
|  | 166 | { | 
|---|
|  | 167 | const result_t result = +1.; | 
|---|
|  | 168 | return std::vector<result_t>(1, result); | 
|---|
|  | 169 | break; | 
|---|
|  | 170 | } | 
|---|
|  | 171 | default: | 
|---|
|  | 172 | break; | 
|---|
|  | 173 | } | 
|---|
|  | 174 |  | 
|---|
|  | 175 | return PairPotential_Harmonic::results_t(1, 0.); | 
|---|
|  | 176 | } | 
|---|
|  | 177 |  | 
|---|