| 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 | 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 | 
 | 
|---|
| 112 | EmpiricalPotential *PotentialFactory::createInstance(
 | 
|---|
| 113 |     const std::string &potentialtype,
 | 
|---|
| 114 |     const SerializablePotential::ParticleTypes_t &types) const
 | 
|---|
| 115 | {
 | 
|---|
| 116 |   switch (getTypeForName(potentialtype)) {
 | 
|---|
| 117 |   case constant:
 | 
|---|
| 118 |       return new ConstantPotential(types);
 | 
|---|
| 119 |   case tersoff:
 | 
|---|
| 120 |       return new ManyBodyPotential_Tersoff(types);
 | 
|---|
| 121 |   case morse:
 | 
|---|
| 122 |       return new PairPotential_Morse(types);
 | 
|---|
| 123 |   case harmonic_bond:
 | 
|---|
| 124 |       return new PairPotential_Harmonic(types);
 | 
|---|
| 125 |   case harmonic_angle:
 | 
|---|
| 126 |       return new ThreeBodyPotential_Angle(types);
 | 
|---|
| 127 |   case lennardjones:
 | 
|---|
| 128 |       return new PairPotential_LennardJones(types);
 | 
|---|
| 129 |   case torsion:
 | 
|---|
| 130 |       return new FourBodyPotential_Torsion(types);
 | 
|---|
| 131 |   case improper:
 | 
|---|
| 132 |       return new FourBodyPotential_Improper(types);
 | 
|---|
| 133 |   default:
 | 
|---|
| 134 |     ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create.");
 | 
|---|
| 135 |   }
 | 
|---|
| 136 |   return NULL;
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | EmpiricalPotential* PotentialFactory::getDefaultPotential(const std::string &_name) const
 | 
|---|
| 140 | {
 | 
|---|
| 141 |   EmpiricalPotential *potential = NULL;
 | 
|---|
| 142 |   switch (getTypeForName(_name)) {
 | 
|---|
| 143 |   case constant:
 | 
|---|
| 144 |     potential = new ConstantPotential();
 | 
|---|
| 145 |     break;
 | 
|---|
| 146 |   case tersoff:
 | 
|---|
| 147 |     potential = new ManyBodyPotential_Tersoff();
 | 
|---|
| 148 |     break;
 | 
|---|
| 149 |   case morse:
 | 
|---|
| 150 |     potential = new PairPotential_Morse();
 | 
|---|
| 151 |     break;
 | 
|---|
| 152 |   case harmonic_bond:
 | 
|---|
| 153 |     potential = new PairPotential_Harmonic();
 | 
|---|
| 154 |     break;
 | 
|---|
| 155 |   case harmonic_angle:
 | 
|---|
| 156 |     potential = new ThreeBodyPotential_Angle();
 | 
|---|
| 157 |     break;
 | 
|---|
| 158 |   case lennardjones:
 | 
|---|
| 159 |     potential = new PairPotential_LennardJones();
 | 
|---|
| 160 |     break;
 | 
|---|
| 161 |   case torsion:
 | 
|---|
| 162 |     potential = new FourBodyPotential_Torsion();
 | 
|---|
| 163 |     break;
 | 
|---|
| 164 |   case improper:
 | 
|---|
| 165 |     potential = new FourBodyPotential_Improper();
 | 
|---|
| 166 |     break;
 | 
|---|
| 167 |   default:
 | 
|---|
| 168 |     ASSERT(0, "PotentialFactory::createInstance() - unknown potential desired to create.");
 | 
|---|
| 169 |     break;
 | 
|---|
| 170 |   }
 | 
|---|
| 171 |   return potential;
 | 
|---|
| 172 | }
 | 
|---|
| 173 | 
 | 
|---|
| 174 | EmpiricalPotential const * const PotentialFactory::getDefaultPotential(const enum PotentialTypes &_type) const {
 | 
|---|
| 175 |   return const_cast<EmpiricalPotential const * const>(getDefaultPotential(getNameForType(_type)));
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | 
 | 
|---|
| 179 | CONSTRUCT_SINGLETON(PotentialFactory)
 | 
|---|