| 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 | 
|---|
| 72 | PotentialFactory::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 | ; | 
|---|
| 82 | PotentialFactory::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 |  | 
|---|
| 98 | EmpiricalPotential *PotentialFactory::createInstance( | 
|---|
| 99 | const std::string &potentialtype, | 
|---|
| 100 | const SerializablePotential::ParticleTypes_t &types) const | 
|---|
| 101 | { | 
|---|
| 102 | ASSERT (NameToType.count(potentialtype), | 
|---|
| 103 | "PotentialFactory::createInstance() - cannot find potential of name "+potentialtype); | 
|---|
| 104 | switch (NameToType[potentialtype]) { | 
|---|
| 105 | case constant: | 
|---|
| 106 | return new ConstantPotential(types); | 
|---|
| 107 | case tersoff: | 
|---|
| 108 | return new ManyBodyPotential_Tersoff(types); | 
|---|
| 109 | case morse: | 
|---|
| 110 | return new PairPotential_Morse(types); | 
|---|
| 111 | case harmonic_bond: | 
|---|
| 112 | return new PairPotential_Harmonic(types); | 
|---|
| 113 | case harmonic_angle: | 
|---|
| 114 | return new ThreeBodyPotential_Angle(types); | 
|---|
| 115 | case lennardjones: | 
|---|
| 116 | return new PairPotential_LennardJones(types); | 
|---|
| 117 | case torsion: | 
|---|
| 118 | return new FourBodyPotential_Torsion(types); | 
|---|
| 119 | case improper: | 
|---|
| 120 | return new FourBodyPotential_Improper(types); | 
|---|
| 121 | default: | 
|---|
| 122 | ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create."); | 
|---|
| 123 | } | 
|---|
| 124 | return NULL; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | EmpiricalPotential *PotentialFactory::createInstance( | 
|---|
| 128 | const std::string &potentialtype, | 
|---|
| 129 | std::istream &serialized) const | 
|---|
| 130 | { | 
|---|
| 131 | EmpiricalPotential *potential = NULL; | 
|---|
| 132 | ASSERT (NameToType.count(potentialtype), | 
|---|
| 133 | "PotentialFactory::createInstance() - cannot find potential of name "+potentialtype); | 
|---|
| 134 | switch (NameToType[potentialtype]) { | 
|---|
| 135 | case constant: | 
|---|
| 136 | potential = new ConstantPotential(); | 
|---|
| 137 | break; | 
|---|
| 138 | case tersoff: | 
|---|
| 139 | potential = new ManyBodyPotential_Tersoff(); | 
|---|
| 140 | break; | 
|---|
| 141 | case morse: | 
|---|
| 142 | potential = new PairPotential_Morse(); | 
|---|
| 143 | break; | 
|---|
| 144 | case harmonic_bond: | 
|---|
| 145 | potential = new PairPotential_Harmonic(); | 
|---|
| 146 | break; | 
|---|
| 147 | case harmonic_angle: | 
|---|
| 148 | potential = new ThreeBodyPotential_Angle(); | 
|---|
| 149 | break; | 
|---|
| 150 | case lennardjones: | 
|---|
| 151 | potential = new PairPotential_LennardJones(); | 
|---|
| 152 | break; | 
|---|
| 153 | case torsion: | 
|---|
| 154 | potential = new FourBodyPotential_Torsion(); | 
|---|
| 155 | break; | 
|---|
| 156 | case improper: | 
|---|
| 157 | potential = new FourBodyPotential_Improper(); | 
|---|
| 158 | break; | 
|---|
| 159 | default: | 
|---|
| 160 | ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create."); | 
|---|
| 161 | break; | 
|---|
| 162 | } | 
|---|
| 163 | // now stream in types and parameters | 
|---|
| 164 | potential->stream_from(serialized); | 
|---|
| 165 |  | 
|---|
| 166 | return potential; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | CONSTRUCT_SINGLETON(PotentialFactory) | 
|---|