LCOV - code coverage report
Current view: top level - coupling - CouplingCellPlotter.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 153 0
Test Date: 2026-08-21 15:14:06 Functions: 0.0 % 7 0

            Line data    Source code
       1              : // Copyright (C) 2015 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 _MOLECULARDYNAMICS_COUPLING_COUPLINGCELLPLOTTER_H_
       6              : #define _MOLECULARDYNAMICS_COUPLING_COUPLINGCELLPLOTTER_H_
       7              : 
       8              : #include "coupling/KineticEnergyController.h"
       9              : #include "coupling/cell-mappings/ComputeMassMapping.h"
      10              : #include "coupling/cell-mappings/ComputeMomentumMapping.h"
      11              : #include "coupling/cell-mappings/VTKMoleculePlotter.h"
      12              : #include "coupling/datastructures/LinkedCellContainer.h"
      13              : #include "coupling/interface/MDSolverInterface.h"
      14              : #include "tarch/la/Vector.h"
      15              : #include <fstream>
      16              : #include <sstream>
      17              : 
      18              : namespace coupling {
      19              : template <class LinkedCell, unsigned int dim> class CouplingCellPlotter;
      20              : }
      21              : 
      22              : /** @brief plots the coupling cell data.
      23              :  *  @author Philipp Neumann
      24              :  *  @tparam LinkedCell the LinkedCell class is given by the implementation of
      25              :  * linked cells in the molecular dynamics simulation
      26              :  *  @tparam dim  refers to the spacial dimension of the simulation, can be 1, 2,
      27              :  * or 3*/
      28              : template <class LinkedCell, unsigned int dim> class coupling::CouplingCellPlotter {
      29              : public:
      30              :   /** @brief a simple constructor
      31              :    *  @param ID the id of the md simulation
      32              :    *  @param filename name for the file
      33              :    *  @param rank mpi rank of the current process
      34              :    *  @param t number of time step to plot
      35              :    *  @param cells coupling cells to plot
      36              :    *  @param mdSolverInterface interface of the md solver */
      37            0 :   CouplingCellPlotter(unsigned int ID, std::string filename, unsigned int rank, unsigned int t, datastructures::LinkedCellContainer<LinkedCell, dim>& cells,
      38              :                       coupling::interface::MDSolverInterface<LinkedCell, dim>* const mdSolverInterface) {
      39            0 :     plotMoleculeFile(ID, filename, rank, t, cells, mdSolverInterface);
      40            0 :     plotCouplingCellFile(ID, filename, rank, t, cells, mdSolverInterface);
      41            0 :   }
      42              : 
      43              :   /** @brief a simple destructor*/
      44            0 :   ~CouplingCellPlotter() {}
      45              : 
      46              : private:
      47              :   /** the file will contain the velocity, position, and potentials of the
      48              :    * molecules
      49              :    *  @brief writes molecule information to VTK file.
      50              :    *  @param ID the id of the md simulation
      51              :    *  @param filename name for the file
      52              :    *  @param rank mpi rank of the current process
      53              :    *  @param t number of time step to plot
      54              :    *  @param cells coupling cells to plot
      55              :    *  @param mdSolverInterface interface of the md solver */
      56            0 :   void plotMoleculeFile(unsigned int ID, std::string filename, unsigned int rank, unsigned int t,
      57              :                         coupling::datastructures::LinkedCellContainer<LinkedCell, dim>& cells,
      58              :                         coupling::interface::MDSolverInterface<LinkedCell, dim>* const mdSolverInterface) {
      59              :     // open files
      60            0 :     std::ofstream moleculeFile;
      61              :     // store positions and velocities of molecules
      62            0 :     std::stringstream moleculeVelocities;
      63            0 :     std::stringstream moleculePositions;
      64            0 :     std::stringstream moleculePotentials;
      65              :     // zeros appended in vtk file
      66            0 :     std::string appendFloatZeros("");
      67              :     switch (dim) {
      68              :     case 1:
      69              :       appendFloatZeros = " 0.0 0.0";
      70              :       break;
      71              :     case 2:
      72              :       appendFloatZeros = " 0.0";
      73              :       break;
      74              :     case 3:
      75            0 :       appendFloatZeros = "";
      76              :       break;
      77              :     default:
      78              :       break;
      79              :     }
      80            0 :     Writer4Molecules writer(moleculeVelocities, moleculePositions, moleculePotentials, appendFloatZeros, mdSolverInterface);
      81            0 :     cells.applyToLocalNonGhostCouplingCellsWithLinkedCells(writer);
      82              :     // open file and write info
      83            0 :     open(ID, filename, "Molecules", rank, t, moleculeFile);
      84            0 :     moleculeFile << "# vtk DataFile Version 2.0" << std::endl;
      85            0 :     moleculeFile << "Generated by MaMiCo (Philipp Neumann)" << std::endl;
      86            0 :     moleculeFile << "ASCII" << std::endl;
      87            0 :     moleculeFile << "DATASET UNSTRUCTURED_GRID" << std::endl;
      88            0 :     moleculeFile << "POINTS " << writer.getMoleculeCounter() << " float" << std::endl;
      89            0 :     moleculeFile << moleculePositions.str() << std::endl << std::endl;
      90            0 :     moleculeFile << "POINT_DATA " << writer.getMoleculeCounter() << std::endl;
      91            0 :     moleculeFile << "VECTORS velocity float" << std::endl;
      92            0 :     moleculeFile << moleculeVelocities.str() << std::endl;
      93            0 :     moleculeFile << "SCALARS potentialEnergy float 1" << std::endl;
      94            0 :     moleculeFile << "LOOKUP_TABLE DEFAULT" << std::endl;
      95            0 :     moleculeFile << moleculePotentials.str() << std::endl;
      96            0 :     moleculePositions.clear();
      97            0 :     moleculePositions.str("");
      98            0 :     moleculeVelocities.clear();
      99            0 :     moleculeVelocities.str("");
     100            0 :     moleculePotentials.clear();
     101            0 :     moleculePotentials.str("");
     102            0 :     moleculeFile.close();
     103            0 :   }
     104              : 
     105              :   /** @brief writes molecule information + coupling cell information to VTK
     106              :    * file.
     107              :    *  @param ID the id of the md simulation
     108              :    *  @param filename name for the file
     109              :    *  @param rank mpi rank of the current process
     110              :    *  @param t number of time step to plot
     111              :    *  @param cells coupling cells to plot
     112              :    *  @param mdSolverInterface interface of the md solver */
     113            0 :   void plotCouplingCellFile(unsigned int ID, std::string filename, unsigned int rank, unsigned int t,
     114              :                             coupling::datastructures::LinkedCellContainer<LinkedCell, dim>& cells,
     115              :                             coupling::interface::MDSolverInterface<LinkedCell, dim>* const mdSolverInterface) {
     116              :     // stringstreams for all cell properties of interest
     117            0 :     std::stringstream microscopicMasses;
     118            0 :     microscopicMasses << "SCALARS microscopicMassBuffer float 1" << std::endl;
     119            0 :     microscopicMasses << "LOOKUP_TABLE DEFAULT" << std::endl;
     120            0 :     std::stringstream macroscopicMasses;
     121            0 :     macroscopicMasses << "SCALARS macroscopicMassBuffer float 1" << std::endl;
     122            0 :     macroscopicMasses << "LOOKUP_TABLE DEFAULT" << std::endl;
     123            0 :     std::stringstream microscopicMomenta;
     124            0 :     microscopicMomenta << "VECTORS microscopicMomentumBuffer float" << std::endl;
     125            0 :     std::stringstream macroscopicMomenta;
     126            0 :     macroscopicMomenta << "VECTORS macroscopicMomentumBuffer float" << std::endl;
     127            0 :     std::stringstream meanVelocities;
     128            0 :     meanVelocities << "VECTORS meanVelocity float" << std::endl;
     129            0 :     std::stringstream masses;
     130            0 :     masses << "SCALARS mass float 1" << std::endl;
     131            0 :     masses << "LOOKUP_TABLE DEFAULT" << std::endl;
     132            0 :     std::stringstream energies;
     133            0 :     energies << "SCALARS meanPotentialEnergyBuffer float 1" << std::endl;
     134            0 :     energies << "LOOKUP_TABLE DEFAULT" << std::endl;
     135            0 :     std::stringstream temperatures;
     136            0 :     temperatures << "SCALARS temperature float 1" << std::endl;
     137            0 :     temperatures << "LOOKUP_TABLE DEFAULT" << std::endl;
     138              :     // ofstream for file
     139            0 :     std::ofstream ofCell;
     140              :     // zeros appended in vtk file
     141            0 :     std::string appendFloatZeros("");
     142              :     switch (dim) {
     143              :     case 1:
     144              :       appendFloatZeros = " 0.0 0.0";
     145              :       break;
     146              :     case 2:
     147              :       appendFloatZeros = " 0.0";
     148              :       break;
     149              :     case 3:
     150            0 :       appendFloatZeros = "";
     151              :       break;
     152              :     default:
     153              :       break;
     154              :     }
     155              : 
     156            0 :     tarch::la::Vector<3, double> original = I11{{0, 0, 0}}.getCellMidPoint() - 0.5 * IDXS.getCouplingCellSize();
     157              : 
     158              :     // open file
     159            0 :     open(ID, filename, "CouplingCells", rank, t, ofCell);
     160            0 :     Writer4Cells writer(microscopicMasses, macroscopicMasses, microscopicMomenta, macroscopicMomenta, meanVelocities, masses, energies, temperatures,
     161              :                         appendFloatZeros, mdSolverInterface);
     162            0 :     cells.applyToAllLocalCouplingCellsWithLinkedCells(writer);
     163            0 :     ofCell << "# vtk DataFile Version 2.0" << std::endl;
     164            0 :     ofCell << "generated by MaMiCo (Philipp Neumann)" << std::endl;
     165            0 :     ofCell << "ASCII" << std::endl << std::endl;
     166            0 :     ofCell << "DATASET STRUCTURED_POINTS" << std::endl;
     167            0 :     ofCell << "DIMENSIONS " << I03::numberCellsInDomain << std::endl;
     168            0 :     ofCell << "ORIGIN ";
     169            0 :     for (unsigned int d = 0; d < 3; d++) {
     170            0 :       ofCell << original[d] << " ";
     171              :     }
     172            0 :     ofCell << std::endl;
     173            0 :     ofCell << "SPACING ";
     174            0 :     for (unsigned int d = 0; d < dim; d++) {
     175            0 :       ofCell << IDXS.getCouplingCellSize()[d] << " ";
     176              :     }
     177            0 :     for (unsigned int d = dim; d < 3; d++) {
     178              :       ofCell << "1.0 ";
     179              :     }
     180            0 :     ofCell << std::endl;
     181            0 :     ofCell << "POINT_DATA " << I02::linearNumberCellsInDomain << std::endl << std::endl;
     182            0 :     ofCell << microscopicMasses.str() << std::endl << std::endl;
     183            0 :     microscopicMasses.clear();
     184            0 :     microscopicMasses.str("");
     185            0 :     ofCell << macroscopicMasses.str() << std::endl << std::endl;
     186            0 :     macroscopicMasses.clear();
     187            0 :     macroscopicMasses.str("");
     188            0 :     ofCell << masses.str() << std::endl << std::endl;
     189            0 :     masses.clear();
     190            0 :     masses.str("");
     191            0 :     ofCell << energies.str() << std::endl << std::endl;
     192            0 :     energies.clear();
     193            0 :     energies.str("");
     194            0 :     ofCell << temperatures.str() << std::endl << std::endl;
     195            0 :     temperatures.clear();
     196            0 :     temperatures.str("");
     197            0 :     ofCell << microscopicMomenta.str() << std::endl << std::endl;
     198            0 :     microscopicMomenta.clear();
     199            0 :     microscopicMomenta.str("");
     200            0 :     ofCell << macroscopicMomenta.str() << std::endl << std::endl;
     201            0 :     macroscopicMomenta.clear();
     202            0 :     macroscopicMomenta.str("");
     203            0 :     ofCell << meanVelocities.str() << std::endl << std::endl;
     204            0 :     meanVelocities.clear();
     205            0 :     meanVelocities.str("");
     206              :     // close file
     207            0 :     ofCell.close();
     208            0 :   }
     209              : 
     210              :   /** @brief opens a file
     211              :    *  @param ID the id of the md simulation
     212              :    *  @param filename name for the file
     213              :    *  @param fileType type of the file (e.g. vtk)
     214              :    *  @param rank mpi rank of the current process
     215              :    *  @param t number of time step to plot
     216              :    *  @param of this is the stream, where it will be written to */
     217            0 :   void open(unsigned int ID, std::string filename, std::string fileType, unsigned int rank, unsigned int t, std::ofstream& of) {
     218            0 :     std::stringstream ss;
     219            0 :     ss << filename << "_" << fileType << "_" << ID << "_" << rank << "_" << t << ".vtk";
     220            0 :     of.open(ss.str().c_str());
     221            0 :     if (!of.is_open()) {
     222            0 :       std::cout << "ERROR coupling::CouplingCellPlotter: Could not open file " << ss.str() << "!" << std::endl;
     223            0 :       exit(EXIT_FAILURE);
     224              :     }
     225            0 :   }
     226              : 
     227              :   /** @brief class for writing coupling cell data to stringstreams.
     228              :    *  @author Philipp Neumann*/
     229              :   class Writer4Cells {
     230              :   public:
     231              :     /** @brief a simple constructor
     232              :      *  @param microscopicMasses stream for the microscopic masses
     233              :      *  @param macroscopicMasses stream for the macroscopic masses
     234              :      *  @param microscopicMomenta stream for the microscopic momenta
     235              :      *  @param macroscopicMomenta stream for the macroscopic momenta
     236              :      *  @param meanVelocities stream for the mean velocities
     237              :      *  @param masses stream for the masses
     238              :      *  @param energies stream for the energies
     239              :      *  @param temperatures stream for the temperatures
     240              :      *  @param appendFloatZeros string containing '0' to add in the case of 1d
     241              :      * or 2d
     242              :      *  @param mdSolverInterface interface of the md solver */
     243            0 :     Writer4Cells(std::stringstream& microscopicMasses, std::stringstream& macroscopicMasses, std::stringstream& microscopicMomenta,
     244              :                  std::stringstream& macroscopicMomenta, std::stringstream& meanVelocities, std::stringstream& masses, std::stringstream& energies,
     245              :                  std::stringstream& temperatures, const std::string& appendFloatZeros,
     246              :                  coupling::interface::MDSolverInterface<LinkedCell, dim>* const mdSolverInterface)
     247            0 :         : _microscopicMasses(microscopicMasses), _macroscopicMasses(macroscopicMasses), _microscopicMomenta(microscopicMomenta),
     248            0 :           _macroscopicMomenta(macroscopicMomenta), _meanVelocities(meanVelocities), _masses(masses), _energies(energies), _temperatures(temperatures),
     249            0 :           _appendFloatZeros(appendFloatZeros), _computeMassMapping(mdSolverInterface), _computeMomentumMapping(mdSolverInterface),
     250            0 :           _kineticEnergyController(mdSolverInterface) {}
     251              : 
     252              :     /** @brief a simple destructor*/
     253            0 :     ~Writer4Cells() {}
     254              : 
     255              :     /** @brief does everything which is neccessary before the application of the
     256              :      * writer to the cells */
     257              :     void beginCellIteration() {}
     258              : 
     259              :     /** @brief does everything which is neccessary after the application of the
     260              :      * writer to the cells */
     261              :     void endCellIteration() {}
     262              : 
     263              :     /** @brief writes the data from the cell to the stringstreams
     264              :      *  @param cell coupling cell
     265              :      *  @param index linearised index of the coupling cell*/
     266            0 :     void apply(coupling::datastructures::CouplingCellWithLinkedCells<LinkedCell, dim>& cell, const I02& index) {
     267              :       // compute local quantities from molecules, if this is an inner cell
     268            0 :       double mass = 0.0;
     269            0 :       tarch::la::Vector<dim, double> meanVelocity(0.0);
     270            0 :       double temperature = 0.0;
     271              : 
     272            0 :       if (I10::contains(index)) {
     273            0 :         cell.iterateConstCells(_computeMassMapping);
     274            0 :         mass = _computeMassMapping.getMass();
     275            0 :         cell.iterateConstCells(_computeMomentumMapping);
     276            0 :         meanVelocity = _computeMomentumMapping.getMeanVelocity();
     277            0 :         temperature = _kineticEnergyController.computeTemperature(cell);
     278              :       }
     279              : 
     280              :       // write data to streams
     281            0 :       _microscopicMasses << cell.getMicroscopicMass() << std::endl;
     282            0 :       _macroscopicMasses << cell.getMacroscopicMass() << std::endl;
     283            0 :       _masses << mass << std::endl;
     284            0 :       _temperatures << temperature << std::endl;
     285            0 :       _energies << cell.getPotentialEnergy() << std::endl;
     286            0 :       for (unsigned int d = 0; d < dim; d++) {
     287            0 :         _microscopicMomenta << cell.getMicroscopicMomentum()[d] << " ";
     288            0 :         _macroscopicMomenta << cell.getMacroscopicMomentum()[d] << " ";
     289            0 :         _meanVelocities << meanVelocity[d] << " ";
     290              :       }
     291            0 :       _microscopicMomenta << _appendFloatZeros << std::endl;
     292            0 :       _macroscopicMomenta << _appendFloatZeros << std::endl;
     293            0 :       _meanVelocities << _appendFloatZeros << std::endl;
     294            0 :     }
     295              : 
     296              :   private:
     297              :     /** stream containing the microscopic mass of the cells (buffer to transfer
     298              :      * mass from macro to md)*/
     299              :     std::stringstream& _microscopicMasses;
     300              :     /** stream containing the macroscopic mass of the cells (buffer to transfer
     301              :      * mass from md to macro)*/
     302              :     std::stringstream& _macroscopicMasses;
     303              :     /** stream containing the microscopic momenta of the cells (buffer to
     304              :      * transfer momenta from macro to md)*/
     305              :     std::stringstream& _microscopicMomenta;
     306              :     /** stream containing the macroscopic momenta of the cells (buffer to
     307              :      * transfer momenta from md to macro )*/
     308              :     std::stringstream& _macroscopicMomenta;
     309              :     /** stream containing the mean velocity of the cells*/
     310              :     std::stringstream& _meanVelocities;
     311              :     /** stream containing the total mass in the cells*/
     312              :     std::stringstream& _masses;
     313              :     /** stream containing the total energy in the cells*/
     314              :     std::stringstream& _energies;
     315              :     /** stream containing the mean temperature in the cells*/
     316              :     std::stringstream& _temperatures;
     317              :     /** string containing '0' to add in the case of 1d or 2d */
     318              :     const std::string _appendFloatZeros;
     319              :     /** instance of the computeMassMapping*/
     320              :     coupling::cellmappings::ComputeMassMapping<LinkedCell, dim> _computeMassMapping;
     321              :     /** instance of the computeMomentumMapping*/
     322              :     coupling::cellmappings::ComputeMomentumMapping<LinkedCell, dim> _computeMomentumMapping;
     323              :     /** instance of the KineticEnergyController*/
     324              :     coupling::KineticEnergyController<LinkedCell, dim> _kineticEnergyController;
     325              :   };
     326              : 
     327              :   /** We make use of the traversal callback pattern of CouplingCells.
     328              :    *  @brief class for writing molecule data to stringstreams.
     329              :    *  @author Philipp Neumann */
     330            0 :   class Writer4Molecules {
     331              :   public:
     332              :     /** @brief a simple constructor
     333              :      *  @param moleculeVelocities stream for the molecule velocities
     334              :      *  @param moleculePositions stream for the molecule positions
     335              :      *  @param moleculePotentials stream for the molecule potentials
     336              :      *  @param appendFloatZeros string containing '0' to add in the case of 1d
     337              :      * or 2d
     338              :      *  @param mdSolverInterface interface of the md solver */
     339            0 :     Writer4Molecules(std::stringstream& moleculeVelocities, std::stringstream& moleculePositions, std::stringstream& moleculePotentials,
     340              :                      const std::string& appendFloatZeros, coupling::interface::MDSolverInterface<LinkedCell, dim>* const mdSolverInterface)
     341            0 :         : _moleculeVelocities(moleculeVelocities), _moleculePositions(moleculePositions), _moleculePotentials(moleculePotentials),
     342            0 :           _appendFloatZeros(appendFloatZeros), _mdSolverInterface(mdSolverInterface), _moleculeCounter(0) {}
     343              : 
     344              :     /** @brief does everything which is neccessary before the application of the
     345              :      * writer to the cells */
     346              :     void beginCellIteration() {}
     347              : 
     348              :     /** @brief does everything which is neccessary after the application of the
     349              :      * writer to the cells */
     350              :     void endCellIteration() {}
     351              : 
     352              :     /** @brief aplication of the vtkMoleculePlotter to the coupling cells*/
     353            0 :     void apply(coupling::datastructures::CouplingCellWithLinkedCells<LinkedCell, dim>& cell, const I02& index) {
     354            0 :       coupling::cellmappings::VTKMoleculePlotter<LinkedCell, dim> vtkMoleculePlotter(_moleculeVelocities, _moleculePositions, _moleculePotentials,
     355            0 :                                                                                      _appendFloatZeros, _mdSolverInterface);
     356            0 :       cell.iterateConstCells(vtkMoleculePlotter);
     357            0 :       _moleculeCounter += vtkMoleculePlotter.getParticleCounter();
     358            0 :     }
     359              : 
     360              :     /** @brief returns the number of molecules
     361              :      *  @returns the number of molecules plotted */
     362            0 :     unsigned int getMoleculeCounter() const { return _moleculeCounter; }
     363              : 
     364              :   private:
     365              :     /** stringstream for the velocities of the molecules*/
     366              :     std::stringstream& _moleculeVelocities;
     367              :     /** stringstream for the positions of the molecules*/
     368              :     std::stringstream& _moleculePositions;
     369              :     /** stringstream for the potentials of the molecules*/
     370              :     std::stringstream& _moleculePotentials;
     371              :     /** string containing '0' to add in the case of 1d or 2d */
     372              :     const std::string _appendFloatZeros;
     373              :     /** interface to the md solver*/
     374              :     coupling::interface::MDSolverInterface<LinkedCell, dim>* const _mdSolverInterface;
     375              :     /** counter for the molecules that have been plotted*/
     376              :     unsigned int _moleculeCounter;
     377              :   };
     378              : };
     379              : #endif // _MOLECULARDYNAMICS_COUPLING_COUPLINGCELLPLOTTER_H_
        

Generated by: LCOV version 2.0-1