LCOV - code coverage report
Current view: top level - coupling/interface - MDSimulationFactory.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 27 70.4 %
Date: 2025-06-25 11:26:37 Functions: 4 4 100.0 %

          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 "simplemd/LinkedCell.h"
      26             : #define MY_LINKEDCELL simplemd::LinkedCell
      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             : #endif
      42             : 
      43             : /** interface for different MD solvers.
      44             :  *  @author Philipp Neumann
      45             :  */
      46             : namespace coupling {
      47             : namespace interface {
      48             : 
      49             : /** factory to produced md simulation, md solver interface (for mamico) and the
      50             :  *coupling cell service using singleton pattern.
      51             :  *      @brief factory to produced md simulation, md solver interface (for
      52             :  *mamico) and the coupling cell service
      53             :  *  @author Philipp Neumann
      54             :  */
      55             : class SimulationAndInterfaceFactory {
      56             : public:
      57             :   /** @returns the SimulationAndInterfaceFactory object
      58             :    *    @note singleton pattern
      59             :    */
      60          24 :   static SimulationAndInterfaceFactory& getInstance() {
      61          28 :     static SimulationAndInterfaceFactory singleton;
      62          24 :     return singleton;
      63             :   }
      64             : 
      65             :   /** returns a pointer to the md simulation.
      66             :    *  @param configuration
      67             :    *  @param mamicoConfiguration
      68             :    *  @param localComm
      69             :    *    @remark This will create a new simulation which needs to be deleted at
      70             :    *the end
      71             :    */
      72          12 :   coupling::interface::MDSimulation* getMDSimulation(const simplemd::configurations::MolecularDynamicsConfiguration& configuration,
      73             :                                                      const coupling::configurations::MaMiCoConfiguration<MDSIMULATIONFACTORY_DIMENSION>& mamicoConfiguration
      74             : #if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
      75             :                                                      ,
      76             :                                                      MPI_Comm localComm
      77             : #endif
      78             :   ) {
      79             : #if defined(SIMPLE_MD)
      80          12 :     return new coupling::interface::SimpleMDSimulation(configuration);
      81             : #elif defined(LAMMPS_MD)
      82             :     return new coupling::interface::LammpsMDSimulation(configuration, mamicoConfiguration
      83             : #if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
      84             :                                                        ,
      85             :                                                        localComm
      86             : #endif
      87             :     );
      88             : #elif defined(LAMMPS_DPD)
      89             :     return new coupling::interface::LammpsDPDSimulation(configuration, mamicoConfiguration
      90             : #if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
      91             :                                                         ,
      92             :                                                         localComm
      93             : #endif
      94             :     );
      95             : #elif defined(LS1_MARDYN)
      96             :     return new coupling::interface::LS1MDSimulation(configuration
      97             : #if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
      98             :                                                     ,
      99             :                                                     localComm
     100             : #endif
     101             :     );
     102             : #else
     103             :     std::cout << "ERROR MDSimulationFactory::getMDSimulation(): Unknown MD "
     104             :                  "simulation!"
     105             :               << std::endl;
     106             :     exit(EXIT_FAILURE);
     107             :     return NULL;
     108             : #endif
     109             :   }
     110             : 
     111             :   /** returns the MD solver interface. This method should be called AFTER
     112             :    * initialising the MD simulation AND AFTER the equilibration of MD. We thus
     113             :    * expect that getMDSimulationInterface() and switchOnCoupling() of the
     114             :    * MDSimulationInterface() have been called before.
     115             :    *  @param configuration
     116             :    *  @param mamicoConfiguration
     117             :    *  @param mdSimulation
     118             :    */
     119             :   coupling::interface::MDSolverInterface<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>*
     120          12 :   getMDSolverInterface(const simplemd::configurations::MolecularDynamicsConfiguration& configuration,
     121             :                        const coupling::configurations::MaMiCoConfiguration<MDSIMULATIONFACTORY_DIMENSION>& mamicoConfiguration,
     122             :                        coupling::interface::MDSimulation* mdSimulation) {
     123          12 :     coupling::interface::MDSolverInterface<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>* mdSolverInterface = NULL;
     124             : #if defined(SIMPLE_MD)
     125             :     // for the simple MD code, we create a new md solver interface and also add
     126             :     // it to the mamico interface provider (the latter is not really required,
     127             :     // but makes the simulation state more consistent in the overall simulation)
     128          12 :     coupling::interface::SimpleMDSimulation* simpleMDSimulation = (coupling::interface::SimpleMDSimulation*)mdSimulation;
     129          12 :     if (simpleMDSimulation == NULL) {
     130           0 :       std::cout << "ERROR MDSimulationFactory::getMDSolverInterface(): Could "
     131           0 :                    "not cast to SimpleMDSimulation!"
     132           0 :                 << std::endl;
     133           0 :       exit(EXIT_FAILURE);
     134             :     }
     135          12 :     mdSolverInterface = new coupling::interface::SimpleMDSolverInterface(
     136          12 :         simpleMDSimulation->getBoundaryTreatment(),
     137          12 :         simpleMDSimulation->getParallelTopologyService(), simpleMDSimulation->getMoleculeService(), simpleMDSimulation->getLinkedCellService(),
     138          12 :         simpleMDSimulation->getMolecularPropertiesService(), (simpleMDSimulation->getParallelTopologyService()).getLocalBoundaryInformation(),
     139          12 :         configuration.getSimulationConfiguration().getDt());
     140          12 :     coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().setMDSolverInterface(mdSolverInterface);
     141             : #elif defined(LAMMPS_MD)
     142             :     // as switchOnCoupling() should have been called before this method, the
     143             :     // fix-mamico should be already initialised. hence the MDSolverInterface of
     144             :     // the mamico interface provider should be initialised at this stage
     145             :     mdSolverInterface = coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().getMDSolverInterface();
     146             : #elif defined(LAMMPS_DPD)
     147             :     mdSolverInterface = coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().getMDSolverInterface();
     148             : #elif defined(LS1_MARDYN)
     149             :     mdSolverInterface = new coupling::interface::LS1MDSolverInterface(mamicoConfiguration.getCouplingCellConfiguration().getCouplingCellSize(),
     150             :                                                                       mamicoConfiguration.getCouplingCellConfiguration().getNumberLinkedCellsPerCouplingCell());
     151             :     coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().setMDSolverInterface(mdSolverInterface);
     152             : #endif
     153             : 
     154          12 :     if (mdSolverInterface == NULL) {
     155           0 :       std::cout << "ERROR MDSimulationFactory::getMDSolverInterface(): "
     156           0 :                    "mdSolverInterface==NULL!"
     157           0 :                 << std::endl;
     158           0 :       exit(EXIT_FAILURE);
     159             :     }
     160          12 :     return mdSolverInterface;
     161             :   }
     162             : 
     163             :   /** shuts down the MD solver interfaces and deletes the interface if required
     164             :      (depending on the respective MD simulation) */
     165             :   void shutdownMDSolverInterface() {
     166             : #if defined(SIMPLE_MD)
     167             :     coupling::interface::MDSolverInterface<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>* interface =
     168             :         coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().getMDSolverInterface();
     169             :     if (interface != NULL) {
     170             :       delete interface;
     171             :     }
     172             :     coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().setMDSolverInterface(NULL);
     173             : #elif defined(LAMMPS_MD)
     174             : // nop, since the fix-mamico takes care of deleting the MD solver interface
     175             : #elif defined(LAMMPS_DPD)
     176             : // nop
     177             : #elif defined(LS1_MARDYN)
     178             :     coupling::interface::MDSolverInterface<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>* interface =
     179             :         coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().getMDSolverInterface();
     180             :     if (interface != NULL) {
     181             :       delete interface;
     182             :     }
     183             :     coupling::interface::MamicoInterfaceProvider<MY_LINKEDCELL, MDSIMULATIONFACTORY_DIMENSION>::getInstance().setMDSolverInterface(NULL);
     184             : #endif
     185             :   }
     186             : 
     187             : private:
     188             :   /** Private constructor
     189             :    *  @note singelton pattern
     190             :    */
     191           4 :   SimulationAndInterfaceFactory() {}
     192             :   /** Private destructor
     193             :    *  @note singelton pattern
     194             :    */
     195           4 :   ~SimulationAndInterfaceFactory() {}
     196             : };
     197             : 
     198             : } // namespace interface
     199             : } // namespace coupling
     200             : #endif // _MDSIMULATIONFACTORY_H_

Generated by: LCOV version 1.14