Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
TempestLinearRemap.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file TempestLinearRemap.cpp
4 /// \author Vijay Mahadevan
5 /// \version Mar 08, 2017
6 ///
7 
8 #ifdef WIN32 /* windows */
9 #define _USE_MATH_DEFINES // For M_PI
10 #endif
11 
12 #pragma GCC diagnostic push
13 #pragma GCC diagnostic ignored "-Wdeprecated-copy-with-user-provided-copy"
14 #pragma GCC diagnostic ignored "-Wsign-compare"
15 
16 #include "Announce.h"
17 #include "DataArray3D.h"
18 #include "FiniteElementTools.h"
19 #include "FiniteVolumeTools.h"
20 #include "GaussLobattoQuadrature.h"
21 #include "TriangularQuadrature.h"
22 #include "MathHelper.h"
23 #include "SparseMatrix.h"
24 #include "OverlapMesh.h"
25 
26 #include "DebugOutput.hpp"
27 #include "moab/AdaptiveKDTree.hpp"
28 
30 #include "moab/TupleList.hpp"
31 #include "moab/MeshTopoUtil.hpp"
32 
33 #pragma GCC diagnostic pop
34 
35 #include <fstream>
36 #include <cmath>
37 #include <cstdlib>
38 #include <sstream>
39 #include <numeric>
40 #include <algorithm>
41 #include <unordered_set>
42 
43 // #define VERBOSE
44 #define USE_ComputeAdjacencyRelations
45 
46 /// <summary>
47 /// Face index and distance metric pair.
48 /// </summary>
49 typedef std::pair< int, int > FaceDistancePair;
50 
51 /// <summary>
52 /// Vector storing adjacent Faces.
53 /// </summary>
54 typedef std::vector< FaceDistancePair > AdjacentFaceVector;
55 
56 ///////////////////////////////////////////////////////////////////////////////
57 
58 moab::ErrorCode moab::TempestOnlineMap::LinearRemapNN_MOAB( bool use_GID_matching, bool strict_check )
59 {
60  /* m_mapRemap size = (m_nTotDofs_Dest X m_nTotDofs_SrcCov) */
61 
62 #ifdef VVERBOSE
63  {
64  std::ofstream output_file( "rowcolindices.txt", std::ios::out );
65  output_file << m_nTotDofs_Dest << " " << m_nTotDofs_SrcCov << " " << row_gdofmap.size() << " "
66  << row_ldofmap.size() << " " << col_gdofmap.size() << " " << col_ldofmap.size() << "\n";
67  output_file << "Rows \n";
68  for( unsigned iv = 0; iv < row_gdofmap.size(); iv++ )
69  output_file << row_gdofmap[iv] << " " << row_dofmap[iv] << "\n";
70  output_file << "Cols \n";
71  for( unsigned iv = 0; iv < col_gdofmap.size(); iv++ )
72  output_file << col_gdofmap[iv] << " " << col_dofmap[iv] << "\n";
73  output_file.flush(); // required here
74  output_file.close();
75  }
76 #endif
77 
78  if( use_GID_matching )
79  {
80  std::map< unsigned, unsigned > src_gl;
81  for( unsigned it = 0; it < col_gdofmap.size(); ++it )
82  src_gl[col_gdofmap[it]] = it;
83 
84  std::map< unsigned, unsigned >::iterator iter;
85  for( unsigned it = 0; it < row_gdofmap.size(); ++it )
86  {
87  unsigned row = row_gdofmap[it];
88  iter = src_gl.find( row );
89  if( strict_check && iter == src_gl.end() )
90  {
91  std::cout << "Searching for global target DOF " << row
92  << " but could not find correspondence in source mesh.\n";
93  assert( false );
94  }
95  else if( iter == src_gl.end() )
96  {
97  continue;
98  }
99  else
100  {
101  unsigned icol = src_gl[row];
102  unsigned irow = it;
103 
104  // Set the permutation matrix in local space
105  m_mapRemap( irow, icol ) = 1.0;
106  }
107  }
108 
109  return moab::MB_SUCCESS;
110  }
111  else
112  {
113  /* Create a Kd-tree to perform local queries to find nearest neighbors */
114 
115  return moab::MB_FAILURE;
116  }
117 }
118 
119 ///////////////////////////////////////////////////////////////////////////////
120 
122 {
123  // Order of triangular quadrature rule
124  const int TriQuadRuleOrder = 4;
125 
126  // Verify ReverseNodeArray has been calculated
127  if( m_meshInputCov->faces.size() > 0 && m_meshInputCov->revnodearray.size() == 0 )
128  {
129  _EXCEPTIONT( "ReverseNodeArray has not been calculated for m_meshInputCov" );
130  }
131 
132  // Triangular quadrature rule
133  TriangularQuadratureRule triquadrule( TriQuadRuleOrder );
134 
135  // Number of coefficients needed at this order
136 #ifdef RECTANGULAR_TRUNCATION
137  int nCoefficients = nOrder * nOrder;
138 #endif
139 #ifdef TRIANGULAR_TRUNCATION
140  int nCoefficients = nOrder * ( nOrder + 1 ) / 2;
141 #endif
142 
143  // Number of faces you need
144  const int nRequiredFaceSetSize = nCoefficients;
145 
146  // Fit weight exponent
147  const int nFitWeightsExponent = nOrder + 2;
148 
149  // Announcements
150  moab::DebugOutput dbgprint( std::cout, this->rank, 0 );
151  dbgprint.set_prefix( "[LinearRemapFVtoFV_Tempest_MOAB]: " );
152  if( is_root )
153  {
154  dbgprint.printf( 0, "Finite Volume to Finite Volume Projection\n" );
155  dbgprint.printf( 0, "Triangular quadrature rule order %i\n", TriQuadRuleOrder );
156  dbgprint.printf( 0, "Number of coefficients: %i\n", nCoefficients );
157  dbgprint.printf( 0, "Required adjacency set size: %i\n", nRequiredFaceSetSize );
158  dbgprint.printf( 0, "Fit weights exponent: %i\n", nFitWeightsExponent );
159  }
160 
161  // Current overlap face
162  int ixOverlap = 0;
163 #ifdef VERBOSE
164  const unsigned outputFrequency = ( m_meshInputCov->faces.size() / 10 ) + 1;
165 #endif
166  DataArray2D< double > dIntArray;
167  DataArray1D< double > dConstraint( nCoefficients );
168 
169  // Loop through all faces on m_meshInputCov
170  for( size_t ixFirst = 0; ixFirst < m_meshInputCov->faces.size(); ixFirst++ )
171  {
172  // Output every 1000 elements
173 #ifdef VERBOSE
174  if( ixFirst % outputFrequency == 0 && is_root )
175  {
176  dbgprint.printf( 0, "Element %zu/%lu\n", ixFirst, m_meshInputCov->faces.size() );
177  }
178 #endif
179  // Find the set of Faces that overlap faceFirst
180  int ixOverlapBegin = ixOverlap;
181  unsigned ixOverlapEnd = ixOverlapBegin;
182 
183  for( ; ixOverlapEnd < m_meshOverlap->faces.size(); ixOverlapEnd++ )
184  {
185  if( ixFirst - m_meshOverlap->vecSourceFaceIx[ixOverlapEnd] != 0 ) break;
186  }
187 
188  unsigned nOverlapFaces = ixOverlapEnd - ixOverlapBegin;
189 
190  if( nOverlapFaces == 0 ) continue;
191 
192  // Build integration array
193  BuildIntegrationArray( *m_meshInputCov, *m_meshOverlap, triquadrule, ixFirst, ixOverlapBegin, ixOverlapEnd,
194  nOrder, dIntArray );
195 
196  // Set of Faces to use in building the reconstruction and associated
197  // distance metric.
198  AdjacentFaceVector vecAdjFaces;
199 
200  GetAdjacentFaceVectorByEdge( *m_meshInputCov, ixFirst, nRequiredFaceSetSize, vecAdjFaces );
201 
202  // Number of adjacent Faces
203  int nAdjFaces = vecAdjFaces.size();
204 
205  // Determine the conservative constraint equation
206  double dFirstArea = m_meshInputCov->vecFaceArea[ixFirst];
207  dConstraint.Zero();
208  for( int p = 0; p < nCoefficients; p++ )
209  {
210  for( unsigned j = 0; j < nOverlapFaces; j++ )
211  {
212  dConstraint[p] += dIntArray[p][j];
213  }
214  dConstraint[p] /= dFirstArea;
215  }
216 
217  // Build the fit array from the integration operator
218  DataArray2D< double > dFitArray;
219  DataArray1D< double > dFitWeights;
220  DataArray2D< double > dFitArrayPlus;
221 
222  BuildFitArray( *m_meshInputCov, triquadrule, ixFirst, vecAdjFaces, nOrder, nFitWeightsExponent, dConstraint,
223  dFitArray, dFitWeights );
224 
225  // Compute the inverse fit array
226  bool fSuccess = InvertFitArray_Corrected( dConstraint, dFitArray, dFitWeights, dFitArrayPlus );
227 
228  // Multiply integration array and fit array
229  DataArray2D< double > dComposedArray( nAdjFaces, nOverlapFaces );
230  if( fSuccess )
231  {
232  // Multiply integration array and inverse fit array
233  for( int i = 0; i < nAdjFaces; i++ )
234  {
235  for( size_t j = 0; j < nOverlapFaces; j++ )
236  {
237  for( int k = 0; k < nCoefficients; k++ )
238  {
239  dComposedArray( i, j ) += dIntArray( k, j ) * dFitArrayPlus( i, k );
240  }
241  }
242  }
243 
244  // Unable to invert fit array, drop to 1st order. In this case
245  // dFitArrayPlus(0,0) = 1 and all other entries are zero.
246  }
247  else
248  {
249  dComposedArray.Zero();
250  for( size_t j = 0; j < nOverlapFaces; j++ )
251  {
252  dComposedArray( 0, j ) += dIntArray( 0, j );
253  }
254  }
255 
256  // Put composed array into map
257  for( unsigned i = 0; i < vecAdjFaces.size(); i++ )
258  {
259  for( unsigned j = 0; j < nOverlapFaces; j++ )
260  {
261  int& ixFirstFaceLoc = vecAdjFaces[i].first;
262  int& ixSecondFaceLoc = m_meshOverlap->vecTargetFaceIx[ixOverlap + j];
263  // int ixFirstFaceGlob = m_remapper->GetGlobalID(moab::Remapper::SourceMesh,
264  // ixFirstFaceLoc); int ixSecondFaceGlob =
265  // m_remapper->GetGlobalID(moab::Remapper::TargetMesh, ixSecondFaceLoc);
266 
267  // signal to not participate, because it is a ghost target
268  if( ixSecondFaceLoc < 0 ) continue; // do not do anything
269 
270  m_mapRemap( ixSecondFaceLoc, ixFirstFaceLoc ) +=
271  dComposedArray[i][j] / m_meshOutput->vecFaceArea[ixSecondFaceLoc];
272  }
273  }
274 
275  // Increment the current overlap index
276  ixOverlap += nOverlapFaces;
277  }
278 
279  return;
280 }
281 
282 ///////////////////////////////////////////////////////////////////////////////
283 
285 {
286  int nrows = m_weightMatrix.rows(); // Number of rows
287  int ncols = m_weightMatrix.cols(); // Number of columns
288  int NNZ = m_weightMatrix.nonZeros(); // Number of non zero values
289 #ifdef MOAB_HAVE_MPI
290  // find out min/max for NNZ, ncols, nrows
291  // should work on std c++ 11
292  int arr3[6] = { NNZ, nrows, ncols, -NNZ, -nrows, -ncols };
293  int rarr3[6];
294  MPI_Reduce( arr3, rarr3, 6, MPI_INT, MPI_MIN, 0, m_pcomm->comm() );
295 
296  int total[3];
297  MPI_Reduce( arr3, total, 3, MPI_INT, MPI_SUM, 0, m_pcomm->comm() );
298  if( !rank )
299  std::cout << "-> Rows (min/max/sum): (" << rarr3[1] << " / " << -rarr3[4] << " / " << total[1] << "), "
300  << " Cols (min/max/sum): (" << rarr3[2] << " / " << -rarr3[5] << " / " << total[2] << "), "
301  << " NNZ (min/max/sum): (" << rarr3[0] << " / " << -rarr3[3] << " / " << total[0] << ")\n";
302 #else
303  std::cout << "-> Rows: " << nrows << ", Cols: " << ncols << ", NNZ: " << NNZ << "\n";
304 #endif
305 }
306 
307 #ifdef MOAB_HAVE_EIGEN3
308 void moab::TempestOnlineMap::copy_tempest_sparsemat_to_eigen3()
309 {
310 #ifndef VERBOSE
311 #define VERBOSE_ACTIVATED
312 // #define VERBOSE
313 #endif
314 
315  /* Should the columns be the global size of the matrix ? */
316  m_weightMatrix.resize( m_nTotDofs_Dest, m_nTotDofs_SrcCov );
317  m_rowVector.resize( m_weightMatrix.rows() );
318  m_colVector.resize( m_weightMatrix.cols() );
319 
320 #ifdef VERBOSE
321  int locrows = std::max( m_mapRemap.GetRows(), m_nTotDofs_Dest );
322  int loccols = std::max( m_mapRemap.GetColumns(), m_nTotDofs_SrcCov );
323 
324  std::cout << m_weightMatrix.rows() << ", " << locrows << ", " << m_weightMatrix.cols() << ", " << loccols << "\n";
325  // assert(m_weightMatrix.rows() == locrows && m_weightMatrix.cols() == loccols);
326 #endif
327 
328  DataArray1D< int > lrows;
329  DataArray1D< int > lcols;
330  DataArray1D< double > lvals;
331  m_mapRemap.GetEntries( lrows, lcols, lvals );
332  size_t locvals = lvals.GetRows();
333 
334  // first matrix
335  typedef Eigen::Triplet< double > Triplet;
336  std::vector< Triplet > tripletList;
337  tripletList.reserve( locvals );
338  for( size_t iv = 0; iv < locvals; iv++ )
339  {
340  tripletList.push_back( Triplet( lrows[iv], lcols[iv], lvals[iv] ) );
341  }
342  m_weightMatrix.setFromTriplets( tripletList.begin(), tripletList.end() );
343  m_weightMatrix.makeCompressed();
344 
345 #ifdef VERBOSE
346  std::stringstream sstr;
347  sstr << "tempestmatrix.txt.0000" << rank;
348  std::ofstream output_file( sstr.str(), std::ios::out );
349  output_file << "0 " << locrows << " 0 " << loccols << "\n";
350  for( unsigned iv = 0; iv < locvals; iv++ )
351  {
352  // output_file << lrows[iv] << " " << row_ldofmap[lrows[iv]] << " " <<
353  // row_gdofmap[row_ldofmap[lrows[iv]]] << " " << col_gdofmap[col_ldofmap[lcols[iv]]] << " "
354  // << lvals[iv] << "\n";
355  output_file << row_gdofmap[row_ldofmap[lrows[iv]]] << " " << col_gdofmap[col_ldofmap[lcols[iv]]] << " "
356  << lvals[iv] << "\n";
357  }
358  output_file.flush(); // required here
359  output_file.close();
360 #endif
361 
362 #ifdef VERBOSE_ACTIVATED
363 #undef VERBOSE_ACTIVATED
364 #undef VERBOSE
365 #endif
366  return;
367 }
368 
369 ///////////////////////////////////////////////////////////////////////////////
370 
371 template < typename T >
372 static std::vector< size_t > sort_indexes( const std::vector< T >& v )
373 {
374  // initialize original index locations
375  std::vector< size_t > idx( v.size() );
376  std::iota( idx.begin(), idx.end(), 0 );
377 
378  // sort indexes based on comparing values in v
379  // using std::stable_sort instead of std::sort
380  // to avoid unnecessary index re-orderings
381  // when v contains elements of equal values
382  std::stable_sort( idx.begin(), idx.end(), [&v]( size_t i1, size_t i2 ) { return fabs( v[i1] ) > fabs( v[i2] ); } );
383 
384  return idx;
385 }
386 
387 double moab::TempestOnlineMap::QLTLimiter( int caasIteration,
388  std::vector< double >& dataCorrectedField,
389  std::vector< double >& dataLowerBound,
390  std::vector< double >& dataUpperBound,
391  std::vector< double >& dMassDefect )
392 {
393  const size_t nrows = dataCorrectedField.size();
394  double dMassL = 0.0;
395  double dMassU = 0.0;
396  std::vector< double > dataCorrection( nrows );
397  double dMassDiffCum = 0.0;
398  double dLMinusU = fabs( dataUpperBound[0] - dataLowerBound[0] );
399  const DataArray1D< double >& dTargetAreas = this->m_remapper->m_target->vecFaceArea;
400 
401  // std::vector< size_t > sortedIdx = sort_indexes( dMassDefect );
402  std::vector< std::unordered_set< int > > vecAdjTargetFaces( nrows );
403  constexpr bool useMOABAdjacencies = true;
404 #ifdef USE_ComputeAdjacencyRelations
405  if( useMOABAdjacencies )
406  ComputeAdjacencyRelations( vecAdjTargetFaces, caasIteration, m_remapper->m_target_entities,
407  useMOABAdjacencies );
408  else
409  ComputeAdjacencyRelations( vecAdjTargetFaces, caasIteration, m_remapper->m_target_entities, useMOABAdjacencies,
410  this->m_remapper->m_target );
411 #else
412  moab::MeshTopoUtil mtu( m_interface );
413  ;
414 #endif
415 
416  for( size_t i = 0; i < nrows; i++ )
417  {
418  // size_t index = sortedIdx[i];
419  size_t index = i;
420  dataCorrection[index] = fmax( dataLowerBound[index], fmin( dataUpperBound[index], 0.0 ) );
421  // dMassDiff[index] = dMassDefect[index] - dTargetAreas[index] * dataCorrection[index];
422  // dMassDiff[index] = dMassDefect[index];
423 
424  dMassL += dTargetAreas[index] * dataLowerBound[index];
425  dMassU += dTargetAreas[index] * dataUpperBound[index];
426  dLMinusU = fmax( dLMinusU, fabs( dataUpperBound[index] - dataLowerBound[index] ) );
427  dMassDiffCum += dMassDefect[index] - dTargetAreas[index] * dataCorrection[index];
428 
429 #ifndef USE_ComputeAdjacencyRelations
430  vecAdjTargetFaces[index].insert( index ); // add self target face first
431  {
432  // Compute the adjacent faces to the target face
433  if( useMOABAdjacencies )
434  {
435  moab::Range ents;
436  // ents.insert( m_remapper->m_target_entities.index( m_remapper->m_target_entities[index] ) );
437  ents.insert( m_remapper->m_target_entities[index] );
438  moab::Range adjEnts;
439  moab::ErrorCode rval = mtu.get_bridge_adjacencies( ents, 0, 2, adjEnts, caasIteration );MB_CHK_SET_ERR_CONT( rval, "Failed to get adjacent faces" );
440  for( moab::Range::iterator it = adjEnts.begin(); it != adjEnts.end(); ++it )
441  {
442  // int adjIndex = m_interface->id_from_handle(*it)-1;
443  int adjIndex = m_remapper->m_target_entities.index( *it );
444  // printf("rank: %d, Element %lu, entity: %lu, adjIndex %d\n", rank, index, *it, adjIndex);
445  if( adjIndex >= 0 ) vecAdjTargetFaces[index].insert( adjIndex );
446  }
447  }
448  else
449  {
450  AdjacentFaceVector vecAdjFaces;
451  GetAdjacentFaceVectorByEdge( *this->m_remapper->m_target, index,
452  ( m_output_order + 1 ) * ( m_output_order + 1 ) * ( m_output_order + 1 ),
453  // ( m_output_order + 1 ) * ( m_output_order + 1 ),
454  // ( 4 ) * ( m_output_order + 1 ) * ( m_output_order + 1 ),
455  vecAdjFaces );
456 
457  // Add the adjacent faces to the target face list
458  for( auto adjFace : vecAdjFaces )
459  if( adjFace.first >= 0 )
460  vecAdjTargetFaces[index].insert( adjFace.first ); // map target face to source face
461  }
462  }
463 #endif
464  }
465 
466 #ifdef MOAB_HAVE_MPI
467  std::vector< double > localDefects( 5, 0.0 ), globalDefects( 5, 0.0 );
468  localDefects[0] = dMassL;
469  localDefects[1] = dMassU;
470  localDefects[2] = dMassDiffCum;
471  localDefects[3] = dLMinusU;
472  // localDefects[4] = dMassCorrectU;
473 
474  MPI_Allreduce( localDefects.data(), globalDefects.data(), 4, MPI_DOUBLE, MPI_SUM, m_pcomm->comm() );
475 
476  dMassL = globalDefects[0];
477  dMassU = globalDefects[1];
478  dMassDiffCum = globalDefects[2];
479  dLMinusU = globalDefects[3];
480  // dMassCorrectU = globalDefects[4];
481 #endif
482 
483  //If the upper and lower bounds are too close together, just clip
484  if( fabs( dMassDiffCum ) < 1e-15 || dLMinusU < 1e-15 )
485  {
486  for( size_t i = 0; i < nrows; i++ )
487  dataCorrectedField[i] += dataCorrection[i];
488  return dMassDiffCum;
489  }
490  else
491  {
492  if( dMassL > dMassDiffCum )
493  {
494  Announce( "Lower bound mass exceeds target mass by %1.15e: CAAS will need another iteration",
495  dMassL - dMassDiffCum );
496  dMassDiffCum = dMassL;
497  // dMass -= dMassL;
498  }
499  else if( dMassU < dMassDiffCum )
500  {
501  Announce( "Target mass exceeds upper bound mass by %1.15e: CAAS will need another iteration",
502  dMassDiffCum - dMassU );
503  dMassDiffCum = dMassU;
504  // dMass -= dMassU;
505  }
506 
507  // TODO: optimize away dataMassVec by a simple transient double within the loop
508  // DataArray1D< double > dataMassVec( nrows ); //vector of mass redistribution
509  for( size_t i = 0; i < nrows; i++ )
510  {
511  // size_t index = sortedIdx[i];
512  size_t index = i;
513  const std::unordered_set< int >& neighbors = vecAdjTargetFaces[index];
514  if( dMassDefect[index] > 0.0 )
515  {
516  double dMassCorrectU = 0.0;
517  for( auto it : neighbors )
518  dMassCorrectU += dTargetAreas[it] * ( dataUpperBound[it] - dataCorrection[it] );
519 
520  // double dMassDiffCumOld = dMassDefect[index];
521  for( auto it : neighbors )
522  dataCorrection[it] +=
523  dMassDefect[index] * ( dataUpperBound[it] - dataCorrection[it] ) / dMassCorrectU;
524  }
525  else
526  {
527  double dMassCorrectL = 0.0;
528  for( auto it : neighbors )
529  dMassCorrectL += dTargetAreas[it] * ( dataCorrection[it] - dataLowerBound[it] );
530 
531  // double dMassDiffCumOld = dMassDefect[index];
532  for( auto it : neighbors )
533  dataCorrection[it] +=
534  dMassDefect[index] * ( dataCorrection[it] - dataLowerBound[it] ) / dMassCorrectL;
535  }
536  }
537 
538  for( size_t i = 0; i < nrows; i++ )
539  dataCorrectedField[i] += dataCorrection[i];
540  }
541 
542  return dMassDiffCum;
543 }
544 
545 void moab::TempestOnlineMap::CAASLimiter( std::vector< double >& dataCorrectedField,
546  std::vector< double >& dataLowerBound,
547  std::vector< double >& dataUpperBound,
548  double& dMass )
549 {
550  const size_t nrows = dataCorrectedField.size();
551  double dMassL = 0.0;
552  double dMassU = 0.0;
553  std::vector< double > dataCorrection( nrows );
554  const DataArray1D< double >& dTargetAreas = this->m_remapper->m_target->vecFaceArea;
555  double dMassDiff = dMass;
556  double dLMinusU = fabs( dataUpperBound[0] - dataLowerBound[0] );
557  double dMassCorrectU = 0.0;
558  double dMassCorrectL = 0.0;
559  for( size_t i = 0; i < nrows; i++ )
560  {
561  dataCorrection[i] = fmax( dataLowerBound[i], fmin( dataUpperBound[i], 0.0 ) );
562  dMassL += dTargetAreas[i] * dataLowerBound[i];
563  dMassU += dTargetAreas[i] * dataUpperBound[i];
564  dMassDiff -= dTargetAreas[i] * dataCorrection[i];
565  dLMinusU = fmax( dLMinusU, fabs( dataUpperBound[i] - dataLowerBound[i] ) );
566  dMassCorrectL += dTargetAreas[i] * ( dataCorrection[i] - dataLowerBound[i] );
567  dMassCorrectU += dTargetAreas[i] * ( dataUpperBound[i] - dataCorrection[i] );
568  }
569 
570 #ifdef MOAB_HAVE_MPI
571  std::vector< double > localDefects( 5, 0.0 ), globalDefects( 5, 0.0 );
572  localDefects[0] = dMassL;
573  localDefects[1] = dMassU;
574  localDefects[2] = dMassDiff;
575  localDefects[3] = dMassCorrectL;
576  localDefects[4] = dMassCorrectU;
577 
578  MPI_Allreduce( localDefects.data(), globalDefects.data(), 5, MPI_DOUBLE, MPI_SUM, m_pcomm->comm() );
579 
580  dMassL = globalDefects[0];
581  dMassU = globalDefects[1];
582  dMassDiff = globalDefects[2];
583  dMassCorrectL = globalDefects[3];
584  dMassCorrectU = globalDefects[4];
585 #endif
586 
587  //If the upper and lower bounds are too close together, just clip
588  if( fabs( dMassDiff ) < 1e-15 || fabs( dLMinusU ) < 1e-15 )
589  {
590  for( size_t i = 0; i < nrows; i++ )
591  dataCorrectedField[i] += dataCorrection[i];
592  return;
593  }
594  else
595  {
596  if( dMassL > dMassDiff )
597  {
598  Announce( "%d: Lower bound mass exceeds target mass by %1.15e: CAAS will need another iteration", rank,
599  dMassL - dMassDiff );
600  dMassDiff = dMassL;
601  dMass -= dMassL;
602  }
603  else if( dMassU < dMassDiff )
604  {
605  Announce( "%d: Target mass exceeds upper bound mass by %1.15e: CAAS will need another iteration", rank,
606  dMassDiff - dMassU );
607  dMassDiff = dMassU;
608  dMass -= dMassU;
609  }
610 
611  // TODO: optimize away dataMassVec by a simple transient double within the loop
612  DataArray1D< double > dataMassVec( nrows ); //vector of mass redistribution
613  if( dMassDiff > 0.0 )
614  {
615  for( size_t i = 0; i < nrows; i++ )
616  {
617  dataMassVec[i] = ( dataUpperBound[i] - dataCorrection[i] ) / dMassCorrectU;
618  dataCorrection[i] += dMassDiff * dataMassVec[i];
619  }
620  }
621  else
622  {
623  for( size_t i = 0; i < nrows; i++ )
624  {
625  dataMassVec[i] = ( dataCorrection[i] - dataLowerBound[i] ) / dMassCorrectL;
626  dataCorrection[i] += dMassDiff * dataMassVec[i];
627  }
628  }
629 
630  for( size_t i = 0; i < nrows; i++ )
631  dataCorrectedField[i] += dataCorrection[i];
632  }
633 
634  return;
635 }
636 
637 std::pair< double, double > moab::TempestOnlineMap::ApplyBoundsLimiting( std::vector< double >& dataInDouble,
638  std::vector< double >& dataOutDouble,
639  CAASType caasType,
640  int caasIteration,
641  double mismatch )
642 {
643  // Currently only implemented for FV to FV remapping
644  // We should generalize this to other types of remapping
645  assert( !dataGLLNodesSrcCov.IsAttached() && !dataGLLNodesDest.IsAttached() );
646 
647  std::pair< double, double > massDefect( 0.0, 0.0 );
648 
649  // Check if the source and target data are of the same size
650  const size_t nTargetCount = dataOutDouble.size();
651  const DataArray1D< double >& m_dOverlapAreas = this->m_remapper->m_overlap->vecFaceArea;
652 
653  // Apply the offline map to the data
654  double dMassDiff = 0.0;
655  std::vector< double > x( nTargetCount );
656  std::vector< double > dataLowerBound( nTargetCount );
657  std::vector< double > dataUpperBound( nTargetCount );
658  std::vector< double > massVector( nTargetCount );
659  std::vector< std::unordered_set< int > > vecSourceOvTarget( nTargetCount );
660 
661 #undef USE_ComputeAdjacencyRelations
662  constexpr bool useMOABAdjacencies = true;
663 #ifdef USE_ComputeAdjacencyRelations
664  // Compute the adjacent faces to the source face
665  // However, calling MOAB to do this does not work correctly as we need ixS to be the index
666  // Cannot just iterate over all entities in the source covering mesh
667  if( caasType == CAAS_QLT || caasType == CAAS_LOCAL_ADJACENT )
668  {
669  if( useMOABAdjacencies )
670  {
671  moab::ErrorCode rval =
672  ComputeAdjacencyRelations( vecSourceOvTarget, caasIteration, m_remapper->m_covering_source_entities,
673  useMOABAdjacencies );MB_CHK_SET_ERR_CONT( rval, "Failed to get adjacent faces" );
674  }
675  else
676  {
677  moab::ErrorCode rval =
678  ComputeAdjacencyRelations( vecSourceOvTarget, caasIteration, m_remapper->m_covering_source_entities,
679  useMOABAdjacencies, m_meshInputCov );MB_CHK_SET_ERR_CONT( rval, "Failed to get adjacent faces" );
680  }
681  }
682 #else
683  moab::MeshTopoUtil mtu( m_interface );
684 #endif
685 
686  // Initialize the bounds on the given source and target data
687  double dSourceMin = dataInDouble[0];
688  double dSourceMax = dataInDouble[0];
689  double dTargetMin = dataOutDouble[0];
690  double dTargetMax = dataOutDouble[0];
691  for( size_t i = 0; i < m_meshOverlap->faces.size(); i++ )
692  {
693  const int ixS = m_meshOverlap->vecSourceFaceIx[i];
694  const int ixT = m_meshOverlap->vecTargetFaceIx[i];
695 
696  if( ixT < 0 ) continue; // skip ghost target faces
697 
698  assert( m_dOverlapAreas[i] > 0.0 );
699  assert( ixS >= 0 );
700  assert( ixT >= 0 );
701 
702 #ifndef USE_ComputeAdjacencyRelations
703  // Compute the adjacent faces to the target face
704  vecSourceOvTarget[ixT].insert( ixS ); // map target face to source face
705  if( ( caasType == CAAS_QLT || caasType == CAAS_LOCAL_ADJACENT ) )
706  {
707  if( useMOABAdjacencies )
708  {
709  moab::Range ents;
710  ents.insert( m_remapper->m_covering_source_entities[ixS] );
711  moab::Range adjEnts;
712  moab::ErrorCode rval = mtu.get_bridge_adjacencies( ents, 0, 2, adjEnts, caasIteration );MB_CHK_SET_ERR_CONT( rval, "Failed to get adjacent faces" );
713  for( moab::Range::iterator it = adjEnts.begin(); it != adjEnts.end(); ++it )
714  {
715  int adjIndex = m_remapper->m_covering_source_entities.index( *it );
716  if( adjIndex >= 0 ) vecSourceOvTarget[ixT].insert( adjIndex );
717  }
718  }
719  else
720  {
721  // Compute the adjacent faces to the target face
722  AdjacentFaceVector vecAdjFaces;
723  GetAdjacentFaceVectorByEdge( *m_meshInputCov, ixS,
724  ( caasIteration ) * ( m_input_order + 1 ) * ( m_input_order + 1 ),
725  vecAdjFaces );
726 
727  //Compute min/max over neighboring faces
728  for( size_t iadj = 0; iadj < vecAdjFaces.size(); iadj++ )
729  vecSourceOvTarget[ixT].insert( vecAdjFaces[iadj].first ); // map target face to source face
730  }
731  }
732 #endif
733 
734  // Update the min and max values of the source data
735  dSourceMax = fmax( dSourceMax, dataInDouble[ixS] );
736  dSourceMin = fmin( dSourceMin, dataInDouble[ixS] );
737 
738  // Update the min and max values of the target data
739  dTargetMin = fmin( dTargetMin, dataOutDouble[ixT] );
740  dTargetMax = fmax( dTargetMax, dataOutDouble[ixT] );
741 
742  const double locMassDiff = ( dataInDouble[ixS] * m_dOverlapAreas[i] ) - // source mass
743  ( dataOutDouble[ixT] * m_dOverlapAreas[i] ); // target mass
744 
745  // Update the mass difference between source and target faces
746  // linked to the overlap mesh element
747  dMassDiff += locMassDiff; // target mass
748  massVector[ixT] += locMassDiff;
749  }
750 
751 #ifdef MOAB_HAVE_MPI
752  std::vector< double > localMinMaxDefects( 5, 0.0 ), globalMinMaxDefects( 5, 0.0 );
753  localMinMaxDefects[0] = dSourceMin;
754  localMinMaxDefects[1] = dTargetMin;
755  localMinMaxDefects[2] = dSourceMax;
756  localMinMaxDefects[3] = dTargetMax;
757  localMinMaxDefects[4] = dMassDiff;
758 
759  if( caasType == CAAS_GLOBAL )
760  {
761  MPI_Allreduce( localMinMaxDefects.data(), globalMinMaxDefects.data(), 2, MPI_DOUBLE, MPI_MIN, m_pcomm->comm() );
762  MPI_Allreduce( localMinMaxDefects.data() + 2, globalMinMaxDefects.data() + 2, 2, MPI_DOUBLE, MPI_MAX,
763  m_pcomm->comm() );
764  dSourceMin = globalMinMaxDefects[0];
765  dSourceMax = globalMinMaxDefects[2];
766  dTargetMin = globalMinMaxDefects[1];
767  dTargetMax = globalMinMaxDefects[3];
768  }
769  if( caasIteration == 1 )
770  MPI_Allreduce( localMinMaxDefects.data() + 4, globalMinMaxDefects.data() + 4, 1, MPI_DOUBLE, MPI_SUM,
771  m_pcomm->comm() );
772  else
773  globalMinMaxDefects[4] = mismatch;
774 
775  dMassDiff = localMinMaxDefects[4];
776  // massDefect.first = localMinMaxDefects[4];
777  massDefect.first = globalMinMaxDefects[4];
778 #else
779 
780  // massDefect.first = fabs( dMassDiff / ( dSourceMax - dSourceMin ) );
781  massDefect.first = dMassDiff;
782 #endif
783 
784  // Early exit if the values are monotone already.
785  // if( ( dTargetMax <= dSourceMax && dTargetMin <= dSourceMin ) || fabs( massDefect.first ) < 1e-16 )
786  if( fabs( massDefect.first ) > 1e-20 )
787  {
788  if( caasType == CAAS_GLOBAL )
789  {
790  for( size_t i = 0; i < nTargetCount; i++ )
791  {
792  dataLowerBound[i] = dSourceMin - dataOutDouble[i];
793  dataUpperBound[i] = dSourceMax - dataOutDouble[i];
794  }
795  } // if( caasType == CAAS_GLOBAL )
796  else // caasType == CAAS_LOCAL
797  {
798  // Compute the local min and max values of the target data
799  std::vector< double > vecLocalUpperBound( nTargetCount );
800  std::vector< double > vecLocalLowerBound( nTargetCount );
801  // Loop over the target faces and compute the min and max values
802  // of the source data linked to the target faces
803  for( size_t i = 0; i < nTargetCount; i++ )
804  {
805  assert( vecSourceOvTarget[i].size() );
806 
807  double dMinI = 1E10; // dataInDouble[vecSourceOvTarget[i][0]];
808  double dMaxI = -1E10; // dataInDouble[vecSourceOvTarget[i][0]];
809 
810  // Compute max over intersecting source faces
811  for( const auto& srcElem : vecSourceOvTarget[i] )
812  {
813  dMinI = fmin( dMinI, dataInDouble[srcElem] ); // min over intersecting source faces
814  dMaxI = fmax( dMaxI, dataInDouble[srcElem] ); // max over intersecting source faces
815  }
816 
817  // Update the min and max values of the target data
818  vecLocalLowerBound[i] = dMinI;
819  vecLocalUpperBound[i] = dMaxI;
820  }
821 
822  for( size_t i = 0; i < nTargetCount; i++ )
823  {
824  dataLowerBound[i] = vecLocalLowerBound[i] - dataOutDouble[i];
825  dataUpperBound[i] = vecLocalUpperBound[i] - dataOutDouble[i];
826  }
827  } // caasType == CAAS_LOCAL
828 
829  // Invoke CAAS or QLT application on the map
830  if( fabs( dMassDiff ) > 1e-20 )
831  {
832  if( caasType == CAAS_QLT )
833  dMassDiff = QLTLimiter( caasIteration, dataOutDouble, dataLowerBound, dataUpperBound, massVector );
834  else
835  CAASLimiter( dataOutDouble, dataLowerBound, dataUpperBound, dMassDiff );
836  }
837 
838  // Announce output mass
839  double dMassDiffPost = 0.0;
840  for( size_t i = 0; i < m_meshOverlap->faces.size(); i++ )
841  {
842  const int ixS = m_meshOverlap->vecSourceFaceIx[i];
843  const int ixT = m_meshOverlap->vecTargetFaceIx[i];
844 
845  if( ixT < 0 ) continue; // skip ghost target faces
846 
847  // Update the mass difference between source and target faces
848  // linked to the overlap mesh element
849  dMassDiffPost += ( dataInDouble[ixS] * m_dOverlapAreas[i] ) - // source mass
850  ( dataOutDouble[ixT] * m_dOverlapAreas[i] ); // target mass
851  }
852  // massDefect.second = fabs( dMassDiffPost / ( dSourceMax - dSourceMin ) );
853  massDefect.second = dMassDiffPost;
854  }
855 
856  // Ideally should perform an AllReduce here to get the global mass difference across all processors
857  // But if we satisfy the constraint on every task, essentially, the global mass difference should be zero!
858  return massDefect;
859 }
860 
861 ///////////////////////////////////////////////////////////////////////////////
862 
863 // ** Kahan Summation Algorithm for improved numerical accuracy **
864 struct KahanSum
865 {
866  double sum = 0.0;
867  double correction = 0.0;
868 
869  void add( double value )
870  {
871  double y = value - correction; // Correct the input
872  double t = sum + y; // Perform the sum
873  correction = ( t - sum ) - y; // Update correction
874  sum = t; // Store the new sum
875  }
876 
877  double result() const
878  {
879  return sum;
880  }
881 };
882 
883 // Pairwise summation helper function
884 inline double pairwiseSum( const std::set< double >& sorted )
885 {
886  if( sorted.empty() ) return 0.0;
887  if( sorted.size() == 1 ) return *sorted.begin();
888 
889  // Accumulate pairwise to minimize rounding error
890  double sum = 0.0;
891  for( double val : sorted )
892  sum += val;
893  return sum;
894 }
895 
896 // Pairwise summation helper function
897 inline double pairwiseKahanSum( const std::set< double >& sorted )
898 {
899  if( sorted.empty() ) return 0.0;
900  if( sorted.size() == 1 ) return *sorted.begin();
901 
902  // Accumulate pairwise to minimize rounding error
903  // Apply pairwise summation with Kahan correction
904  KahanSum kahan;
905  for( double val : sorted )
906  kahan.add( val );
907  return kahan.result();
908 }
909 
910 // Sparse matrix-vector multiplication using pairwise summation
911 inline void deterministicSparseMatVecMul( const typename moab::TempestOnlineMap::WeightMatrix& A,
912  const typename moab::TempestOnlineMap::WeightColVector& x,
913  typename moab::TempestOnlineMap::WeightRowVector& result )
914 {
915  constexpr bool useKahanSum = false;
916  constexpr bool usePairwiseSum = false;
917 
918  result.setZero(); // Ensure no uninitialized memory issues
919 
920  // Iterate row-wise to enforce a fixed summation order
921  for( int row = 0; row < A.outerSize(); ++row )
922  {
923  std::set< double > accumulators;
924  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
925  {
926  // accumulators contains the sorted values of the product: A(row, col) * x(col)
927  accumulators.insert( it.value() * x( it.col() ) );
928  }
929  if( usePairwiseSum ) result( row ) = pairwiseSum( accumulators );
930  if( useKahanSum ) result( row ) = pairwiseKahanSum( accumulators );
931 
932  if( !usePairwiseSum && !useKahanSum )
933  {
934  double sum = 0.0;
935  for( double val : accumulators )
936  sum += val;
937  result( row ) = sum;
938  }
939  }
940 }
941 
942 //
943 // Perform a deterministic sparse matrix-vector multiplication
944 inline void deterministicSparseMatVecMulKahan( const typename moab::TempestOnlineMap::WeightMatrix& A,
945  const typename moab::TempestOnlineMap::WeightColVector& x,
946  typename moab::TempestOnlineMap::WeightRowVector& result )
947 {
948  result.setZero(); // Ensure no uninitialized memory issues
949 
950  // Iterate row-wise to enforce a fixed summation order
951  for( int row = 0; row < A.outerSize(); ++row )
952  {
953  KahanSum kahan;
954  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
955  {
956  double product = it.value() * x( it.col() ); // Compute product
957  kahan.add( product );
958  }
959 
960  result( row ) = kahan.result();
961  }
962 }
963 
964 // Perform a deterministic sparse matrix-vector multiplication
965 inline void deterministicSparseMatVecMulClean( const typename moab::TempestOnlineMap::WeightMatrix& A,
966  const typename moab::TempestOnlineMap::WeightColVector& x,
967  typename moab::TempestOnlineMap::WeightRowVector& result )
968 {
969  result.setZero(); // Ensure no uninitialized memory issues
970 
971  // Iterate row-wise to enforce a fixed summation order
972  for( int row = 0; row < A.outerSize(); ++row )
973  {
974  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
975  {
976  const double product = it.value() * x( it.col() ); // Compute product
977  result( it.row() ) += product;
978  }
979  }
980 }
981 
982 inline void deterministicSparseMatVecMulNative( const typename moab::TempestOnlineMap::WeightMatrix& A,
983  const typename moab::TempestOnlineMap::WeightColVector& x,
984  typename moab::TempestOnlineMap::WeightRowVector& result )
985 {
986  result = A * x; // Perform the matrix-vector multiplication using Eigen3
987 }
988 
989 // Deterministic sparse matrix-vector multiplication with A^T * x using pairwise summation
990 inline void deterministicSparseMatTransposeVecMul( const typename moab::TempestOnlineMap::WeightMatrix& A,
991  const typename moab::TempestOnlineMap::WeightRowVector& x,
992  typename moab::TempestOnlineMap::WeightColVector& result )
993 {
994  result.setZero(); // Ensure no uninitialized memory issues
995 
996  // Temporary storage for pairwise summation
997  std::vector< std::set< double > > accumulators( A.cols() );
998 
999  // Iterate over A row-wise, but accumulate into result as if computing A^T * x
1000  for( int row = 0; row < A.outerSize(); ++row )
1001  {
1002  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
1003  {
1004  accumulators[it.col()].insert( it.value() * x( row ) );
1005  }
1006  }
1007 
1008  // Compute final sum using pairwise summation for each entry
1009  for( int col = 0; col < A.cols(); ++col )
1010  {
1011  // result( col ) = pairwiseSum( accumulators[col] );
1012  result( col ) = pairwiseKahanSum( accumulators[col] );
1013  }
1014 }
1015 
1016 // Perform a deterministic sparse matrix-vector multiplication
1017 inline void deterministicSparseMatTransposeVecMulClean( const typename moab::TempestOnlineMap::WeightMatrix& A,
1018  const typename moab::TempestOnlineMap::WeightRowVector& x,
1019  typename moab::TempestOnlineMap::WeightColVector& result )
1020 {
1021  result.setZero(); // Ensure no uninitialized memory issues
1022 
1023  // Iterate over A row-wise, but accumulate into result as if computing A^T * x
1024  for( int row = 0; row < A.outerSize(); ++row )
1025  {
1026  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
1027  {
1028  const double product = it.value() * x( row ); // Compute product
1029  result( it.col() ) += product; // Accumulate contributions to the corresponding row in A^T
1030  }
1031  }
1032 }
1033 
1034 // Perform a deterministic sparse matrix-vector multiplication
1035 inline void deterministicSparseMatTransposeVecMulNative( const typename moab::TempestOnlineMap::WeightMatrix& A,
1036  const typename moab::TempestOnlineMap::WeightRowVector& x,
1037  typename moab::TempestOnlineMap::WeightColVector& result )
1038 {
1039  result = A.adjoint() * x; // Perform the adjoint.matrix-vector multiplication using Eigen3
1040 }
1041 ///////////////////////////////////////////////////////////////////////////////
1042 
1043 moab::ErrorCode moab::TempestOnlineMap::ApplyWeights( std::vector< double >& srcVals,
1044  std::vector< double >& tgtVals,
1045  bool transpose )
1046 {
1047  // Reset the source and target data first
1048  m_rowVector.setZero();
1049  m_colVector.setZero();
1050 
1051 #ifdef VERBOSE
1052  std::stringstream sstr;
1053  static int callId = 0;
1054  callId++;
1055  sstr << "projection_id_" << callId << "_s_" << size << "_rk_" << rank << ".txt";
1056  std::ofstream output_file( sstr.str() );
1057 #endif
1058  // Perform the actual projection of weights: application of weight matrix onto the source
1059  // solution vector
1060 
1061  if( transpose )
1062  {
1063  // Permute the source data first
1064  for( unsigned i = 0; i < srcVals.size(); ++i )
1065  {
1066  if( row_dtoc_dofmap[i] >= 0 )
1067  m_rowVector( row_dtoc_dofmap[i] ) = srcVals[i]; // permute and set the row (source) vector properly
1068  }
1069  // deterministicSparseMatTransposeVecMulClean( m_weightMatrix, m_rowVector, m_colVector );
1070  //deterministicSparseMatTransposeVecMul( m_weightMatrix, m_rowVector, m_colVector );
1071  // deterministicSparseMatTransposeVecMulNative( m_weightMatrix, m_rowVector, m_colVector );
1072  m_colVector = m_weightMatrix.adjoint() * m_rowVector;
1073 
1074  // Permute the resulting target data back
1075  for( unsigned i = 0; i < tgtVals.size(); ++i )
1076  {
1077  if( col_dtoc_dofmap[i] >= 0 )
1078  tgtVals[i] = m_colVector( col_dtoc_dofmap[i] ); // permute and set the row (source) vector properly
1079  }
1080  }
1081  else
1082  {
1083 #ifdef VERBOSE
1084  output_file << "ColVector: " << m_colVector.size() << ", SrcVals: " << srcVals.size()
1085  << ", Sizes: " << m_nTotDofs_SrcCov << ", " << col_dtoc_dofmap.size() << "\n";
1086 #endif
1087  for( unsigned i = 0; i < srcVals.size(); ++i )
1088  {
1089  if( col_dtoc_dofmap[i] >= 0 )
1090  m_colVector( col_dtoc_dofmap[i] ) = srcVals[i]; // permute and set the row (source) vector properly
1091 #ifdef VERBOSE
1092  output_file << i << " " << col_gdofmap[col_dtoc_dofmap[i]] + 1 << " " << srcVals[i] << "\n";
1093 #endif
1094  }
1095  // deterministicSparseMatVecMulClean( m_weightMatrix, m_colVector, m_rowVector );
1096  // deterministicSparseMatVecMul( m_weightMatrix, m_colVector, m_rowVector );
1097  // deterministicSparseMatVecMulNative( m_weightMatrix, m_colVector, m_rowVector );
1098  // deterministicSparseMatVecMulKahan( m_weightMatrix, m_colVector, m_rowVector );
1099  m_rowVector = m_weightMatrix * m_colVector;
1100 
1101  // Permute the resulting target data back
1102 #ifdef VERBOSE
1103  output_file << "RowVector: " << m_rowVector.size() << ", TgtVals:" << tgtVals.size()
1104  << ", Sizes: " << m_nTotDofs_Dest << ", " << row_gdofmap.size() << "\n";
1105 #endif
1106  for( unsigned i = 0; i < tgtVals.size(); ++i )
1107  {
1108  if( row_dtoc_dofmap[i] >= 0 )
1109  {
1110  tgtVals[i] = m_rowVector( row_dtoc_dofmap[i] ); // permute and set the row (source) vector properly
1111 #ifdef VERBOSE
1112  output_file << i << " " << row_gdofmap[row_dtoc_dofmap[i]] + 1 << " " << tgtVals[i] << "\n";
1113 #endif
1114  }
1115  }
1116  }
1117 
1118  // if( caasType != CAAS_NONE )
1119  // {
1120  // constexpr int nmax_caas_iterations = 5;
1121  // double mismatch = 1.0;
1122  // int caasIteration = 0;
1123  // while( mismatch > 1e-15 &&
1124  // caasIteration++ < nmax_caas_iterations ) // iterate until convergence or a maximum of 5 iterations
1125  // {
1126  // std::pair< double, double > mDefect = this->ApplyCAASLimiting( srcVals, tgtVals, caasType );
1127  // if( m_remapper->verbose )
1128  // printf( "Rank %d: -- Iteration: %d, Net original mass defect: %3.4e, mass defect post-CAAS: %3.4e\n",
1129  // m_remapper->rank, caasIteration, mDefect.first, mDefect.second );
1130  // mismatch = mDefect.second;
1131  // }
1132  // }
1133 
1134 #ifdef VERBOSE
1135  output_file.flush(); // required here
1136  output_file.close();
1137 #endif
1138 
1139  // All done with matvec application
1140  return moab::MB_SUCCESS;
1141 }
1142 
1143 #endif
1144 
1145 ///////////////////////////////////////////////////////////////////////////////
1146 
1147 extern void ForceConsistencyConservation3( const DataArray1D< double >& vecSourceArea,
1148  const DataArray1D< double >& vecTargetArea,
1149  DataArray2D< double >& dCoeff,
1150  bool fMonotone,
1151  bool fSparseConstraints = false );
1152 
1153 ///////////////////////////////////////////////////////////////////////////////
1154 
1155 extern void ForceIntArrayConsistencyConservation( const DataArray1D< double >& vecSourceArea,
1156  const DataArray1D< double >& vecTargetArea,
1157  DataArray2D< double >& dCoeff,
1158  bool fMonotone );
1159 
1160 ///////////////////////////////////////////////////////////////////////////////
1161 
1162 void moab::TempestOnlineMap::LinearRemapSE4_Tempest_MOAB( const DataArray3D< int >& dataGLLNodes,
1163  const DataArray3D< double >& dataGLLJacobian,
1164  int nMonotoneType,
1165  bool fContinuousIn,
1166  bool fNoConservation )
1167 {
1168  // Order of the polynomial interpolant
1169  int nP = dataGLLNodes.GetRows();
1170 
1171  // Order of triangular quadrature rule
1172  const int TriQuadRuleOrder = 4;
1173 
1174  // Triangular quadrature rule
1175  TriangularQuadratureRule triquadrule( TriQuadRuleOrder );
1176 
1177  int TriQuadraturePoints = triquadrule.GetPoints();
1178 
1179  const DataArray2D< double >& TriQuadratureG = triquadrule.GetG();
1180 
1181  const DataArray1D< double >& TriQuadratureW = triquadrule.GetW();
1182 
1183  // Sample coefficients
1184  DataArray2D< double > dSampleCoeff( nP, nP );
1185 
1186  // GLL Quadrature nodes on quadrilateral elements
1187  DataArray1D< double > dG;
1188  DataArray1D< double > dW;
1189  GaussLobattoQuadrature::GetPoints( nP, 0.0, 1.0, dG, dW );
1190 
1191  // Announcements
1192  moab::DebugOutput dbgprint( std::cout, this->rank, 0 );
1193  dbgprint.set_prefix( "[LinearRemapSE4_Tempest_MOAB]: " );
1194  if( is_root )
1195  {
1196  dbgprint.printf( 0, "Finite Element to Finite Volume Projection\n" );
1197  dbgprint.printf( 0, "Triangular quadrature rule order %i\n", TriQuadRuleOrder );
1198  dbgprint.printf( 0, "Order of the FE polynomial interpolant: %i\n", nP );
1199  }
1200 
1201  // Get SparseMatrix represntation of the OfflineMap
1202  SparseMatrix< double >& smatMap = this->GetSparseMatrix();
1203 
1204  // NodeVector from m_meshOverlap
1205  const NodeVector& nodesOverlap = m_meshOverlap->nodes;
1206  const NodeVector& nodesFirst = m_meshInputCov->nodes;
1207 
1208  // Vector of source areas
1209  DataArray1D< double > vecSourceArea( nP * nP );
1210 
1211  DataArray1D< double > vecTargetArea;
1212  DataArray2D< double > dCoeff;
1213 
1214 #ifdef VERBOSE
1215  std::stringstream sstr;
1216  sstr << "remapdata_" << rank << ".txt";
1217  std::ofstream output_file( sstr.str() );
1218 #endif
1219 
1220  // Current Overlap Face
1221  int ixOverlap = 0;
1222 #ifdef VERBOSE
1223  const unsigned outputFrequency = ( m_meshInputCov->faces.size() / 10 ) + 1;
1224 #endif
1225  // generic triangle used for area computation, for triangles around the center of overlap face;
1226  // used for overlap faces with more than 4 edges;
1227  // nodes array will be set for each triangle;
1228  // these triangles are not part of the mesh structure, they are just temporary during
1229  // aforementioned decomposition.
1230  Face faceTri( 3 );
1231  NodeVector nodes( 3 );
1232  faceTri.SetNode( 0, 0 );
1233  faceTri.SetNode( 1, 1 );
1234  faceTri.SetNode( 2, 2 );
1235 
1236  // Loop over all input Faces
1237  for( size_t ixFirst = 0; ixFirst < m_meshInputCov->faces.size(); ixFirst++ )
1238  {
1239  const Face& faceFirst = m_meshInputCov->faces[ixFirst];
1240 
1241  if( faceFirst.edges.size() != 4 )
1242  {
1243  _EXCEPTIONT( "Only quadrilateral elements allowed for SE remapping" );
1244  }
1245 #ifdef VERBOSE
1246  // Announce computation progress
1247  if( ixFirst % outputFrequency == 0 && is_root )
1248  {
1249  dbgprint.printf( 0, "Element %zu/%lu\n", ixFirst, m_meshInputCov->faces.size() );
1250  }
1251 #endif
1252  // Need to re-number the overlap elements such that vecSourceFaceIx[a:b] = 0, then 1 and so
1253  // on wrt the input mesh data Then the overlap_end and overlap_begin will be correct.
1254  // However, the relation with MOAB and Tempest will go out of the roof
1255 
1256  // Determine how many overlap Faces and triangles are present
1257  int nOverlapFaces = 0;
1258  size_t ixOverlapTemp = ixOverlap;
1259  for( ; ixOverlapTemp < m_meshOverlap->faces.size(); ixOverlapTemp++ )
1260  {
1261  // if( m_meshOverlap->vecTargetFaceIx[ixOverlapTemp] < 0 ) continue; // skip ghost target faces
1262  // const Face & faceOverlap = m_meshOverlap->faces[ixOverlapTemp];
1263  if( ixFirst - m_meshOverlap->vecSourceFaceIx[ixOverlapTemp] != 0 ) break;
1264 
1265  nOverlapFaces++;
1266  }
1267 
1268  // No overlaps
1269  if( nOverlapFaces == 0 ) continue;
1270 
1271  // Allocate remap coefficients array for meshFirst Face
1272  DataArray3D< double > dRemapCoeff( nP, nP, nOverlapFaces );
1273 
1274  // Find the local remap coefficients
1275  for( int j = 0; j < nOverlapFaces; j++ )
1276  {
1277  const Face& faceOverlap = m_meshOverlap->faces[ixOverlap + j];
1278  if( m_meshOverlap->vecFaceArea[ixOverlap + j] < 1.e-16 ) // machine precision
1279  {
1280  Announce( "Very small overlap at index %i area polygon: (%1.10e )", ixOverlap + j,
1281  m_meshOverlap->vecFaceArea[ixOverlap + j] );
1282  int n = faceOverlap.edges.size();
1283  Announce( "Number nodes: %d", n );
1284  for( int k = 0; k < n; k++ )
1285  {
1286  Node nd = nodesOverlap[faceOverlap[k]];
1287  Announce( "Node %d %d : %1.10e %1.10e %1.10e ", k, faceOverlap[k], nd.x, nd.y, nd.z );
1288  }
1289  continue;
1290  }
1291 
1292  // #ifdef VERBOSE
1293  // if ( is_root )
1294  // Announce ( "\tLocal ID: %i/%i = %i, areas = %2.8e", j + ixOverlap, nOverlapFaces,
1295  // m_remapper->lid_to_gid_covsrc[m_meshOverlap->vecSourceFaceIx[ixOverlap + j]],
1296  // m_meshOverlap->vecFaceArea[ixOverlap + j] );
1297  // #endif
1298 
1299  int nbEdges = faceOverlap.edges.size();
1300  int nOverlapTriangles = 1;
1301  Node center; // not used if nbEdges == 3
1302  if( nbEdges > 3 )
1303  { // decompose from center in this case
1304  nOverlapTriangles = nbEdges;
1305  for( int k = 0; k < nbEdges; k++ )
1306  {
1307  const Node& node = nodesOverlap[faceOverlap[k]];
1308  center = center + node;
1309  }
1310  center = center / nbEdges;
1311  center = center.Normalized(); // project back on sphere of radius 1
1312  }
1313 
1314  Node node0, node1, node2;
1315  double dTriangleArea;
1316 
1317  // Loop over all sub-triangles of this Overlap Face
1318  for( int k = 0; k < nOverlapTriangles; k++ )
1319  {
1320  if( nbEdges == 3 ) // will come here only once, nOverlapTriangles == 1 in this case
1321  {
1322  node0 = nodesOverlap[faceOverlap[0]];
1323  node1 = nodesOverlap[faceOverlap[1]];
1324  node2 = nodesOverlap[faceOverlap[2]];
1325  dTriangleArea = CalculateFaceArea( faceOverlap, nodesOverlap );
1326  }
1327  else // decompose polygon in triangles around the center
1328  {
1329  node0 = center;
1330  node1 = nodesOverlap[faceOverlap[k]];
1331  int k1 = ( k + 1 ) % nbEdges;
1332  node2 = nodesOverlap[faceOverlap[k1]];
1333  nodes[0] = center;
1334  nodes[1] = node1;
1335  nodes[2] = node2;
1336  dTriangleArea = CalculateFaceArea( faceTri, nodes );
1337  }
1338  // Coordinates of quadrature Node
1339  for( int l = 0; l < TriQuadraturePoints; l++ )
1340  {
1341  Node nodeQuadrature;
1342  nodeQuadrature.x = TriQuadratureG[l][0] * node0.x + TriQuadratureG[l][1] * node1.x +
1343  TriQuadratureG[l][2] * node2.x;
1344 
1345  nodeQuadrature.y = TriQuadratureG[l][0] * node0.y + TriQuadratureG[l][1] * node1.y +
1346  TriQuadratureG[l][2] * node2.y;
1347 
1348  nodeQuadrature.z = TriQuadratureG[l][0] * node0.z + TriQuadratureG[l][1] * node1.z +
1349  TriQuadratureG[l][2] * node2.z;
1350 
1351  nodeQuadrature = nodeQuadrature.Normalized();
1352 
1353  // Find components of quadrature point in basis
1354  // of the first Face
1355  double dAlpha;
1356  double dBeta;
1357 
1358  ApplyInverseMap( faceFirst, nodesFirst, nodeQuadrature, dAlpha, dBeta );
1359 
1360  // Check inverse map value
1361  if( ( dAlpha < -1.0e-13 ) || ( dAlpha > 1.0 + 1.0e-13 ) || ( dBeta < -1.0e-13 ) ||
1362  ( dBeta > 1.0 + 1.0e-13 ) )
1363  {
1364  _EXCEPTION4( "Inverse Map for element %d and subtriangle %d out of range "
1365  "(%1.5e %1.5e)",
1366  j, l, dAlpha, dBeta );
1367  }
1368 
1369  // Sample the finite element at this point
1370  SampleGLLFiniteElement( nMonotoneType, nP, dAlpha, dBeta, dSampleCoeff );
1371 
1372  // Add sample coefficients to the map if m_meshOverlap->vecFaceArea[ixOverlap + j] > 0
1373  for( int p = 0; p < nP; p++ )
1374  {
1375  for( int q = 0; q < nP; q++ )
1376  {
1377  dRemapCoeff[p][q][j] += TriQuadratureW[l] * dTriangleArea * dSampleCoeff[p][q] /
1378  m_meshOverlap->vecFaceArea[ixOverlap + j];
1379  }
1380  }
1381  }
1382  }
1383  }
1384 
1385 #ifdef VERBOSE
1386  output_file << "[" << m_remapper->lid_to_gid_covsrc[ixFirst] << "] \t";
1387  for( int j = 0; j < nOverlapFaces; j++ )
1388  {
1389  for( int p = 0; p < nP; p++ )
1390  {
1391  for( int q = 0; q < nP; q++ )
1392  {
1393  output_file << dRemapCoeff[p][q][j] << " ";
1394  }
1395  }
1396  }
1397  output_file << std::endl;
1398 #endif
1399 
1400  // Force consistency and conservation
1401  if( !fNoConservation )
1402  {
1403  double dTargetArea = 0.0;
1404  for( int j = 0; j < nOverlapFaces; j++ )
1405  {
1406  dTargetArea += m_meshOverlap->vecFaceArea[ixOverlap + j];
1407  }
1408 
1409  for( int p = 0; p < nP; p++ )
1410  {
1411  for( int q = 0; q < nP; q++ )
1412  {
1413  vecSourceArea[p * nP + q] = dataGLLJacobian[p][q][ixFirst];
1414  }
1415  }
1416 
1417  const double areaTolerance = 1e-10;
1418  // Source elements are completely covered by target volumes
1419  if( fabs( m_meshInputCov->vecFaceArea[ixFirst] - dTargetArea ) <= areaTolerance )
1420  {
1421  vecTargetArea.Allocate( nOverlapFaces );
1422  for( int j = 0; j < nOverlapFaces; j++ )
1423  {
1424  vecTargetArea[j] = m_meshOverlap->vecFaceArea[ixOverlap + j];
1425  }
1426 
1427  dCoeff.Allocate( nOverlapFaces, nP * nP );
1428 
1429  for( int j = 0; j < nOverlapFaces; j++ )
1430  {
1431  for( int p = 0; p < nP; p++ )
1432  {
1433  for( int q = 0; q < nP; q++ )
1434  {
1435  dCoeff[j][p * nP + q] = dRemapCoeff[p][q][j];
1436  }
1437  }
1438  }
1439 
1440  // Target volumes only partially cover source elements
1441  }
1442  else if( m_meshInputCov->vecFaceArea[ixFirst] - dTargetArea > areaTolerance )
1443  {
1444  double dExtraneousArea = m_meshInputCov->vecFaceArea[ixFirst] - dTargetArea;
1445 
1446  vecTargetArea.Allocate( nOverlapFaces + 1 );
1447  for( int j = 0; j < nOverlapFaces; j++ )
1448  {
1449  vecTargetArea[j] = m_meshOverlap->vecFaceArea[ixOverlap + j];
1450  }
1451  vecTargetArea[nOverlapFaces] = dExtraneousArea;
1452 
1453 #ifdef VERBOSE
1454  Announce( "Partial volume: %i (%1.10e / %1.10e)", ixFirst, dTargetArea,
1455  m_meshInputCov->vecFaceArea[ixFirst] );
1456 #endif
1457  if( dTargetArea > m_meshInputCov->vecFaceArea[ixFirst] )
1458  {
1459  _EXCEPTIONT( "Partial element area exceeds total element area" );
1460  }
1461 
1462  dCoeff.Allocate( nOverlapFaces + 1, nP * nP );
1463 
1464  for( int j = 0; j < nOverlapFaces; j++ )
1465  {
1466  for( int p = 0; p < nP; p++ )
1467  {
1468  for( int q = 0; q < nP; q++ )
1469  {
1470  dCoeff[j][p * nP + q] = dRemapCoeff[p][q][j];
1471  }
1472  }
1473  }
1474  for( int p = 0; p < nP; p++ )
1475  {
1476  for( int q = 0; q < nP; q++ )
1477  {
1478  dCoeff[nOverlapFaces][p * nP + q] = dataGLLJacobian[p][q][ixFirst];
1479  }
1480  }
1481  for( int j = 0; j < nOverlapFaces; j++ )
1482  {
1483  for( int p = 0; p < nP; p++ )
1484  {
1485  for( int q = 0; q < nP; q++ )
1486  {
1487  dCoeff[nOverlapFaces][p * nP + q] -=
1488  dRemapCoeff[p][q][j] * m_meshOverlap->vecFaceArea[ixOverlap + j];
1489  }
1490  }
1491  }
1492  for( int p = 0; p < nP; p++ )
1493  {
1494  for( int q = 0; q < nP; q++ )
1495  {
1496  dCoeff[nOverlapFaces][p * nP + q] /= dExtraneousArea;
1497  }
1498  }
1499 
1500  // Source elements only partially cover target volumes
1501  }
1502  else
1503  {
1504  Announce( "Coverage area: %1.10e, and target element area: %1.10e)", ixFirst,
1505  m_meshInputCov->vecFaceArea[ixFirst], dTargetArea );
1506  _EXCEPTIONT( "Target grid must be a subset of source grid" );
1507  }
1508 
1509  ForceConsistencyConservation3( vecSourceArea, vecTargetArea, dCoeff, ( nMonotoneType > 0 )
1510  /*, m_remapper->lid_to_gid_covsrc[ixFirst]*/ );
1511 
1512  for( int j = 0; j < nOverlapFaces; j++ )
1513  {
1514  for( int p = 0; p < nP; p++ )
1515  {
1516  for( int q = 0; q < nP; q++ )
1517  {
1518  dRemapCoeff[p][q][j] = dCoeff[j][p * nP + q];
1519  }
1520  }
1521  }
1522  }
1523 
1524 #ifdef VERBOSE
1525  // output_file << "[" << m_remapper->lid_to_gid_covsrc[ixFirst] << "] \t";
1526  // for ( int j = 0; j < nOverlapFaces; j++ )
1527  // {
1528  // for ( int p = 0; p < nP; p++ )
1529  // {
1530  // for ( int q = 0; q < nP; q++ )
1531  // {
1532  // output_file << dRemapCoeff[p][q][j] << " ";
1533  // }
1534  // }
1535  // }
1536  // output_file << std::endl;
1537 #endif
1538 
1539  // Put these remap coefficients into the SparseMatrix map
1540  for( int j = 0; j < nOverlapFaces; j++ )
1541  {
1542  int ixSecondFace = m_meshOverlap->vecTargetFaceIx[ixOverlap + j];
1543 
1544  // signal to not participate, because it is a ghost target
1545  if( ixSecondFace < 0 ) continue; // do not do anything
1546 
1547  for( int p = 0; p < nP; p++ )
1548  {
1549  for( int q = 0; q < nP; q++ )
1550  {
1551  if( fContinuousIn )
1552  {
1553  int ixFirstNode = dataGLLNodes[p][q][ixFirst] - 1;
1554 
1555  smatMap( ixSecondFace, ixFirstNode ) += dRemapCoeff[p][q][j] *
1556  m_meshOverlap->vecFaceArea[ixOverlap + j] /
1557  m_meshOutput->vecFaceArea[ixSecondFace];
1558  }
1559  else
1560  {
1561  int ixFirstNode = ixFirst * nP * nP + p * nP + q;
1562 
1563  smatMap( ixSecondFace, ixFirstNode ) += dRemapCoeff[p][q][j] *
1564  m_meshOverlap->vecFaceArea[ixOverlap + j] /
1565  m_meshOutput->vecFaceArea[ixSecondFace];
1566  }
1567  }
1568  }
1569  }
1570  // Increment the current overlap index
1571  ixOverlap += nOverlapFaces;
1572  }
1573 #ifdef VERBOSE
1574  output_file.flush(); // required here
1575  output_file.close();
1576 #endif
1577 
1578  return;
1579 }
1580 
1581 ///////////////////////////////////////////////////////////////////////////////
1582 
1583 void moab::TempestOnlineMap::LinearRemapGLLtoGLL2_MOAB( const DataArray3D< int >& dataGLLNodesIn,
1584  const DataArray3D< double >& dataGLLJacobianIn,
1585  const DataArray3D< int >& dataGLLNodesOut,
1586  const DataArray3D< double >& dataGLLJacobianOut,
1587  const DataArray1D< double >& dataNodalAreaOut,
1588  int nPin,
1589  int nPout,
1590  int nMonotoneType,
1591  bool fContinuousIn,
1592  bool fContinuousOut,
1593  bool fNoConservation )
1594 {
1595  // Triangular quadrature rule
1596  TriangularQuadratureRule triquadrule( 8 );
1597 
1598  const DataArray2D< double >& dG = triquadrule.GetG();
1599  const DataArray1D< double >& dW = triquadrule.GetW();
1600 
1601  // Get SparseMatrix represntation of the OfflineMap
1602  SparseMatrix< double >& smatMap = this->GetSparseMatrix();
1603 
1604  // Sample coefficients
1605  DataArray2D< double > dSampleCoeffIn( nPin, nPin );
1606  DataArray2D< double > dSampleCoeffOut( nPout, nPout );
1607 
1608  // Announcemnets
1609  moab::DebugOutput dbgprint( std::cout, this->rank, 0 );
1610  dbgprint.set_prefix( "[LinearRemapGLLtoGLL2_MOAB]: " );
1611  if( is_root )
1612  {
1613  dbgprint.printf( 0, "Finite Element to Finite Element Projection\n" );
1614  dbgprint.printf( 0, "Order of the input FE polynomial interpolant: %i\n", nPin );
1615  dbgprint.printf( 0, "Order of the output FE polynomial interpolant: %i\n", nPout );
1616  }
1617 
1618  // Build the integration array for each element on m_meshOverlap
1619  DataArray3D< double > dGlobalIntArray( nPin * nPin, m_meshOverlap->faces.size(), nPout * nPout );
1620 
1621  // Number of overlap Faces per source Face
1622  DataArray1D< int > nAllOverlapFaces( m_meshInputCov->faces.size() );
1623 
1624  int ixOverlap = 0;
1625  for( size_t ixFirst = 0; ixFirst < m_meshInputCov->faces.size(); ixFirst++ )
1626  {
1627  // Determine how many overlap Faces and triangles are present
1628  int nOverlapFaces = 0;
1629  size_t ixOverlapTemp = ixOverlap;
1630  for( ; ixOverlapTemp < m_meshOverlap->faces.size(); ixOverlapTemp++ )
1631  {
1632  // const Face & faceOverlap = m_meshOverlap->faces[ixOverlapTemp];
1633  if( ixFirst - m_meshOverlap->vecSourceFaceIx[ixOverlapTemp] != 0 )
1634  {
1635  break;
1636  }
1637 
1638  nOverlapFaces++;
1639  }
1640 
1641  nAllOverlapFaces[ixFirst] = nOverlapFaces;
1642 
1643  // Increment the current overlap index
1644  ixOverlap += nAllOverlapFaces[ixFirst];
1645  }
1646 
1647  // Geometric area of each output node
1648  DataArray2D< double > dGeometricOutputArea( m_meshOutput->faces.size(), nPout * nPout );
1649 
1650  // Area of each overlap element in the output basis
1651  DataArray2D< double > dOverlapOutputArea( m_meshOverlap->faces.size(), nPout * nPout );
1652 
1653  // Loop through all faces on m_meshInputCov
1654  ixOverlap = 0;
1655 #ifdef VERBOSE
1656  const unsigned outputFrequency = ( m_meshInputCov->faces.size() / 10 ) + 1;
1657 #endif
1658  if( is_root ) dbgprint.printf( 0, "Building conservative distribution maps\n" );
1659 
1660  // generic triangle used for area computation, for triangles around the center of overlap face;
1661  // used for overlap faces with more than 4 edges;
1662  // nodes array will be set for each triangle;
1663  // these triangles are not part of the mesh structure, they are just temporary during
1664  // aforementioned decomposition.
1665  Face faceTri( 3 );
1666  NodeVector nodes( 3 );
1667  faceTri.SetNode( 0, 0 );
1668  faceTri.SetNode( 1, 1 );
1669  faceTri.SetNode( 2, 2 );
1670 
1671  for( size_t ixFirst = 0; ixFirst < m_meshInputCov->faces.size(); ixFirst++ )
1672  {
1673 #ifdef VERBOSE
1674  // Announce computation progress
1675  if( ixFirst % outputFrequency == 0 && is_root )
1676  {
1677  dbgprint.printf( 0, "Element %zu/%lu\n", ixFirst, m_meshInputCov->faces.size() );
1678  }
1679 #endif
1680  // Quantities from the First Mesh
1681  const Face& faceFirst = m_meshInputCov->faces[ixFirst];
1682 
1683  const NodeVector& nodesFirst = m_meshInputCov->nodes;
1684 
1685  // Number of overlapping Faces and triangles
1686  int nOverlapFaces = nAllOverlapFaces[ixFirst];
1687 
1688  if( !nOverlapFaces ) continue;
1689 
1690  // // Calculate total element Jacobian
1691  // double dTotalJacobian = 0.0;
1692  // for (int s = 0; s < nPin; s++) {
1693  // for (int t = 0; t < nPin; t++) {
1694  // dTotalJacobian += dataGLLJacobianIn[s][t][ixFirst];
1695  // }
1696  // }
1697 
1698  // Loop through all Overlap Faces
1699  for( int i = 0; i < nOverlapFaces; i++ )
1700  {
1701  // Quantities from the overlap Mesh
1702  const Face& faceOverlap = m_meshOverlap->faces[ixOverlap + i];
1703 
1704  const NodeVector& nodesOverlap = m_meshOverlap->nodes;
1705 
1706  // Quantities from the Second Mesh
1707  int ixSecond = m_meshOverlap->vecTargetFaceIx[ixOverlap + i];
1708 
1709  // signal to not participate, because it is a ghost target
1710  if( ixSecond < 0 ) continue; // do not do anything
1711 
1712  const NodeVector& nodesSecond = m_meshOutput->nodes;
1713 
1714  const Face& faceSecond = m_meshOutput->faces[ixSecond];
1715 
1716  int nbEdges = faceOverlap.edges.size();
1717  int nOverlapTriangles = 1;
1718  Node center; // not used if nbEdges == 3
1719  if( nbEdges > 3 )
1720  { // decompose from center in this case
1721  nOverlapTriangles = nbEdges;
1722  for( int k = 0; k < nbEdges; k++ )
1723  {
1724  const Node& node = nodesOverlap[faceOverlap[k]];
1725  center = center + node;
1726  }
1727  center = center / nbEdges;
1728  center = center.Normalized(); // project back on sphere of radius 1
1729  }
1730 
1731  Node node0, node1, node2;
1732  double dTriArea;
1733 
1734  // Loop over all sub-triangles of this Overlap Face
1735  for( int j = 0; j < nOverlapTriangles; j++ )
1736  {
1737  if( nbEdges == 3 ) // will come here only once, nOverlapTriangles == 1 in this case
1738  {
1739  node0 = nodesOverlap[faceOverlap[0]];
1740  node1 = nodesOverlap[faceOverlap[1]];
1741  node2 = nodesOverlap[faceOverlap[2]];
1742  dTriArea = CalculateFaceArea( faceOverlap, nodesOverlap );
1743  }
1744  else // decompose polygon in triangles around the center
1745  {
1746  node0 = center;
1747  node1 = nodesOverlap[faceOverlap[j]];
1748  int j1 = ( j + 1 ) % nbEdges;
1749  node2 = nodesOverlap[faceOverlap[j1]];
1750  nodes[0] = center;
1751  nodes[1] = node1;
1752  nodes[2] = node2;
1753  dTriArea = CalculateFaceArea( faceTri, nodes );
1754  }
1755 
1756  for( int k = 0; k < triquadrule.GetPoints(); k++ )
1757  {
1758  // Get the nodal location of this point
1759  double dX[3];
1760 
1761  dX[0] = dG( k, 0 ) * node0.x + dG( k, 1 ) * node1.x + dG( k, 2 ) * node2.x;
1762  dX[1] = dG( k, 0 ) * node0.y + dG( k, 1 ) * node1.y + dG( k, 2 ) * node2.y;
1763  dX[2] = dG( k, 0 ) * node0.z + dG( k, 1 ) * node1.z + dG( k, 2 ) * node2.z;
1764 
1765  double dMag = sqrt( dX[0] * dX[0] + dX[1] * dX[1] + dX[2] * dX[2] );
1766 
1767  dX[0] /= dMag;
1768  dX[1] /= dMag;
1769  dX[2] /= dMag;
1770 
1771  Node nodeQuadrature( dX[0], dX[1], dX[2] );
1772 
1773  // Find the components of this quadrature point in the basis
1774  // of the first Face.
1775  double dAlphaIn;
1776  double dBetaIn;
1777 
1778  ApplyInverseMap( faceFirst, nodesFirst, nodeQuadrature, dAlphaIn, dBetaIn );
1779 
1780  // Find the components of this quadrature point in the basis
1781  // of the second Face.
1782  double dAlphaOut;
1783  double dBetaOut;
1784 
1785  ApplyInverseMap( faceSecond, nodesSecond, nodeQuadrature, dAlphaOut, dBetaOut );
1786 
1787  /*
1788  // Check inverse map value
1789  if ((dAlphaIn < 0.0) || (dAlphaIn > 1.0) ||
1790  (dBetaIn < 0.0) || (dBetaIn > 1.0)
1791  ) {
1792  _EXCEPTION2("Inverse Map out of range (%1.5e %1.5e)",
1793  dAlphaIn, dBetaIn);
1794  }
1795 
1796  // Check inverse map value
1797  if ((dAlphaOut < 0.0) || (dAlphaOut > 1.0) ||
1798  (dBetaOut < 0.0) || (dBetaOut > 1.0)
1799  ) {
1800  _EXCEPTION2("Inverse Map out of range (%1.5e %1.5e)",
1801  dAlphaOut, dBetaOut);
1802  }
1803  */
1804  // Sample the First finite element at this point
1805  SampleGLLFiniteElement( nMonotoneType, nPin, dAlphaIn, dBetaIn, dSampleCoeffIn );
1806 
1807  // Sample the Second finite element at this point
1808  SampleGLLFiniteElement( nMonotoneType, nPout, dAlphaOut, dBetaOut, dSampleCoeffOut );
1809 
1810  // Overlap output area
1811  for( int s = 0; s < nPout; s++ )
1812  {
1813  for( int t = 0; t < nPout; t++ )
1814  {
1815  double dNodeArea = dSampleCoeffOut[s][t] * dW[k] * dTriArea;
1816 
1817  dOverlapOutputArea[ixOverlap + i][s * nPout + t] += dNodeArea;
1818 
1819  dGeometricOutputArea[ixSecond][s * nPout + t] += dNodeArea;
1820  }
1821  }
1822 
1823  // Compute overlap integral
1824  int ixp = 0;
1825  for( int p = 0; p < nPin; p++ )
1826  {
1827  for( int q = 0; q < nPin; q++ )
1828  {
1829  int ixs = 0;
1830  for( int s = 0; s < nPout; s++ )
1831  {
1832  for( int t = 0; t < nPout; t++ )
1833  {
1834  // Sample the Second finite element at this point
1835  dGlobalIntArray[ixp][ixOverlap + i][ixs] +=
1836  dSampleCoeffOut[s][t] * dSampleCoeffIn[p][q] * dW[k] * dTriArea;
1837 
1838  ixs++;
1839  }
1840  }
1841 
1842  ixp++;
1843  }
1844  }
1845  }
1846  }
1847  }
1848 
1849  // Coefficients
1850  DataArray2D< double > dCoeff( nOverlapFaces * nPout * nPout, nPin * nPin );
1851 
1852  for( int i = 0; i < nOverlapFaces; i++ )
1853  {
1854  // int ixSecondFace = m_meshOverlap->vecTargetFaceIx[ixOverlap + i];
1855 
1856  int ixp = 0;
1857  for( int p = 0; p < nPin; p++ )
1858  {
1859  for( int q = 0; q < nPin; q++ )
1860  {
1861  int ixs = 0;
1862  for( int s = 0; s < nPout; s++ )
1863  {
1864  for( int t = 0; t < nPout; t++ )
1865  {
1866  dCoeff[i * nPout * nPout + ixs][ixp] = dGlobalIntArray[ixp][ixOverlap + i][ixs] /
1867  dOverlapOutputArea[ixOverlap + i][s * nPout + t];
1868 
1869  ixs++;
1870  }
1871  }
1872 
1873  ixp++;
1874  }
1875  }
1876  }
1877 
1878  // Source areas
1879  DataArray1D< double > vecSourceArea( nPin * nPin );
1880 
1881  for( int p = 0; p < nPin; p++ )
1882  {
1883  for( int q = 0; q < nPin; q++ )
1884  {
1885  vecSourceArea[p * nPin + q] = dataGLLJacobianIn[p][q][ixFirst];
1886  }
1887  }
1888 
1889  // Target areas
1890  DataArray1D< double > vecTargetArea( nOverlapFaces * nPout * nPout );
1891 
1892  for( int i = 0; i < nOverlapFaces; i++ )
1893  {
1894  // int ixSecond = m_meshOverlap->vecTargetFaceIx[ixOverlap + i];
1895  int ixs = 0;
1896  for( int s = 0; s < nPout; s++ )
1897  {
1898  for( int t = 0; t < nPout; t++ )
1899  {
1900  vecTargetArea[i * nPout * nPout + ixs] = dOverlapOutputArea[ixOverlap + i][nPout * s + t];
1901 
1902  ixs++;
1903  }
1904  }
1905  }
1906 
1907  // Force consistency and conservation
1908  if( !fNoConservation )
1909  {
1910  ForceIntArrayConsistencyConservation( vecSourceArea, vecTargetArea, dCoeff, ( nMonotoneType != 0 ) );
1911  }
1912 
1913  // Update global coefficients
1914  for( int i = 0; i < nOverlapFaces; i++ )
1915  {
1916  int ixp = 0;
1917  for( int p = 0; p < nPin; p++ )
1918  {
1919  for( int q = 0; q < nPin; q++ )
1920  {
1921  int ixs = 0;
1922  for( int s = 0; s < nPout; s++ )
1923  {
1924  for( int t = 0; t < nPout; t++ )
1925  {
1926  dGlobalIntArray[ixp][ixOverlap + i][ixs] =
1927  dCoeff[i * nPout * nPout + ixs][ixp] * dOverlapOutputArea[ixOverlap + i][s * nPout + t];
1928 
1929  ixs++;
1930  }
1931  }
1932 
1933  ixp++;
1934  }
1935  }
1936  }
1937 
1938 #ifdef VVERBOSE
1939  // Check column sums (conservation)
1940  for( int i = 0; i < nPin * nPin; i++ )
1941  {
1942  double dColSum = 0.0;
1943  for( int j = 0; j < nOverlapFaces * nPout * nPout; j++ )
1944  {
1945  dColSum += dCoeff[j][i] * vecTargetArea[j];
1946  }
1947  printf( "Col %i: %1.15e\n", i, dColSum / vecSourceArea[i] );
1948  }
1949 
1950  // Check row sums (consistency)
1951  for( int j = 0; j < nOverlapFaces * nPout * nPout; j++ )
1952  {
1953  double dRowSum = 0.0;
1954  for( int i = 0; i < nPin * nPin; i++ )
1955  {
1956  dRowSum += dCoeff[j][i];
1957  }
1958  printf( "Row %i: %1.15e\n", j, dRowSum );
1959  }
1960 #endif
1961 
1962  // Increment the current overlap index
1963  ixOverlap += nOverlapFaces;
1964  }
1965 
1966  // Build redistribution map within target element
1967  if( is_root ) dbgprint.printf( 0, "Building redistribution maps on target mesh\n" );
1968  DataArray1D< double > dRedistSourceArea( nPout * nPout );
1969  DataArray1D< double > dRedistTargetArea( nPout * nPout );
1970  std::vector< DataArray2D< double > > dRedistributionMaps;
1971  dRedistributionMaps.resize( m_meshOutput->faces.size() );
1972 
1973  for( size_t ixSecond = 0; ixSecond < m_meshOutput->faces.size(); ixSecond++ )
1974  {
1975  dRedistributionMaps[ixSecond].Allocate( nPout * nPout, nPout * nPout );
1976 
1977  for( int i = 0; i < nPout * nPout; i++ )
1978  {
1979  dRedistributionMaps[ixSecond][i][i] = 1.0;
1980  }
1981 
1982  for( int s = 0; s < nPout * nPout; s++ )
1983  {
1984  dRedistSourceArea[s] = dGeometricOutputArea[ixSecond][s];
1985  }
1986 
1987  for( int s = 0; s < nPout * nPout; s++ )
1988  {
1989  dRedistTargetArea[s] = dataGLLJacobianOut[s / nPout][s % nPout][ixSecond];
1990  }
1991 
1992  if( !fNoConservation )
1993  {
1994  ForceIntArrayConsistencyConservation( dRedistSourceArea, dRedistTargetArea, dRedistributionMaps[ixSecond],
1995  ( nMonotoneType != 0 ) );
1996 
1997  for( int s = 0; s < nPout * nPout; s++ )
1998  {
1999  for( int t = 0; t < nPout * nPout; t++ )
2000  {
2001  dRedistributionMaps[ixSecond][s][t] *= dRedistTargetArea[s] / dRedistSourceArea[t];
2002  }
2003  }
2004  }
2005  }
2006 
2007  // Construct the total geometric area
2008  DataArray1D< double > dTotalGeometricArea( dataNodalAreaOut.GetRows() );
2009  for( size_t ixSecond = 0; ixSecond < m_meshOutput->faces.size(); ixSecond++ )
2010  {
2011  for( int s = 0; s < nPout; s++ )
2012  {
2013  for( int t = 0; t < nPout; t++ )
2014  {
2015  dTotalGeometricArea[dataGLLNodesOut[s][t][ixSecond] - 1] +=
2016  dGeometricOutputArea[ixSecond][s * nPout + t];
2017  }
2018  }
2019  }
2020 
2021  // Compose the integration operator with the output map
2022  ixOverlap = 0;
2023 
2024  if( is_root ) dbgprint.printf( 0, "Assembling map\n" );
2025 
2026  // Map from source DOFs to target DOFs with redistribution applied
2027  DataArray2D< double > dRedistributedOp( nPin * nPin, nPout * nPout );
2028 
2029  for( size_t ixFirst = 0; ixFirst < m_meshInputCov->faces.size(); ixFirst++ )
2030  {
2031 #ifdef VERBOSE
2032  // Announce computation progress
2033  if( ixFirst % outputFrequency == 0 && is_root )
2034  {
2035  dbgprint.printf( 0, "Element %zu/%lu\n", ixFirst, m_meshInputCov->faces.size() );
2036  }
2037 #endif
2038  // Number of overlapping Faces and triangles
2039  int nOverlapFaces = nAllOverlapFaces[ixFirst];
2040 
2041  if( !nOverlapFaces ) continue;
2042 
2043  // Put composed array into map
2044  for( int j = 0; j < nOverlapFaces; j++ )
2045  {
2046  int ixSecondFace = m_meshOverlap->vecTargetFaceIx[ixOverlap + j];
2047 
2048  // signal to not participate, because it is a ghost target
2049  if( ixSecondFace < 0 ) continue; // do not do anything
2050 
2051  dRedistributedOp.Zero();
2052  for( int p = 0; p < nPin * nPin; p++ )
2053  {
2054  for( int s = 0; s < nPout * nPout; s++ )
2055  {
2056  for( int t = 0; t < nPout * nPout; t++ )
2057  {
2058  dRedistributedOp[p][s] +=
2059  dRedistributionMaps[ixSecondFace][s][t] * dGlobalIntArray[p][ixOverlap + j][t];
2060  }
2061  }
2062  }
2063 
2064  int ixp = 0;
2065  for( int p = 0; p < nPin; p++ )
2066  {
2067  for( int q = 0; q < nPin; q++ )
2068  {
2069  int ixFirstNode;
2070  if( fContinuousIn )
2071  {
2072  ixFirstNode = dataGLLNodesIn[p][q][ixFirst] - 1;
2073  }
2074  else
2075  {
2076  ixFirstNode = ixFirst * nPin * nPin + p * nPin + q;
2077  }
2078 
2079  int ixs = 0;
2080  for( int s = 0; s < nPout; s++ )
2081  {
2082  for( int t = 0; t < nPout; t++ )
2083  {
2084  int ixSecondNode;
2085  if( fContinuousOut )
2086  {
2087  ixSecondNode = dataGLLNodesOut[s][t][ixSecondFace] - 1;
2088 
2089  if( !fNoConservation )
2090  {
2091  smatMap( ixSecondNode, ixFirstNode ) +=
2092  dRedistributedOp[ixp][ixs] / dataNodalAreaOut[ixSecondNode];
2093  }
2094  else
2095  {
2096  smatMap( ixSecondNode, ixFirstNode ) +=
2097  dRedistributedOp[ixp][ixs] / dTotalGeometricArea[ixSecondNode];
2098  }
2099  }
2100  else
2101  {
2102  ixSecondNode = ixSecondFace * nPout * nPout + s * nPout + t;
2103 
2104  if( !fNoConservation )
2105  {
2106  smatMap( ixSecondNode, ixFirstNode ) +=
2107  dRedistributedOp[ixp][ixs] / dataGLLJacobianOut[s][t][ixSecondFace];
2108  }
2109  else
2110  {
2111  smatMap( ixSecondNode, ixFirstNode ) +=
2112  dRedistributedOp[ixp][ixs] / dGeometricOutputArea[ixSecondFace][s * nPout + t];
2113  }
2114  }
2115 
2116  ixs++;
2117  }
2118  }
2119 
2120  ixp++;
2121  }
2122  }
2123  }
2124 
2125  // Increment the current overlap index
2126  ixOverlap += nOverlapFaces;
2127  }
2128 
2129  return;
2130 }
2131 
2132 ///////////////////////////////////////////////////////////////////////////////
2133 
2134 void moab::TempestOnlineMap::LinearRemapGLLtoGLL2_Pointwise_MOAB( const DataArray3D< int >& dataGLLNodesIn,
2135  const DataArray3D< double >& /*dataGLLJacobianIn*/,
2136  const DataArray3D< int >& dataGLLNodesOut,
2137  const DataArray3D< double >& /*dataGLLJacobianOut*/,
2138  const DataArray1D< double >& dataNodalAreaOut,
2139  int nPin,
2140  int nPout,
2141  int nMonotoneType,
2142  bool fContinuousIn,
2143  bool fContinuousOut )
2144 {
2145  // Gauss-Lobatto quadrature within Faces
2146  DataArray1D< double > dGL;
2147  DataArray1D< double > dWL;
2148 
2149  GaussLobattoQuadrature::GetPoints( nPout, 0.0, 1.0, dGL, dWL );
2150 
2151  // Get SparseMatrix represntation of the OfflineMap
2152  SparseMatrix< double >& smatMap = this->GetSparseMatrix();
2153 
2154  // Sample coefficients
2155  DataArray2D< double > dSampleCoeffIn( nPin, nPin );
2156 
2157  // Announcemnets
2158  moab::DebugOutput dbgprint( std::cout, this->rank, 0 );
2159  dbgprint.set_prefix( "[LinearRemapGLLtoGLL2_Pointwise_MOAB]: " );
2160  if( is_root )
2161  {
2162  dbgprint.printf( 0, "Finite Element to Finite Element (Pointwise) Projection\n" );
2163  dbgprint.printf( 0, "Order of the input FE polynomial interpolant: %i\n", nPin );
2164  dbgprint.printf( 0, "Order of the output FE polynomial interpolant: %i\n", nPout );
2165  }
2166 
2167  // Number of overlap Faces per source Face
2168  DataArray1D< int > nAllOverlapFaces( m_meshInputCov->faces.size() );
2169 
2170  int ixOverlap = 0;
2171 
2172  for( size_t ixFirst = 0; ixFirst < m_meshInputCov->faces.size(); ixFirst++ )
2173  {
2174  size_t ixOverlapTemp = ixOverlap;
2175  for( ; ixOverlapTemp < m_meshOverlap->faces.size(); ixOverlapTemp++ )
2176  {
2177  // const Face & faceOverlap = m_meshOverlap->faces[ixOverlapTemp];
2178 
2179  if( ixFirst - m_meshOverlap->vecSourceFaceIx[ixOverlapTemp] != 0 ) break;
2180 
2181  nAllOverlapFaces[ixFirst]++;
2182  }
2183 
2184  // Increment the current overlap index
2185  ixOverlap += nAllOverlapFaces[ixFirst];
2186  }
2187 
2188  // Number of times this point was found
2189  DataArray1D< bool > fSecondNodeFound( dataNodalAreaOut.GetRows() );
2190 
2191  ixOverlap = 0;
2192 #ifdef VERBOSE
2193  const unsigned outputFrequency = ( m_meshInputCov->faces.size() / 10 ) + 1;
2194 #endif
2195  // Loop through all faces on m_meshInputCov
2196  for( size_t ixFirst = 0; ixFirst < m_meshInputCov->faces.size(); ixFirst++ )
2197  {
2198 #ifdef VERBOSE
2199  // Announce computation progress
2200  if( ixFirst % outputFrequency == 0 && is_root )
2201  {
2202  dbgprint.printf( 0, "Element %zu/%lu\n", ixFirst, m_meshInputCov->faces.size() );
2203  }
2204 #endif
2205  // Quantities from the First Mesh
2206  const Face& faceFirst = m_meshInputCov->faces[ixFirst];
2207 
2208  const NodeVector& nodesFirst = m_meshInputCov->nodes;
2209 
2210  // Number of overlapping Faces and triangles
2211  int nOverlapFaces = nAllOverlapFaces[ixFirst];
2212 
2213  // Loop through all Overlap Faces
2214  for( int i = 0; i < nOverlapFaces; i++ )
2215  {
2216  // Quantities from the Second Mesh
2217  int ixSecond = m_meshOverlap->vecTargetFaceIx[ixOverlap + i];
2218 
2219  // signal to not participate, because it is a ghost target
2220  if( ixSecond < 0 ) continue; // do not do anything
2221 
2222  const NodeVector& nodesSecond = m_meshOutput->nodes;
2223  const Face& faceSecond = m_meshOutput->faces[ixSecond];
2224 
2225  // Loop through all nodes on the second face
2226  for( int s = 0; s < nPout; s++ )
2227  {
2228  for( int t = 0; t < nPout; t++ )
2229  {
2230  size_t ixSecondNode;
2231  if( fContinuousOut )
2232  {
2233  ixSecondNode = dataGLLNodesOut[s][t][ixSecond] - 1;
2234  }
2235  else
2236  {
2237  ixSecondNode = ixSecond * nPout * nPout + s * nPout + t;
2238  }
2239 
2240  if( ixSecondNode >= fSecondNodeFound.GetRows() ) _EXCEPTIONT( "Logic error" );
2241 
2242  // Check if this node has been found already
2243  if( fSecondNodeFound[ixSecondNode] ) continue;
2244 
2245  // Check this node
2246  Node node;
2247  Node dDx1G;
2248  Node dDx2G;
2249 
2250  ApplyLocalMap( faceSecond, nodesSecond, dGL[t], dGL[s], node, dDx1G, dDx2G );
2251 
2252  // Find the components of this quadrature point in the basis
2253  // of the first Face.
2254  double dAlphaIn;
2255  double dBetaIn;
2256 
2257  ApplyInverseMap( faceFirst, nodesFirst, node, dAlphaIn, dBetaIn );
2258 
2259  // Check if this node is within the first Face
2260  if( ( dAlphaIn < -1.0e-10 ) || ( dAlphaIn > 1.0 + 1.0e-10 ) || ( dBetaIn < -1.0e-10 ) ||
2261  ( dBetaIn > 1.0 + 1.0e-10 ) )
2262  continue;
2263 
2264  // Node is within the overlap region, mark as found
2265  fSecondNodeFound[ixSecondNode] = true;
2266 
2267  // Sample the First finite element at this point
2268  SampleGLLFiniteElement( nMonotoneType, nPin, dAlphaIn, dBetaIn, dSampleCoeffIn );
2269 
2270  // Add to map
2271  for( int p = 0; p < nPin; p++ )
2272  {
2273  for( int q = 0; q < nPin; q++ )
2274  {
2275  int ixFirstNode;
2276  if( fContinuousIn )
2277  {
2278  ixFirstNode = dataGLLNodesIn[p][q][ixFirst] - 1;
2279  }
2280  else
2281  {
2282  ixFirstNode = ixFirst * nPin * nPin + p * nPin + q;
2283  }
2284 
2285  smatMap( ixSecondNode, ixFirstNode ) += dSampleCoeffIn[p][q];
2286  }
2287  }
2288  }
2289  }
2290  }
2291 
2292  // Increment the current overlap index
2293  ixOverlap += nOverlapFaces;
2294  }
2295 
2296  // Check for missing samples
2297  for( size_t i = 0; i < fSecondNodeFound.GetRows(); i++ )
2298  {
2299  if( !fSecondNodeFound[i] )
2300  {
2301  _EXCEPTION1( "Can't sample point %i", i );
2302  }
2303  }
2304 
2305  return;
2306 }
2307 
2308 ///////////////////////////////////////////////////////////////////////////////