1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of MoleCuilder.
|
---|
10 | *
|
---|
11 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation, either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
23 | */
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * PairPotential_Harmonic.cpp
|
---|
27 | *
|
---|
28 | * Created on: Sep 26, 2012
|
---|
29 | * Author: heber
|
---|
30 | */
|
---|
31 |
|
---|
32 |
|
---|
33 | // include config.h
|
---|
34 | #ifdef HAVE_CONFIG_H
|
---|
35 | #include <config.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include "CodePatterns/MemDebug.hpp"
|
---|
39 |
|
---|
40 | #include "PairPotential_Harmonic.hpp"
|
---|
41 |
|
---|
42 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
|
---|
43 | #include <boost/bind.hpp>
|
---|
44 | #include <boost/lambda/lambda.hpp>
|
---|
45 | #include <string>
|
---|
46 |
|
---|
47 | #include "CodePatterns/Assert.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 | // static definitions
|
---|
57 | const PairPotential_Harmonic::ParameterNames_t
|
---|
58 | PairPotential_Harmonic::ParameterNames =
|
---|
59 | boost::assign::list_of<std::string>
|
---|
60 | ("spring_constant")
|
---|
61 | ("equilibrium_distance")
|
---|
62 | ;
|
---|
63 | const std::string PairPotential_Harmonic::potential_token("harmonic_bond");
|
---|
64 |
|
---|
65 | PairPotential_Harmonic::PairPotential_Harmonic() :
|
---|
66 | EmpiricalPotential(),
|
---|
67 | params(parameters_t(MAXPARAMS, 0.))
|
---|
68 | {
|
---|
69 | // have some decent defaults for parameter_derivative checking
|
---|
70 | params[spring_constant] = 1.;
|
---|
71 | params[equilibrium_distance] = 1.;
|
---|
72 | }
|
---|
73 |
|
---|
74 | PairPotential_Harmonic::PairPotential_Harmonic(
|
---|
75 | const ParticleTypes_t &_ParticleTypes) :
|
---|
76 | EmpiricalPotential(_ParticleTypes),
|
---|
77 | params(parameters_t(MAXPARAMS, 0.))
|
---|
78 | {
|
---|
79 | // have some decent defaults for parameter_derivative checking
|
---|
80 | params[spring_constant] = 1.;
|
---|
81 | params[equilibrium_distance] = 1.;
|
---|
82 | }
|
---|
83 |
|
---|
84 | PairPotential_Harmonic::PairPotential_Harmonic(
|
---|
85 | const ParticleTypes_t &_ParticleTypes,
|
---|
86 | const double _spring_constant,
|
---|
87 | const double _equilibrium_distance) :
|
---|
88 | EmpiricalPotential(_ParticleTypes),
|
---|
89 | params(parameters_t(MAXPARAMS, 0.))
|
---|
90 | {
|
---|
91 | params[spring_constant] = _spring_constant;
|
---|
92 | params[equilibrium_distance] = _equilibrium_distance;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void PairPotential_Harmonic::setParameters(const parameters_t &_params)
|
---|
96 | {
|
---|
97 | const size_t paramsDim = _params.size();
|
---|
98 | ASSERT( paramsDim <= getParameterDimension(),
|
---|
99 | "PairPotential_Harmonic::setParameters() - we need not more than "
|
---|
100 | +toString(getParameterDimension())+" parameters.");
|
---|
101 | for(size_t i=0;i<paramsDim;++i)
|
---|
102 | params[i] = _params[i];
|
---|
103 |
|
---|
104 | #ifndef NDEBUG
|
---|
105 | parameters_t check_params(getParameters());
|
---|
106 | check_params.resize(paramsDim); // truncate to same size
|
---|
107 | ASSERT( check_params == _params,
|
---|
108 | "PairPotential_Harmonic::setParameters() - failed, mismatch in to be set "
|
---|
109 | +toString(_params)+" and set "+toString(check_params)+" params.");
|
---|
110 | #endif
|
---|
111 | }
|
---|
112 |
|
---|
113 | PairPotential_Harmonic::results_t
|
---|
114 | PairPotential_Harmonic::operator()(
|
---|
115 | const arguments_t &arguments
|
---|
116 | ) const
|
---|
117 | {
|
---|
118 | ASSERT( arguments.size() == 1,
|
---|
119 | "PairPotential_Harmonic::operator() - requires exactly one argument.");
|
---|
120 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
121 | arguments, getParticleTypes()),
|
---|
122 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
123 | const argument_t &r_ij = arguments[0];
|
---|
124 | const result_t result =
|
---|
125 | params[spring_constant]
|
---|
126 | * Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
|
---|
127 | return std::vector<result_t>(1, result);
|
---|
128 | }
|
---|
129 |
|
---|
130 | PairPotential_Harmonic::derivative_components_t
|
---|
131 | PairPotential_Harmonic::derivative(
|
---|
132 | const arguments_t &arguments
|
---|
133 | ) const
|
---|
134 | {
|
---|
135 | ASSERT( arguments.size() == 1,
|
---|
136 | "PairPotential_Harmonic::operator() - requires exactly one argument.");
|
---|
137 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
138 | arguments, getParticleTypes()),
|
---|
139 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
140 | derivative_components_t result;
|
---|
141 | const argument_t &r_ij = arguments[0];
|
---|
142 | result.push_back( 2. * params[spring_constant] * ( r_ij.distance - params[equilibrium_distance]) );
|
---|
143 | ASSERT( result.size() == 1,
|
---|
144 | "PairPotential_Harmonic::operator() - we did not create exactly one component.");
|
---|
145 | return result;
|
---|
146 | }
|
---|
147 |
|
---|
148 | PairPotential_Harmonic::results_t
|
---|
149 | PairPotential_Harmonic::parameter_derivative(
|
---|
150 | const arguments_t &arguments,
|
---|
151 | const size_t index
|
---|
152 | ) const
|
---|
153 | {
|
---|
154 | ASSERT( arguments.size() == 1,
|
---|
155 | "PairPotential_Harmonic::parameter_derivative() - requires exactly one argument.");
|
---|
156 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
157 | arguments, getParticleTypes()),
|
---|
158 | "PairPotential_Harmonic::operator() - types don't match with ones in arguments.");
|
---|
159 | const argument_t &r_ij = arguments[0];
|
---|
160 | switch (index) {
|
---|
161 | case spring_constant:
|
---|
162 | {
|
---|
163 | const result_t result =
|
---|
164 | Helpers::pow( r_ij.distance - params[equilibrium_distance], 2 );
|
---|
165 | return std::vector<result_t>(1, result);
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | case equilibrium_distance:
|
---|
169 | {
|
---|
170 | const result_t result =
|
---|
171 | -2. * params[spring_constant]
|
---|
172 | * ( r_ij.distance - params[equilibrium_distance]);
|
---|
173 | return std::vector<result_t>(1, result);
|
---|
174 | break;
|
---|
175 | }
|
---|
176 | default:
|
---|
177 | ASSERT(0, "PairPotential_Harmonic::parameter_derivative() - derivative to unknown parameter desired.");
|
---|
178 | break;
|
---|
179 | }
|
---|
180 |
|
---|
181 | return PairPotential_Harmonic::results_t(1, 0.);
|
---|
182 | }
|
---|
183 |
|
---|
184 | FunctionModel::extractor_t
|
---|
185 | PairPotential_Harmonic::getFragmentSpecificExtractor() const
|
---|
186 | {
|
---|
187 | Fragment::charges_t charges;
|
---|
188 | charges.resize(getParticleTypes().size());
|
---|
189 | std::transform(getParticleTypes().begin(), getParticleTypes().end(),
|
---|
190 | charges.begin(), boost::lambda::_1);
|
---|
191 | FunctionModel::extractor_t returnfunction =
|
---|
192 | boost::bind(&Extractors::gatherDistancesFromFragment,
|
---|
193 | boost::bind(&Fragment::getPositions, _1),
|
---|
194 | boost::bind(&Fragment::getCharges, _1),
|
---|
195 | charges,
|
---|
196 | _2);
|
---|
197 | return returnfunction;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void
|
---|
201 | PairPotential_Harmonic::setParametersToRandomInitialValues(
|
---|
202 | const TrainingData &data)
|
---|
203 | {
|
---|
204 | params[PairPotential_Harmonic::equilibrium_distance] = 3e+0*rand()/(double)RAND_MAX + .5;// 1.;
|
---|
205 | params[PairPotential_Harmonic::spring_constant] = 1e+0*rand()/(double)RAND_MAX;// 0.2;
|
---|
206 | }
|
---|
207 |
|
---|