| [d76ce74] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2012 University of Bonn. All rights reserved. | 
|---|
| [060e1e] | 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| [d76ce74] | 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 |  | 
|---|
| [9eb71b3] | 37 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| [d76ce74] | 38 |  | 
|---|
|  | 39 | #include "PotentialFactory.hpp" | 
|---|
|  | 40 |  | 
|---|
|  | 41 | #include <boost/assign/list_of.hpp> | 
|---|
|  | 42 | #include <boost/lambda/lambda.hpp> | 
|---|
| [931077] | 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> | 
|---|
| [d76ce74] | 48 |  | 
|---|
|  | 49 | #include "CodePatterns/Singleton_impl.hpp" | 
|---|
|  | 50 |  | 
|---|
|  | 51 | #include "Potentials/SerializablePotential.hpp" | 
|---|
| [060e1e] | 52 |  | 
|---|
|  | 53 | #include "AllPotentialHeaders.hpp" | 
|---|
| [d76ce74] | 54 |  | 
|---|
| [931077] | 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 |  | 
|---|
| [d76ce74] | 71 | // static defines | 
|---|
|  | 72 | PotentialFactory::NameToType_t PotentialFactory::NameToType = | 
|---|
|  | 73 | boost::assign::map_list_of | 
|---|
| [931077] | 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 | 
|---|
| [d76ce74] | 81 | ; | 
|---|
|  | 82 | PotentialFactory::TypeToName_t PotentialFactory::TypeToName = | 
|---|
|  | 83 | boost::assign::map_list_of | 
|---|
| [931077] | 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 | 
|---|
| [d76ce74] | 91 | ; | 
|---|
|  | 92 |  | 
|---|
| [931077] | 93 | #undef sequence_print | 
|---|
|  | 94 | #undef reverse_sequence_print | 
|---|
|  | 95 |  | 
|---|
|  | 96 | #include "Potentials/Specifics/PotentialTypes.undef" | 
|---|
| [d76ce74] | 97 |  | 
|---|
| [f6abb1] | 98 | enum 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 |  | 
|---|
|  | 105 | const 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 |  | 
|---|
| [a0d8aa] | 112 | unsigned 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 |  | 
|---|
| [fdd23a] | 121 | EmpiricalPotential *PotentialFactory::createInstance( | 
|---|
| [d76ce74] | 122 | const std::string &potentialtype, | 
|---|
| [035c0d] | 123 | const SerializablePotential::ParticleTypes_t &types) const | 
|---|
| [d76ce74] | 124 | { | 
|---|
| [f6abb1] | 125 | switch (getTypeForName(potentialtype)) { | 
|---|
| [8203ce8] | 126 | case constant: | 
|---|
|  | 127 | return new ConstantPotential(types); | 
|---|
| [d76ce74] | 128 | case tersoff: | 
|---|
|  | 129 | return new ManyBodyPotential_Tersoff(types); | 
|---|
|  | 130 | case morse: | 
|---|
|  | 131 | return new PairPotential_Morse(types); | 
|---|
| [ff90e3] | 132 | case harmonic_bond: | 
|---|
| [d76ce74] | 133 | return new PairPotential_Harmonic(types); | 
|---|
|  | 134 | case harmonic_angle: | 
|---|
| [484e2a] | 135 | return new ThreeBodyPotential_Angle(types); | 
|---|
| [1413f4] | 136 | case lennardjones: | 
|---|
|  | 137 | return new PairPotential_LennardJones(types); | 
|---|
| [e2037e] | 138 | case torsion: | 
|---|
|  | 139 | return new FourBodyPotential_Torsion(types); | 
|---|
| [791a12] | 140 | case improper: | 
|---|
|  | 141 | return new FourBodyPotential_Improper(types); | 
|---|
| [d76ce74] | 142 | default: | 
|---|
|  | 143 | ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create."); | 
|---|
|  | 144 | } | 
|---|
|  | 145 | return NULL; | 
|---|
|  | 146 | } | 
|---|
|  | 147 |  | 
|---|
| [e83114] | 148 | EmpiricalPotential *PotentialFactory::createInstance( | 
|---|
|  | 149 | const std::string &potentialtype, | 
|---|
|  | 150 | const SerializablePotential::ParticleTypes_t &types, | 
|---|
|  | 151 | const FunctionModel::parameters_t ¶ms) 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 |  | 
|---|
| [f6abb1] | 188 | EmpiricalPotential* PotentialFactory::getDefaultPotential(const std::string &_name) const | 
|---|
| [b3b31e] | 189 | { | 
|---|
| [f6abb1] | 190 | switch (getTypeForName(_name)) { | 
|---|
| [b3b31e] | 191 | case constant: | 
|---|
| [e83114] | 192 | return new ConstantPotential(); | 
|---|
| [b3b31e] | 193 | case tersoff: | 
|---|
| [e83114] | 194 | return new ManyBodyPotential_Tersoff(); | 
|---|
| [b3b31e] | 195 | case morse: | 
|---|
| [e83114] | 196 | return new PairPotential_Morse(); | 
|---|
| [ff90e3] | 197 | case harmonic_bond: | 
|---|
| [e83114] | 198 | return new PairPotential_Harmonic(); | 
|---|
| [b3b31e] | 199 | case harmonic_angle: | 
|---|
| [e83114] | 200 | return new ThreeBodyPotential_Angle(); | 
|---|
| [1413f4] | 201 | case lennardjones: | 
|---|
| [e83114] | 202 | return new PairPotential_LennardJones(); | 
|---|
| [e2037e] | 203 | case torsion: | 
|---|
| [e83114] | 204 | return new FourBodyPotential_Torsion(); | 
|---|
| [791a12] | 205 | case improper: | 
|---|
| [e83114] | 206 | return new FourBodyPotential_Improper(); | 
|---|
| [b3b31e] | 207 | default: | 
|---|
|  | 208 | ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create."); | 
|---|
|  | 209 | break; | 
|---|
|  | 210 | } | 
|---|
| [e83114] | 211 | return NULL; | 
|---|
| [b3b31e] | 212 | } | 
|---|
|  | 213 |  | 
|---|
| [55c494] | 214 | EmpiricalPotential const * const PotentialFactory::getDefaultPotential(const enum PotentialTypes &_type) const { | 
|---|
|  | 215 | return const_cast<EmpiricalPotential const * const>(getDefaultPotential(getNameForType(_type))); | 
|---|
|  | 216 | } | 
|---|
|  | 217 |  | 
|---|
| [d76ce74] | 218 | CONSTRUCT_SINGLETON(PotentialFactory) | 
|---|