source: src/Potentials/Specifics/PairPotential_LennardJones.cpp@ 35760a

Fix_FitPotential_needs_atomicnumbers
Last change on this file since 35760a was 35760a, checked in by Frederik Heber <heber@…>, 9 years ago

tempcommit: Merge with b794f5e8

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2013 Frederik Heber. 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_LennardJones.cpp
26 *
27 * Created on: Jul 05, 2013
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_LennardJones.hpp"
40
41#include <boost/assign/list_of.hpp> // for 'map_list_of()'
42#include <boost/bind.hpp>
43#include <boost/lambda/lambda.hpp>
44#include <cmath>
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/InternalCoordinates/TwoBody_Length.hpp"
53#include "Potentials/ParticleTypeCheckers.hpp"
54#include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
55#include "RandomNumbers/RandomNumberGenerator.hpp"
56
57class Fragment;
58
59// static definitions
60const PairPotential_LennardJones::ParameterNames_t
61PairPotential_LennardJones::ParameterNames =
62 boost::assign::list_of<std::string>
63 ("epsilon")
64 ("sigma")
65 ;
66const std::string PairPotential_LennardJones::potential_token("lennardjones");
67Coordinator::ptr PairPotential_LennardJones::coordinator(Memory::ignore(new TwoBody_Length()));
68static PotentialSubgraph generateSubgraph()
69{
70 return PotentialSubgraph();
71}
72const PotentialSubgraph PairPotential_LennardJones::subgraph(generateSubgraph());
73
74void PairPotential_LennardJones::setDefaultParameters()
75{
76 params[epsilon] = 1e-5;
77 params[sigma] = 8.2;
78}
79
80PairPotential_LennardJones::PairPotential_LennardJones() :
81 EmpiricalPotential(),
82 params(parameters_t(MAXPARAMS, 0.))
83{
84 // have some decent defaults for parameter_derivative checking
85 setDefaultParameters();
86}
87
88PairPotential_LennardJones::PairPotential_LennardJones(
89 const ParticleTypes_t &_ParticleTypes
90 ) :
91 EmpiricalPotential(_ParticleTypes),
92 params(parameters_t(MAXPARAMS, 0.))
93{
94 // have some decent defaults for parameter_derivative checking
95 setDefaultParameters();
96}
97
98PairPotential_LennardJones::PairPotential_LennardJones(
99 const ParticleTypes_t &_ParticleTypes,
100 const double _epsilon,
101 const double _sigma
102 ) :
103 EmpiricalPotential(_ParticleTypes),
104 params(parameters_t(MAXPARAMS, 0.))
105{
106 params[epsilon] = _epsilon;
107 params[sigma] = _sigma;
108}
109
110void PairPotential_LennardJones::setParameters(const parameters_t &_params)
111{
112 const size_t paramsDim = _params.size();
113 ASSERT( paramsDim <= getParameterDimension(),
114 "PairPotential_LennardJones::setParameters() - we need not more than "
115 +toString(getParameterDimension())+" parameters.");
116 for(size_t i=0;i<paramsDim;++i)
117 params[i] = _params[i];
118
119#ifndef NDEBUG
120 parameters_t check_params(getParameters());
121 check_params.resize(paramsDim); // truncate to same size
122 ASSERT( check_params == _params,
123 "PairPotential_LennardJones::setParameters() - failed, mismatch in to be set "
124 +toString(_params)+" and set "+toString(check_params)+" params.");
125#endif
126}
127
128PairPotential_LennardJones::results_t
129PairPotential_LennardJones::operator()(
130 const list_of_arguments_t &listarguments
131 ) const
132{
133 result_t result = 0.;
134 for(list_of_arguments_t::const_iterator iter = listarguments.begin();
135 iter != listarguments.end(); ++iter) {
136 const arguments_t &arguments = *iter;
137 ASSERT( arguments.size() == 1,
138 "PairPotential_LennardJones::operator() - requires one argument.");
139 ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
140 arguments, getParticleTypes()),
141 "PairPotential_LennardJones::operator() - types don't match with ones in arguments.");
142 const double &r = arguments[0].distance;
143 const double temp = Helpers::pow(params[sigma]/r, 6);
144 result += 4.*params[epsilon] * (temp*temp - temp);
145 }
146 return results_t(1, result);
147}
148
149PairPotential_LennardJones::derivative_components_t
150PairPotential_LennardJones::derivative(
151 const list_of_arguments_t &listarguments
152 ) const
153{
154 const double sigma6 = Helpers::pow(params[sigma], 6);
155 result_t result = 0.;
156 for(list_of_arguments_t::const_iterator iter = listarguments.begin();
157 iter != listarguments.end(); ++iter) {
158 const arguments_t &arguments = *iter;
159 ASSERT( arguments.size() == 1,
160 "PairPotential_LennardJones::operator() - requires no argument.");
161 ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
162 arguments, getParticleTypes()),
163 "PairPotential_LennardJones::operator() - types don't match with ones in arguments.");
164 const double &r = arguments[0].distance;
165 result +=
166 4.*params[epsilon] * (
167 sigma6*sigma6*(-12.) / Helpers::pow(r,13)
168 - sigma6*(-6.) /Helpers::pow(r,7)
169 );
170 }
171 return derivative_components_t(1, result);
172}
173
174PairPotential_LennardJones::results_t
175PairPotential_LennardJones::parameter_derivative(
176 const list_of_arguments_t &listarguments,
177 const size_t index
178 ) const
179{
180 result_t result = 0.;
181 for(list_of_arguments_t::const_iterator iter = listarguments.begin();
182 iter != listarguments.end(); ++iter) {
183 const arguments_t &arguments = *iter;
184 ASSERT( arguments.size() == 1,
185 "PairPotential_LennardJones::parameter_derivative() - requires no argument.");
186 ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
187 arguments, getParticleTypes()),
188 "PairPotential_LennardJones::operator() - types don't match with ones in arguments.");
189 const double &r = arguments[0].distance;
190 switch (index) {
191 case epsilon:
192 {
193 const double temp = Helpers::pow(params[sigma]/r, 6);
194 result += 4. * (temp*temp - temp);
195 break;
196 }
197 case sigma:
198 {
199 const double r6 = Helpers::pow(r, 6);
200 result +=
201 4.*params[epsilon] * (
202 12. * Helpers::pow(params[sigma],11)/(r6*r6)
203 - 6. * Helpers::pow(params[sigma],5)/r6
204 );
205 break;
206 }
207 default:
208 break;
209 }
210 }
211 return results_t(1, result);
212}
213
214FunctionModel::filter_t PairPotential_LennardJones::getSpecificFilter() const
215{
216 FunctionModel::filter_t returnfunction =
217 boost::bind(&Extractors::filterArgumentsByParticleTypes,
218 _1,
219 getParticleTypes(), getSubgraph());
220 return returnfunction;
221}
222
223void
224PairPotential_LennardJones::setParametersToRandomInitialValues(
225 const TrainingData &data)
226{
227 RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
228 const double rng_min = random.min();
229 const double rng_max = random.max();
230 params[PairPotential_LennardJones::epsilon] = 1e-2*(random()/(rng_max-rng_min));
231 params[PairPotential_LennardJones::sigma] = (3.+10.*(random()/(rng_max-rng_min)));// 0.5;
232}
233
Note: See TracBrowser for help on using the repository browser.