Line data Source code
1 : // Copyright (C) 2016 Technische Universitaet Muenchen 2 : // This file is part of the Mamico project. For conditions of distribution 3 : // and use, please see the copyright notice in Mamico's main folder, or at 4 : // www5.in.tum.de/mamico 5 : #ifndef _MDSIMULATIONFACTORY_H_ 6 : #define _MDSIMULATIONFACTORY_H_ 7 : 8 : #include <ctime> 9 : #include <math.h> 10 : #include <string> 11 : 12 : // hacked: "currently, only 3D is supported!" (Defined before including headers in ./MDSimulationFactory/ using it) 13 : #define MDSIMULATIONFACTORY_DIMENSION 3 14 : 15 : #include "coupling/CouplingMDDefinitions.h" 16 : #include "coupling/configurations/MaMiCoConfiguration.h" 17 : #include "coupling/interface/MDSimulation.h" 18 : #include "coupling/interface/MamicoInterfaceProvider.h" 19 : #include "simplemd/configurations/MolecularDynamicsConfiguration.h" 20 : #include "tarch/la/ScalarOperations.h" 21 : #include "tarch/utils/MultiMDService.h" 22 : 23 : #if defined(SIMPLE_MD) 24 : #include "coupling/interface/impl/SimpleMD/SimpleMDSolverInterface.h" 25 : #include "coupling/interface/impl/SimpleMD/SimpleMDLinkedCellWrapper.h" 26 : #define MY_LINKEDCELL coupling::interface::SimpleMDLinkedCellWrapper 27 : #include "coupling/interface/impl/SimpleMD/SimpleMDSimulation.h" 28 : #elif defined(LAMMPS_MD) || defined(LAMMPS_DPD) 29 : #if defined(LAMMPS_MD) 30 : #include "coupling/interface/impl/LAMMPS/LammpsMDSimulation.h" 31 : #else // LAMMPS_DPD 32 : #include "coupling/interface/impl/LAMMPS/LammpsDPDSimulation.h" 33 : #endif 34 : #define MY_LINKEDCELL LAMMPS_NS::MamicoCell 35 : #elif defined(LS1_MARDYN) 36 : #include "coupling/interface/impl/ls1/LS1MDSimulation.h" 37 : #include "coupling/interface/impl/ls1/LS1MDSolverInterface.h" 38 : #include "coupling/interface/impl/ls1/LS1RegionWrapper.h" 39 : #include "coupling/interface/impl/ls1/LS1StaticCommData.h" 40 : #define MY_LINKEDCELL ls1::LS1RegionWrapper 41 : #else 42 : #error "No MD solver defined!" 43 : #endif 44 : 45 : /** interface for different MD solvers. 46 : * @author Philipp Neumann 47 : */ 48 : namespace coupling { 49 : namespace interface { 50 : 51 : /** factory to produced md simulation, md solver interface (for mamico) and the 52 : *coupling cell service using singleton pattern. 53 : * @brief factory to produced md simulation, md solver interface (for 54 : *mamico) and the coupling cell service 55 : * @author Philipp Neumann 56 : */ 57 : class SimulationAndInterfaceFactory { 58 : public: 59 : /** @returns the SimulationAndInterfaceFactory object 60 : * @note singleton pattern 61 : */ 62 24 : static SimulationAndInterfaceFactory& getInstance() { 63 28 : static SimulationAndInterfaceFactory singleton; 64 24 : return singleton; 65 : } 66 : 67 : /** returns a pointer to the md simulation. 68 : * @param configuration 69 : * @param mamicoConfiguration 70 : * @param localComm 71 : * @remark This will create a new simulation which needs to be deleted at 72 : *the end 73 : */ 74 12 : coupling::interface::MDSimulation* getMDSimulation(const simplemd::configurations::MolecularDynamicsConfiguration& configuration, 75 : const coupling::configurations::MaMiCoConfiguration<MDSIMULATIONFACTORY_DIMENSION>& mamicoConfiguration 76 : #if (COUPLING_MD_PARALLEL == COUPLING_MD_YES) 77 : , 78 : MPI_Comm localComm 79 : #endif 80 : ) { 81 : #if defined(SIMPLE_MD) 82 12 : return new coupling::interface::SimpleMDSimulation(configuration); 83 : #elif defined(LAMMPS_MD) 84 : return new coupling::interface::LammpsMDSimulation(configuration, mamicoConfiguration 85 : #if (COUPLING_MD_PARALLEL == COUPLING_MD_YES) 86 : , 87 : localComm 88 : #endif 89 : ); 90 : #elif defined(LAMMPS_DPD) 91 : return new coupling::interface::LammpsDPDSimulation(configuration, mamicoConfiguration 92 : #if (COUPLING_MD_PARALLEL == COUPLING_MD_YES) 93 : , 94 : localComm 95 : #endif 96 : ); 97 : #elif defined(LS1_MARDYN) 98 : return new coupling::interface::LS1MDSimulation(configuration 99 : #if (COUPLING_MD_PARALLEL == COUPLING_MD_YES) 100 : , 101 : localComm 102 : #endif 103 : ); 104 : #else 105 : std::cout << "ERROR MDSimulationFactory::getMDSimulation(): Unknown MD " 106 : "simulation!" 107 : << std::endl; 108 : exit(EXIT_FAILURE); 109 : return NULL; 110 : #endif 111 : } 112 : 113 : /** returns the MD solver interface. This method should be called AFTER 114 : * initialising the MD simulation AND AFTER the equilibration of MD. We thus 115 : * expect that getMDSimulationInterface() and switchOnCoupling() of the 116 : * MDSimulationInterface() have been called before. 117 : * @param configuration 118 : * @param mamicoConfiguration 119 : * @param mdSimulation 120 : */ 121 : coupling::interface::MDSolverInterface<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>* 122 12 : getMDSolverInterface(const simplemd::configurations::MolecularDynamicsConfiguration& configuration, 123 : const coupling::configurations::MaMiCoConfiguration<MDSIMULATIONFACTORY_DIMENSION>& mamicoConfiguration, 124 : coupling::interface::MDSimulation* mdSimulation) { 125 12 : coupling::interface::MDSolverInterface<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>* mdSolverInterface = NULL; 126 : #if defined(SIMPLE_MD) 127 : // for the simple MD code, we create a new md solver interface and also add 128 : // it to the mamico interface provider (the latter is not really required, 129 : // but makes the simulation state more consistent in the overall simulation) 130 12 : coupling::interface::SimpleMDSimulation* simpleMDSimulation = (coupling::interface::SimpleMDSimulation*)mdSimulation; 131 12 : if (simpleMDSimulation == NULL) { 132 0 : std::cout << "ERROR MDSimulationFactory::getMDSolverInterface(): Could " 133 0 : "not cast to SimpleMDSimulation!" 134 0 : << std::endl; 135 0 : exit(EXIT_FAILURE); 136 : } 137 12 : mdSolverInterface = new coupling::interface::SimpleMDSolverInterface( 138 12 : simpleMDSimulation->getBoundaryTreatment(), simpleMDSimulation->getParallelTopologyService(), simpleMDSimulation->getMoleculeService(), 139 12 : simpleMDSimulation->getMolecularPropertiesService(), (simpleMDSimulation->getParallelTopologyService()).getLocalBoundaryInformation(), 140 12 : configuration.getSimulationConfiguration().getDt()); 141 12 : coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().setMDSolverInterface(mdSolverInterface); 142 : #elif defined(LAMMPS_MD) 143 : // as switchOnCoupling() should have been called before this method, the 144 : // fix-mamico should be already initialised. hence the MDSolverInterface of 145 : // the mamico interface provider should be initialised at this stage 146 : mdSolverInterface = coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().getMDSolverInterface(); 147 : #elif defined(LAMMPS_DPD) 148 : mdSolverInterface = coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().getMDSolverInterface(); 149 : #elif defined(LS1_MARDYN) 150 : mdSolverInterface = new coupling::interface::LS1MDSolverInterface(mamicoConfiguration.getCouplingCellConfiguration().getCouplingCellSize(), 151 : mamicoConfiguration.getCouplingCellConfiguration().getNumberLinkedCellsPerCouplingCell()); 152 : coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().setMDSolverInterface(mdSolverInterface); 153 : #endif 154 : 155 12 : if (mdSolverInterface == NULL) { 156 0 : std::cout << "ERROR MDSimulationFactory::getMDSolverInterface(): " 157 0 : "mdSolverInterface==NULL!" 158 0 : << std::endl; 159 0 : exit(EXIT_FAILURE); 160 : } 161 12 : return mdSolverInterface; 162 : } 163 : 164 : /** shuts down the MD solver interfaces and deletes the interface if required 165 : (depending on the respective MD simulation) */ 166 : void shutdownMDSolverInterface() { 167 : #if defined(SIMPLE_MD) 168 : coupling::interface::MDSolverInterface<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>* interface = 169 : coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().getMDSolverInterface(); 170 : if (interface != NULL) { 171 : delete interface; 172 : } 173 : coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().setMDSolverInterface(NULL); 174 : #elif defined(LAMMPS_MD) 175 : // nop, since the fix-mamico takes care of deleting the MD solver interface 176 : #elif defined(LAMMPS_DPD) 177 : // nop 178 : #elif defined(LS1_MARDYN) 179 : coupling::interface::MDSolverInterface<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>* interface = 180 : coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().getMDSolverInterface(); 181 : if (interface != NULL) { 182 : delete interface; 183 : } 184 : coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().setMDSolverInterface(NULL); 185 : #endif 186 : } 187 : 188 : private: 189 : /** Private constructor 190 : * @note singelton pattern 191 : */ 192 4 : SimulationAndInterfaceFactory() {} 193 : /** Private destructor 194 : * @note singelton pattern 195 : */ 196 4 : ~SimulationAndInterfaceFactory() {} 197 : }; 198 : 199 : } // namespace interface 200 : } // namespace coupling 201 : #endif // _MDSIMULATIONFACTORY_H_