source: src/Potentials/PotentialFactory.cpp@ a0d8aa

Candidate_v1.7.0 stable
Last change on this file since a0d8aa was a0d8aa, checked in by Frederik Heber <frederik.heber@…>, 8 weeks ago

PotentialFactory exposes particle type numbers.

  • Also, potentials use getParticleTypeNumber() in asserts.
  • Property mode set to 100644
File size: 6.7 KB
Line 
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 * PotentialFactory.cpp
27 *
28 * Created on: 30.11.2012
29 * Author: heber
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 "PotentialFactory.hpp"
40
41#include <boost/assign/list_of.hpp>
42#include <boost/lambda/lambda.hpp>
43#include <boost/preprocessor/seq/elem.hpp>
44#include <boost/preprocessor/seq/seq.hpp>
45#include <boost/preprocessor/seq/size.hpp>
46#include <boost/preprocessor/stringize.hpp>
47#include <boost/preprocessor/iteration/local.hpp>
48
49#include "CodePatterns/Singleton_impl.hpp"
50
51#include "Potentials/SerializablePotential.hpp"
52
53#include "AllPotentialHeaders.hpp"
54
55#include "Potentials/Specifics/PotentialTypes.def"
56
57//!> function to print each member of the sequence in the enumeration
58#define sequence_print(z,n,seq) \
59 ( \
60 BOOST_PP_STRINGIZE( BOOST_PP_SEQ_ELEM(n, seq) ), \
61 BOOST_PP_SEQ_ELEM(n, seq) \
62 )
63
64//!> function to print each member of the sequence in the enumeration
65#define reverse_sequence_print(z,n,seq) \
66 ( \
67 BOOST_PP_SEQ_ELEM(n, seq), \
68 BOOST_PP_STRINGIZE( BOOST_PP_SEQ_ELEM(n, seq) ) \
69 )
70
71// static defines
72PotentialFactory::NameToType_t PotentialFactory::NameToType =
73 boost::assign::map_list_of
74#if defined PotentialTypes_END // do we have parameters at all?
75#define BOOST_PP_LOCAL_MACRO(n) sequence_print(~, n, POTENTIALSEQUENCE)
76#define BOOST_PP_LOCAL_LIMITS (0, PotentialTypes_END-1)
77#include BOOST_PP_LOCAL_ITERATE()
78#undef BOOST_PP_LOCAL_MACRO
79#undef BOOST_PP_LOCAL_LIMITS
80#endif
81 ;
82PotentialFactory::TypeToName_t PotentialFactory::TypeToName =
83 boost::assign::map_list_of
84#if defined PotentialTypes_END // do we have parameters at all?
85#define BOOST_PP_LOCAL_MACRO(n) reverse_sequence_print(~, n, POTENTIALSEQUENCE)
86#define BOOST_PP_LOCAL_LIMITS (0, PotentialTypes_END-1)
87#include BOOST_PP_LOCAL_ITERATE()
88#undef BOOST_PP_LOCAL_MACRO
89#undef BOOST_PP_LOCAL_LIMITS
90#endif
91 ;
92
93#undef sequence_print
94#undef reverse_sequence_print
95
96#include "Potentials/Specifics/PotentialTypes.undef"
97
98enum PotentialTypes PotentialFactory::getTypeForName(const std::string &_name)
99{
100 ASSERT (NameToType.count(_name),
101 "PotentialFactory::getTypeForName() - cannot find potential of name "+_name);
102 return NameToType[_name];
103}
104
105const std::string & PotentialFactory::getNameForType(enum PotentialTypes _type)
106{
107 ASSERT (TypeToName.count(_type),
108 "PotentialFactory::getNameForType() - cannot find potential of name "+_type);
109 return TypeToName[_type];
110}
111
112unsigned int PotentialFactory::getPotentialsParticleTypeNumber(const std::string &_name) const {
113 EmpiricalPotential * const defaultPotential = getDefaultPotential(_name);
114 if (defaultPotential == NULL)
115 return 0;
116 const unsigned int particleTypeNumber = defaultPotential->getParticleTypeNumber();
117 delete defaultPotential;
118 return particleTypeNumber;
119}
120
121EmpiricalPotential *PotentialFactory::createInstance(
122 const std::string &potentialtype,
123 const SerializablePotential::ParticleTypes_t &types) const
124{
125 switch (getTypeForName(potentialtype)) {
126 case constant:
127 return new ConstantPotential(types);
128 case tersoff:
129 return new ManyBodyPotential_Tersoff(types);
130 case morse:
131 return new PairPotential_Morse(types);
132 case harmonic_bond:
133 return new PairPotential_Harmonic(types);
134 case harmonic_angle:
135 return new ThreeBodyPotential_Angle(types);
136 case lennardjones:
137 return new PairPotential_LennardJones(types);
138 case torsion:
139 return new FourBodyPotential_Torsion(types);
140 case improper:
141 return new FourBodyPotential_Improper(types);
142 default:
143 ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create.");
144 }
145 return NULL;
146}
147
148EmpiricalPotential *PotentialFactory::createInstance(
149 const std::string &potentialtype,
150 const SerializablePotential::ParticleTypes_t &types,
151 const FunctionModel::parameters_t &params) const
152{
153 EmpiricalPotential *potential = NULL;
154 switch (getTypeForName(potentialtype)) {
155 case constant:
156 potential = new ConstantPotential(types);
157 break;
158 case tersoff:
159 potential = new ManyBodyPotential_Tersoff(types);
160 break;
161 case morse:
162 potential = new PairPotential_Morse(types);
163 break;
164 case harmonic_bond:
165 potential = new PairPotential_Harmonic(types);
166 break;
167 case harmonic_angle:
168 potential = new ThreeBodyPotential_Angle(types);
169 break;
170 case lennardjones:
171 potential = new PairPotential_LennardJones(types);
172 break;
173 case torsion:
174 potential = new FourBodyPotential_Torsion(types);
175 break;
176 case improper:
177 potential = new FourBodyPotential_Improper(types);
178 break;
179 default:
180 ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create.");
181 break;
182 }
183 if (potential != NULL)
184 potential->setParameters(params);
185 return potential;
186}
187
188EmpiricalPotential* PotentialFactory::getDefaultPotential(const std::string &_name) const
189{
190 switch (getTypeForName(_name)) {
191 case constant:
192 return new ConstantPotential();
193 case tersoff:
194 return new ManyBodyPotential_Tersoff();
195 case morse:
196 return new PairPotential_Morse();
197 case harmonic_bond:
198 return new PairPotential_Harmonic();
199 case harmonic_angle:
200 return new ThreeBodyPotential_Angle();
201 case lennardjones:
202 return new PairPotential_LennardJones();
203 case torsion:
204 return new FourBodyPotential_Torsion();
205 case improper:
206 return new FourBodyPotential_Improper();
207 default:
208 ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create.");
209 break;
210 }
211 return NULL;
212}
213
214EmpiricalPotential const * const PotentialFactory::getDefaultPotential(const enum PotentialTypes &_type) const {
215 return const_cast<EmpiricalPotential const * const>(getDefaultPotential(getNameForType(_type)));
216}
217
218CONSTRUCT_SINGLETON(PotentialFactory)
Note: See TracBrowser for help on using the repository browser.