Line data Source code
1 : #pragma once 2 : 3 : #if (COUPLING_MD_ERROR == COUPLING_MD_YES) 4 : #include <sstream> 5 : #endif 6 : 7 : namespace coupling { 8 : namespace indexing { 9 : 10 : /** 11 : * Conversion function to convert from vector to scalar representation of 12 : * CellIndex spcialisations. 13 : * 14 : * @tparam dim number of dimensions of the coupled simulation. 15 : * @tparam idx_T index type parametrisation used by index. 16 : * @param index index that shall be converted 17 : * @return int representation of index 18 : * 19 : * @author Felix Maurer and Piet Jarmatz 20 : */ 21 1320552 : template <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 800 : return index.get(); 24 : } else { 25 : // copied from deprecated coupling::IndexConversion::getCellIndex()) 26 : 27 : #if (COUPLING_MD_ERROR == COUPLING_MD_YES) 28 1319752 : if (!IndexingService<dim>::getInstance().isInitialized()) { 29 8 : throw std::runtime_error(std::string("coupling::indexing::convertToScalar: IndexingService not initialized! ")); 30 : } 31 5278800 : for (unsigned d = 0; d < dim; d++) 32 7918200 : if (index.get()[d] < 0 || index.get()[d] > ((int)CellIndex<dim, traits...>::numberCellsInDomain[d]) - 1) { 33 64 : std::stringstream ss; 34 64 : ss << "ERROR: Indexing: Cannot convert this vector index to scalar." << std::endl; 35 128 : ss << "Faulty Index = " << index.get() << std::endl; 36 64 : ss << "My rank = " << IndexingService<dim>::getInstance().getRank() << std::endl; 37 64 : ss << "Index type = " << index.TNAME << std::endl; 38 128 : throw std::runtime_error(ss.str()); 39 64 : } 40 : #endif 41 : 42 1319684 : unsigned int i{static_cast<unsigned int>(index.get()[dim - 1])}; 43 3959052 : for (int d = dim - 2; d > -1; d--) { 44 5278736 : i = (CellIndex<dim, traits...>::numberCellsInDomain[d]) * i + static_cast<unsigned int>(index.get()[d]); 45 : } 46 : 47 1319684 : return i; 48 : } 49 : } 50 : 51 : /** 52 : * Conversion function to convert from scalar to vector representation of 53 : * CellIndex spcialisations. 54 : * 55 : * @tparam dim number of dimensions of the coupled simulation. 56 : * @tparam idx_T index type parametrisation used by index. 57 : * @param index index that shall be converted 58 : * @returns vector representation of index 59 : * 60 : * @author Felix Maurer 61 : */ 62 5439336 : template <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 2082052 : return index.get(); 66 : } else { 67 : // copied from coupling::getVectorCellIndex() 68 : 69 : #if (COUPLING_MD_ERROR == COUPLING_MD_YES) 70 3357284 : if (!IndexingService<dim>::getInstance().isInitialized()) { 71 8 : throw std::runtime_error(std::string("coupling::indexing::convertToVector: IndexingService not initialized! ")); 72 : } 73 : #endif 74 : 75 6714560 : tarch::la::Vector<dim, int> i{0}; 76 3357280 : unsigned int i_sca{index.get()}; 77 10071840 : for (int d = dim - 1; d > 0; d--) { 78 6714560 : i[d] = static_cast<int>(i_sca / CellIndex<dim, traits...>::divisionFactor[d]); 79 6714560 : i_sca -= static_cast<unsigned int>(i[d]) * CellIndex<dim, traits...>::divisionFactor[d]; 80 : } 81 3357280 : i[0] = i_sca; 82 : 83 3357280 : return i; 84 : } 85 : } 86 : 87 : } // namespace indexing 88 : } // namespace coupling