MaMiCo 1.2
Loading...
Searching...
No Matches
Operations.h
1#pragma once
2
3#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
4#include <sstream>
5#endif
6
7namespace coupling {
8namespace indexing {
9
21template <unsigned int dim, IndexTrait... traits> unsigned int convertToScalar(const CellIndex<dim, traits...>& index) {
22 if constexpr (std::is_same_v<unsigned int, typename CellIndex<dim, traits...>::value_T>) {
23 return index.get();
24 } else {
25 // copied from deprecated coupling::IndexConversion::getCellIndex())
26
27#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
28 if (!IndexingService<dim>::getInstance().isInitialized()) {
29 throw std::runtime_error(std::string("coupling::indexing::convertToScalar: IndexingService not initialized! "));
30 }
31 for (unsigned d = 0; d < dim; d++)
32 if (index.get()[d] < 0 || index.get()[d] > ((int)CellIndex<dim, traits...>::numberCellsInDomain[d]) - 1) {
33 std::stringstream ss;
34 ss << "ERROR: Indexing: Cannot convert this vector index to scalar." << std::endl;
35 ss << "Faulty Index = " << index.get() << std::endl;
36 ss << "My rank = " << IndexingService<dim>::getInstance().getRank() << std::endl;
37 ss << "Index type = " << index.TNAME << std::endl;
38 throw std::runtime_error(ss.str());
39 }
40#endif
41
42 unsigned int i{static_cast<unsigned int>(index.get()[dim - 1])};
43 for (int d = dim - 2; d > -1; d--) {
44 i = (CellIndex<dim, traits...>::numberCellsInDomain[d]) * i + static_cast<unsigned int>(index.get()[d]);
45 }
46
47 return i;
48 }
49}
50
62template <unsigned int dim, IndexTrait... traits> tarch::la::Vector<dim, int> convertToVector(const CellIndex<dim, traits...>& index) {
63 if constexpr (std::is_same_v<tarch::la::Vector<dim, int>, typename CellIndex<dim, traits...>::value_T>) {
64 // trivial case: convert vector to vector
65 return index.get();
66 } else {
67 // copied from coupling::getVectorCellIndex()
68
69#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
70 if (!IndexingService<dim>::getInstance().isInitialized()) {
71 throw std::runtime_error(std::string("coupling::indexing::convertToVector: IndexingService not initialized! "));
72 }
73#endif
74
75 tarch::la::Vector<dim, int> i{0};
76 unsigned int i_sca{index.get()};
77 for (int d = dim - 1; d > 0; d--) {
78 i[d] = static_cast<int>(i_sca / CellIndex<dim, traits...>::divisionFactor[d]);
79 i_sca -= static_cast<unsigned int>(i[d]) * CellIndex<dim, traits...>::divisionFactor[d];
80 }
81 i[0] = i_sca;
82
83 return i;
84 }
85}
86
87} // namespace indexing
88} // namespace coupling
Definition CellIndex.h:85
static tarch::la::Vector< dim, unsigned int > divisionFactor
Definition CellIndex.h:276
static tarch::la::Vector< dim, unsigned int > numberCellsInDomain
Definition CellIndex.h:264
everything necessary for coupling operations, is defined in here
Definition AdditiveMomentumInsertion.h:15