5#ifndef _MOLECULARDYNAMICS_COUPLING_SERVICES_MULTIMDCELLSERVICE_H_
6#define _MOLECULARDYNAMICS_COUPLING_SERVICES_MULTIMDCELLSERVICE_H_
15#include "coupling/InstanceHandling.h"
16#include "coupling/configurations/MaMiCoConfiguration.h"
17#include "coupling/indexing/IndexingService.h"
18#include "coupling/interface/MDSimulationFactory.h"
19#include "coupling/services/CouplingCellService.h"
20#include "coupling/services/CouplingCellServiceDummy.h"
34 simplemd::configurations::MolecularDynamicsConfiguration& mdConfiguration,
38 _mamicoConfiguration(mamicoConfiguration), _filterPipelineConfiguration(filterPipelineConfiguration),
40 _topologyOffset = computeTopologyOffset();
41 _localNumberMDSimulations = multiMDService.getLocalNumberOfMDSimulations();
44 _blockOffset = _localNumberMDSimulations * _topologyOffset / _intNumberProcesses;
52 if (mdSolverInterfaces.size() == 0) {
53 std::cout <<
"ERROR: Zero MD instances on rank " <<
_multiMDService.getGlobalRank() <<
". Maybe you forgot to adjust number-md-simulations?" << std::endl;
68 "coupling::services::MultiMDCellService::MultiMDCellService("
69 "...): _couplingCellServices==NULL!"
81 for (
unsigned int i = 0; i < _blockOffset; i++) {
83 (i / _localNumberMDSimulations) * _intNumberProcesses);
86 "coupling::services::MultiMDCellService::MultiMDCellServic"
87 "e(...): _couplingCellServices["
88 << i <<
"]==NULL!" << std::endl;
93 for (
unsigned int i = _blockOffset; i < _localNumberMDSimulations + _blockOffset; i++) {
95 i, mdSolverInterfaces[i - _blockOffset], macroscopicSolverInterface, mdConfiguration.getMPIConfiguration().getNumberOfProcesses(),
100 _filterPipelineConfiguration.c_str(), multiMDService, _topologyOffset,
_tws);
102 std::cout <<
"ERROR "
103 "coupling::services::MultiMDCellService::MultiMDCellServic"
104 "e(...): _couplingCellServices["
105 << i <<
"]==NULL!" << std::endl;
113 (i / _localNumberMDSimulations) * _intNumberProcesses);
115 std::cout <<
"ERROR "
116 "coupling::services::MultiMDCellService::MultiMDCellServic"
117 "e(...): _couplingCellServices["
118 << i <<
"]==NULL!" << std::endl;
123 _mdConfiguration.getDomainConfigurationNonConst().setInitFromCheckpoint(
true);
124 std::stringstream filestem;
125 filestem <<
"restart_checkpoint_" << (
_multiMDService.getGlobalRank()) / computeScalarNumberProcesses() <<
"_0";
126 _mdConfiguration.getDomainConfigurationNonConst().setCheckpointFilestem(filestem.str());
127 _mdConfiguration.getDomainConfigurationNonConst().setInitFromSequentialCheckpoint(
false);
130 ~MultiMDCellService() {
145 for (
auto pair : _couplingCells) {
146 std::tie(cell, idx) = pair;
158 if (localIndex < _localNumberMDSimulations) {
159 return *(
_couplingCellServices[_topologyOffset * _localNumberMDSimulations / _intNumberProcesses + localIndex]);
161 std::cout <<
"ERROR MultiMDCellService::getCouplingCellService(localIndex): "
162 "localIndex >_localNumberMDSimulations-1!"
170 if (
_couplingCellServices[_topologyOffset * _localNumberMDSimulations / _intNumberProcesses + localIndex] ==
nullptr)
172 _couplingCellServices[_topologyOffset * _localNumberMDSimulations / _intNumberProcesses + localIndex]->plotEveryMacroscopicTimestep(cycle);
175 void plotEveryMacroscopicTimestep(
int cycle) {
176 for (
unsigned int i = _blockOffset; i < _blockOffset + _localNumberMDSimulations; ++i) {
183 void computeAndStoreTemperature(
double temp) {
184 for (
unsigned int i = _blockOffset; i < _blockOffset + _localNumberMDSimulations; ++i) {
204#if (COUPLING_MD_PARALLEL == COUPLING_MD_NO)
211 std::vector<std::vector<coupling::datastructures::CouplingCell<dim>*>> allCouplingCellsFromMamico(
_totalNumberMDSimulations);
217 allCouplingCellsFromMamico[i] = v->getCouplingCells().getCouplingCells();
228 fromMacro2MD.bcastFromMacro2MD(allDEs, couplingCellsFromMacroscopicSolver, indices, allCouplingCellsFromMamico);
258 for (
unsigned int i = 0; i < v->getCouplingCells().getCouplingCells().size(); ++i) {
262 _sumCouplingCells[i]->addMacroscopicMass(v->getCouplingCells().getCouplingCells()[i]->getMacroscopicMass());
263 _sumCouplingCells[i]->addMacroscopicMomentum(v->getCouplingCells().getCouplingCells()[i]->getMacroscopicMomentum());
276#if (COUPLING_MD_PARALLEL == COUPLING_MD_NO)
281 const unsigned int size = (
unsigned int)couplingCellsFromMacroscopicSolver.size();
299 couplingCell->setMacroscopicMass(0.0);
304 std::vector<std::vector<coupling::datastructures::CouplingCell<dim>*>> allCouplingCellsFromMamico(
_totalNumberMDSimulations);
307 unsigned int totalNumberEquilibratedMDSimulations = 0;
312 totalNumberEquilibratedMDSimulations += 1;
314 _fromMD2Macro.reduceFromMD2Macro(allDEs, couplingCellsFromMacroscopicSolver, indices,
_sumCouplingCells);
317 for (
unsigned int i = 0; i < size; i++) {
318 _couplingCells[i]->setMacroscopicMass(couplingCellsFromMacroscopicSolver[i]->getMacroscopicMass() / (
double)totalNumberEquilibratedMDSimulations);
319 _couplingCells[i]->setMacroscopicMomentum(1.0 / (
double)totalNumberEquilibratedMDSimulations *
320 couplingCellsFromMacroscopicSolver[i]->getMacroscopicMomentum());
331#ifdef DEBUG_FILTER_PIPELINE
332 std::cout <<
"FP: Now applying post-multi-instance filter pipeline" << std::endl;
338 for (
unsigned int i = 0; i < size; i++) {
339 couplingCellsFromMacroscopicSolver[i]->setMacroscopicMass(_couplingCells[i]->getMacroscopicMass());
340 couplingCellsFromMacroscopicSolver[i]->setMacroscopicMomentum(_couplingCells[i]->getMacroscopicMomentum());
354#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
355 if (_couplingCells.size() != md2macroCouplingCells.
size())
356 throw std::runtime_error(std::string(
"Buffers must have the same size!"));
364 for (
auto pair : _couplingCells) {
365 std::tie(cellA, idx) = pair;
372 unsigned int totalNumberEquilibratedMDSimulations = 0;
379 auto itCouplingCells = _couplingCells.begin();
380 auto itMd2macroCouplingCells = md2macroCouplingCells.
begin();
381 while (itCouplingCells != _couplingCells.end()) {
382 std::tie(cellA, idx) = *itCouplingCells;
383 std::tie(cellB, idx) = *itMd2macroCouplingCells;
387 itMd2macroCouplingCells++;
389 totalNumberEquilibratedMDSimulations += 1;
394 for (
auto pair : _couplingCells) {
395 std::tie(cellA, idx) = pair;
402#ifdef DEBUG_FILTER_PIPELINE
403 std::cout <<
"FP: Now applying post-multi-instance filter pipeline" << std::endl;
409 auto itCouplingCells = _couplingCells.begin();
410 auto itMd2macroCouplingCells = md2macroCouplingCells.
begin();
411 while (itCouplingCells != _couplingCells.end()) {
412 std::tie(cellA, idx) = *itCouplingCells;
413 std::tie(cellB, idx) = *itMd2macroCouplingCells;
417 itMd2macroCouplingCells++;
432 if (_couplingCells.size() == 0) {
436 for (
auto pair : cells) {
437 std::tie(cell, idx) = pair;
439 _couplingCells << std::make_pair(newCell, idx);
455#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
457 std::cout <<
"Rank " <<
_multiMDService.getGlobalRank() <<
": _couplingCellService at " << index <<
" == NULL" << std::endl;
464 if (index >= _blockOffset && index < _blockOffset + _localNumberMDSimulations) {
465 unsigned int iSim = index - _blockOffset;
466#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
468 std::cout <<
"MultiMDCellService: removing instance " << iSim <<
"..." << std::endl;
483 unsigned int newLocalNumberMDSimulations = _localNumberMDSimulations - 1;
485 unsigned int newBlockOffset = newLocalNumberMDSimulations * _topologyOffset / _intNumberProcesses;
489 for (
unsigned int i = 0; i <
_multiMDService.getNumberLocalComms(); ++i) {
490 for (
unsigned int j = 0; j < newLocalNumberMDSimulations; ++j) {
491 unsigned int index = i * _localNumberMDSimulations + j;
492 unsigned int newIndex = i * newLocalNumberMDSimulations + j;
497 auto pos =
_warmupPhase.begin() + newLocalNumberMDSimulations * i - i;
502 _localNumberMDSimulations = newLocalNumberMDSimulations;
504 _blockOffset = newBlockOffset;
523 unsigned int newLocalNumberMDSimulations = _localNumberMDSimulations + 1;
525 unsigned int newBlockOffset = newLocalNumberMDSimulations * _topologyOffset / _intNumberProcesses;
528 for (
unsigned int i = 0; i <
_multiMDService.getNumberLocalComms(); ++i) {
529 for (
unsigned int j = 0; j < _localNumberMDSimulations; ++j) {
530 unsigned int index = i * _localNumberMDSimulations + j;
531 unsigned int newIndex = i * newLocalNumberMDSimulations + j;
534 newCouplingCellServices[(i + 1) * newLocalNumberMDSimulations - 1] =
nullptr;
536 auto pos =
_warmupPhase.begin() + ((i + 1) * newLocalNumberMDSimulations - 1);
541 _localNumberMDSimulations = newLocalNumberMDSimulations;
543 _blockOffset = newBlockOffset;
556 std::cout <<
"ERROR coupling::services::MultiMDCellService::addMDSimulation(): "
558 << slot <<
"!" << std::endl;
559 std::exit(EXIT_FAILURE);
562 std::cout <<
"ERROR! "
563 "coupling::services::MultiMDCellService::addMDSimulation(): "
565 << slot <<
" already exists!" << std::endl;
566 std::exit(EXIT_FAILURE);
569 std::stringstream filestem;
570 filestem <<
"restart_checkpoint_" <<
_multiMDService.getGlobalRank() / computeScalarNumberProcesses();
573 if (slot < _blockOffset || slot >= _blockOffset + _localNumberMDSimulations) {
578 _mamicoConfiguration.getParallelTopologyConfiguration(), _mamicoConfiguration.getCouplingCellConfiguration(),
579 (slot / _localNumberMDSimulations) * _intNumberProcesses);
582 unsigned localIndex = slot - _blockOffset;
583 auto* mdSolverInterface = instanceHandling.
addMDSimulation(slot, localIndex);
587 _mamicoConfiguration.getParticleInsertionConfiguration(), _mamicoConfiguration.getMomentumInsertionConfiguration(),
588 _mamicoConfiguration.getBoundaryForceConfiguration(), _mamicoConfiguration.getTransferStrategyConfiguration(),
589 _mamicoConfiguration.getParallelTopologyConfiguration(), _mamicoConfiguration.getThermostatConfiguration(),
590 _mdConfiguration.getSimulationConfiguration().getNumberOfTimesteps(), _mamicoConfiguration.getCouplingCellConfiguration(),
604 unsigned int getLocalNumberOfMDSimulations()
const {
return _localNumberMDSimulations; }
611 if (
_warmupPhase[i] == 0 && i >= _blockOffset && i < _blockOffset + _localNumberMDSimulations) {
620 void writeCheckpoint(
const unsigned int& cycle,
const coupling::InstanceHandling<LinkedCell, dim>& instanceHandling)
const {
623 std::stringstream filestem;
624 filestem <<
"restart_checkpoint_" <<
_multiMDService.getGlobalRank() / computeScalarNumberProcesses();
630 void constructFilterPipelines() {
636 if (
dynamic_cast<coupling::services::CouplingCellServiceImpl<LinkedCell, dim>*
>(mcs) ==
nullptr)
639 if (mcs->getFilterPipeline() ==
nullptr) {
640 mcs->initFiltering();
646 unsigned int computeTopologyOffset()
const {
648 const unsigned int intNumberProcesses = computeScalarNumberProcesses();
649 const unsigned int topologyOffset = (
_multiMDService.getGlobalRank() / intNumberProcesses) * intNumberProcesses;
650 return topologyOffset;
653 unsigned int computeScalarNumberProcesses()
const {
654 unsigned int np =
_multiMDService.getNumberProcessesPerMDSimulation()[0];
655 for (
unsigned int d = 1; d < dim; d++) {
661 coupling::services::CouplingCellService<dim>*
662 createCouplingCellServiceDummy(
unsigned int ID, coupling::interface::MacroscopicSolverInterface<dim>* macroscopicSolverInterface,
663 simplemd::configurations::MolecularDynamicsConfiguration& mdConfiguration, tarch::utils::MultiMDService<dim>& multiMDService,
664 coupling::configurations::MaMiCoConfiguration<dim>& mamicoConfiguration,
unsigned int topologyOffset)
const {
665 return new coupling::services::CouplingCellServiceDummy<dim>(
666 ID, macroscopicSolverInterface, mdConfiguration.getMPIConfiguration().getNumberOfProcesses(), multiMDService.getGlobalRank(),
667 mdConfiguration.getDomainConfiguration().getGlobalDomainSize(), mdConfiguration.getDomainConfiguration().getGlobalDomainOffset(),
671 unsigned int _localNumberMDSimulations;
678 unsigned int _topologyOffset;
681 const unsigned int _intNumberProcesses;
689 const std::string _filterPipelineConfiguration;
692 unsigned int _blockOffset;
693 std::vector<bool> _listActiveMDSimulations;
Simulation slots are managed (i.e., added/removed) via this class. Works and interacts with the class...
Definition InstanceHandling.h:35
auto & getSimpleMD() const
Definition InstanceHandling.h:101
void writeCheckpoint(const std::string &filestem, const unsigned int &T) const
Definition InstanceHandling.h:170
void rmMDSimulation(const unsigned int &index)
Definition InstanceHandling.h:253
void switchOnCoupling()
Definition InstanceHandling.h:127
coupling::interface::MDSolverInterface< LinkedCell, dim > * addMDSimulation(unsigned int slot, unsigned int localIndex)
Definition InstanceHandling.h:225
parses all sub-tags for MaMiCo configuration.
Definition MaMiCoConfiguration.h:31
const coupling::configurations::MomentumInsertionConfiguration & getMomentumInsertionConfiguration() const
Definition MaMiCoConfiguration.h:86
const coupling::configurations::ThermostatConfiguration & getThermostatConfiguration() const
Definition MaMiCoConfiguration.h:146
const coupling::configurations::BoundaryForceConfiguration< dim > & getBoundaryForceConfiguration() const
Definition MaMiCoConfiguration.h:99
const coupling::configurations::ParticleInsertionConfiguration & getParticleInsertionConfiguration() const
Definition MaMiCoConfiguration.h:73
const coupling::configurations::ParallelTopologyConfiguration & getParallelTopologyConfiguration() const
Definition MaMiCoConfiguration.h:125
const coupling::configurations::TransferStrategyConfiguration< dim > & getTransferStrategyConfiguration() const
Definition MaMiCoConfiguration.h:112
const coupling::configurations::CouplingCellConfiguration< dim > & getCouplingCellConfiguration() const
Definition MaMiCoConfiguration.h:68
defines the cell type with cell-averaged quantities only (no linked cells).
Definition CouplingCell.h:29
void addMacroscopicMomentum(const tarch::la::Vector< dim, double > &momentum)
Definition CouplingCell.h:85
const tarch::la::Vector< dim, double > & getMacroscopicMomentum() const
Definition CouplingCell.h:64
void addMacroscopicMass(const double &mass)
Definition CouplingCell.h:82
void setMacroscopicMomentum(const tarch::la::Vector< dim, double > &momentum)
Definition CouplingCell.h:61
const double & getMacroscopicMass() const
Definition CouplingCell.h:58
void setMacroscopicMass(const double &mass)
Definition CouplingCell.h:55
provides access to coupling cells, which may belong to different indexing domains
Definition FlexibleCellContainer.h:30
Iterator begin() const
Definition FlexibleCellContainer.h:121
unsigned int size() const
Definition FlexibleCellContainer.h:78
Definition FilterPipeline.h:44
interface to the MD simulation
Definition MDSolverInterface.h:25
interface for the macroscopic, i.e. continuum solver
Definition MacroscopicSolverInterface.h:23
static MamicoInterfaceProvider & getInstance()
Definition MamicoInterfaceProvider.h:28
coupling::interface::MacroscopicSolverInterface< dim > * getMacroscopicSolverInterface()
Definition MamicoInterfaceProvider.h:43
data exchange from the MD solver to the macroscopic solver. Derived from the class coupling::sendrecv...
Definition DataExchangeFromMD2Macro.h:36
data exchange from the macroscopic solver to the MD solver. Derived from the class coupling::sendrecv...
Definition DataExchangeFromMacro2MD.h:36
sends coupling cell information from MaMiCo to the macroscopic solver. Derived from the class couplin...
Definition FromMD2Macro.h:29
SendReceiveBuffer for transfer of quantities from a macroscopic solver to the coupling cells....
Definition FromMacro2MD.h:28
Definition CouplingCellServiceDummy.h:28
Definition CouplingCellService.h:98
Definition CouplingCellService.h:49
Definition MultiMDCellService.h:29
const int _tws
Definition MultiMDCellService.h:680
coupling::filtering::FilterPipeline< I14, dim > * _postMultiInstanceFilterPipeline
Definition MultiMDCellService.h:707
void sendFromMacro2MD(const coupling::datastructures::FlexibleCellContainer< dim > ¯o2mdCouplingCellContainer)
Definition MultiMDCellService.h:192
void removeSimulationBlock()
Definition MultiMDCellService.h:481
void bcastFromMacro2MD(const std::vector< coupling::datastructures::CouplingCell< dim > * > &couplingCellsFromMacroscopicSolver, const I00 *const indices)
Definition MultiMDCellService.h:203
void sumUpCouplingCellsFromMamico()
Definition MultiMDCellService.h:244
double reduceFromMD2Macro(const std::vector< coupling::datastructures::CouplingCell< dim > * > &couplingCellsFromMacroscopicSolver, const I00 *const indices)
Definition MultiMDCellService.h:275
std::vector< int > _warmupPhase
Definition MultiMDCellService.h:697
double sendFromMD2Macro(const coupling::datastructures::FlexibleCellContainer< dim > &md2macroCouplingCells)
Definition MultiMDCellService.h:349
simplemd::configurations::MolecularDynamicsConfiguration & _mdConfiguration
Definition MultiMDCellService.h:687
coupling::services::CouplingCellService< dim > & getCouplingCellService(unsigned int localIndex)
Definition MultiMDCellService.h:157
coupling::services::CouplingCellService< dim > ** _couplingCellServices
Definition MultiMDCellService.h:675
unsigned int rmMDSimulation(coupling::InstanceHandling< LinkedCell, dim > &instanceHandling, const unsigned int &index)
Definition MultiMDCellService.h:454
unsigned int addMDSimulation(coupling::InstanceHandling< LinkedCell, dim > &instanceHandling, coupling::interface::MacroscopicSolverInterface< dim > *macroscopicSolverInterface, const unsigned int &slot)
Definition MultiMDCellService.h:552
unsigned int _totalNumberMDSimulations
Definition MultiMDCellService.h:674
void addSimulationBlock()
Definition MultiMDCellService.h:521
void plotEveryMacroscopicTimestepforCouplingCellService(unsigned int localIndex, int cycle)
Definition MultiMDCellService.h:169
unsigned int _nextFreeBlock
Definition MultiMDCellService.h:695
void preprocessingForMD2Macro(const coupling::datastructures::FlexibleCellContainer< dim > &cells)
Definition MultiMDCellService.h:426
tarch::utils::MultiMDService< dim > & _multiMDService
Definition MultiMDCellService.h:677
coupling::datastructures::FlexibleCellContainer< dim > _sumCouplingCells
Definition MultiMDCellService.h:685
Definition MultiMDService.h:30
everything necessary for coupling operations, is defined in here
Definition AdditiveMomentumInsertion.h:15