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