1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// *****************************************************************************
/*!
  \file      src/Inciter/NodeDiagnostics.cpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.,
             2022-2025 J. Bakosi
             All rights reserved. See the LICENSE file for details.
  \brief     NodeDiagnostics class for collecting nodal diagnostics
  \details   NodeDiagnostics class for collecting nodal diagnostics, e.g.,
    residuals, and various norms of errors while solving partial differential
    equations.
*/
// *****************************************************************************

#include "Diagnostics.hpp"
#include "NodeDiagnostics.hpp"
#include "DiagReducer.hpp"
#include "Discretization.hpp"
#include "Problems.hpp"<--- Include file: "Problems.hpp" not found.

namespace inciter {

static CkReduction::reducerType DiagMerger;

} // inciter::

using inciter::NodeDiagnostics;

void
NodeDiagnostics::registerReducers()
// *****************************************************************************
//  Configure Charm++ reduction types
//! \details This routine is supposed to be called from a Charm++ initnode
//!   routine. Since the runtime system executes initnode routines exactly once
//!   on every logical node early on in the Charm++ init sequence, they must be
//!   static as they are called without an object. See also: Section
//!   "Initializations at Program Startup" at in the Charm++ manual
//!   http://charm.cs.illinois.edu/manuals/html/charm++/manual.html.
// *****************************************************************************
{
  DiagMerger = CkReduction::addReducer( diagnostics::mergeDiag );
}

bool
NodeDiagnostics::rhocompute( Discretization& d,
                             const tk::Fields& u,
                             const tk::Fields& un,
                             uint64_t diag_iter ) const
// *****************************************************************************
//  Compute diagnostics for density-based solvers
//! \param[in] d Discretization proxy to read from
//! \param[in] u Current solution vector
//! \param[in] un Previous solution vector
//! \param[in] diag_iter Diagnostics output frequency
//! \return True if diagnostics have been computed
//! \details Diagnostics are defined as some norm, e.g., L2 norm, of a quantity,
//!   computed in mesh nodes, A, as ||A||_2 = sqrt[ sum_i(A_i)^2 V_i ],
//!   where the sum is taken over all mesh nodes and V_i is the nodal volume.
//!   We send multiple sets of quantities to the host for aggregation across
//!   the whole mesh. The final aggregated solution will end up in
//!   Transporter::diagnostics(). Aggregation of the partially computed
//!   diagnostics is done via potentially different policies for each field.
//! \see inciter::mergeDiag(), src/Inciter/Diagnostics.hpp
// *****************************************************************************
{
  using namespace diagnostics;

  // Only compute diagnostics if needed in this time step
  if ( (d.It()+1) % diag_iter ) return false;

  auto ncomp = u.nprop();

  // Diagnostics vector (of vectors) during aggregation. See
  // Inciter/Diagnostics.h.
  std::vector< std::vector< tk::real > >
    diag( NUMDIAG, std::vector< tk::real >( ncomp, 0.0 ) );

  const auto& v = d.V();  // nodal volumes without contributions from others

  // query function to evaluate analytic solution (if defined)
  auto sol = problems::SOL();

  // Evaluate analytic solution (if defined)
  auto an = u;
  if (sol) {
    const auto& coord = d.Coord();
    const auto& x = coord[0];
    const auto& y = coord[1];
    const auto& z = coord[2];
    for (std::size_t i=0; i<u.nunk(); ++i) {
      auto s = sol( x[i], y[i], z[i], d.T()+d.Dt(), /*meshid=*/0 );
      s[1] /= s[0];
      s[2] /= s[0];
      s[3] /= s[0];
      s[4] = s[4] / s[0] - 0.5*(s[1]*s[1] + s[2]*s[2] + s[3]*s[3]);
      for (std::size_t c=0; c<s.size(); ++c) an(i,c) = s[c];
    }
  }

  // Put in norms sweeping our mesh chunk
  for (std::size_t i=0; i<u.nunk(); ++i) {
    // Compute sum for L2 norm of the numerical solution
    for (std::size_t c=0; c<ncomp; ++c)
      diag[L2SOL][c] += u(i,c) * u(i,c) * v[i];
    // Compute sum for L2 norm of the residual
    for (std::size_t c=0; c<ncomp; ++c)
      diag[L2RES][c] += (u(i,c)-un(i,c)) * (u(i,c)-un(i,c)) * v[i];
    // Compute sum for the total energy over the entire domain (first entry)
    diag[TOTALEN][0] += u(i,4) * v[i];
    // Compute sum for L2 norm of the numerical-analytic solution
    if (sol) {
      auto nu = u[i];
      nu[1] /= nu[0];
      nu[2] /= nu[0];
      nu[3] /= nu[0];
      nu[4] = nu[4] / nu[0] - 0.5*(nu[1]*nu[1] + nu[2]*nu[2] + nu[3]*nu[3]);
      for (std::size_t c=0; c<5; ++c) {
        auto du = nu[c] - an(i,c);
        diag[L2ERR][c] += du * du * v[i];
        diag[L1ERR][c] += std::abs( du ) * v[i];
      }
      for (std::size_t c=5; c<ncomp; ++c) {
        auto du = u(i,c) - an(i,c);
        diag[L2ERR][c] += du * du * v[i];
        diag[L1ERR][c] += std::abs( du ) * v[i];
      }
    }
  }

  // Append diagnostics vector with metadata on the current time step
  // ITER:: Current iteration count (only the first entry is used)
  // TIME: Current physical time (only the first entry is used)
  // DT: Current physical time step size (only the first entry is used)
  diag[ITER][0] = static_cast< tk::real >( d.It()+1 );
  diag[TIME][0] = d.T() + d.Dt();
  diag[DT][0] = d.Dt();

  // Contribute to diagnostics
  auto stream = serialize( d.MeshId(), ncomp, diag );
  d.contribute( stream.first, stream.second.get(), DiagMerger,
    CkCallback(CkIndex_Transporter::rhodiagnostics(nullptr), d.Tr()) );

  return true;        // diagnostics have been computed
}

bool
NodeDiagnostics::precompute( Discretization& d,
                             const tk::Fields& u,
                             const tk::Fields& un,
                             const std::vector< tk::real >& p,
                             const std::vector< tk::real >& dp,
                             uint64_t diag_iter ) const
// *****************************************************************************
//  Compute diagnostics for pressure-based solvers
//! \param[in] d Discretization proxy to read from
//! \param[in] u Current solution vector
//! \param[in] un Previous solution vector
//! \param[in] p Current pressure solution
//! \param[in] dp Recent pressure solution increment
//! \param[in] diag_iter Diagnostics output frequency
//! \return True if diagnostics have been computed
//! \details Diagnostics are defined as some norm, e.g., L2 norm, of a quantity,
//!   computed in mesh nodes, A, as ||A||_2 = sqrt[ sum_i(A_i)^2 V_i ],
//!   where the sum is taken over all mesh nodes and V_i is the nodal volume.
//!   We send multiple sets of quantities to the host for aggregation across
//!   the whole mesh. The final aggregated solution will end up in
//!   Transporter::diagnostics(). Aggregation of the partially computed
//!   diagnostics is done via potentially different policies for each field.
//! \see inciter::mergeDiag(), src/Inciter/Diagnostics.hpp
// *****************************************************************************
{
  using namespace diagnostics;

  // Only compute diagnostics if needed in this time step
  if ( (d.It()+1) % diag_iter ) return false;

  Assert( p.size() == u.nunk(), "Size mismatch" );
  Assert( p.size() == dp.size(), "Size mismatch" );
  Assert( u.nunk() == un.nunk(), "Size mismatch" );

  auto ncomp = u.nprop();

  const auto& v = d.V();  // nodal volumes without contributions from others

  // query function to evaluate analytic solution (if defined)
  auto pressure_sol = problems::PRESSURE_SOL();

  // Evaluate analytic solution (if defined)
  auto ap = p;
  if (pressure_sol) {
    ncomp = 0;
    const auto& coord = d.Coord();
    const auto& x = coord[0];
    const auto& y = coord[1];
    const auto& z = coord[2];
    for (std::size_t i=0; i<p.size(); ++i) {
      ap[i] = pressure_sol( x[i], y[i], z[i], /*meshid=*/0 );
    }
  }

  // query function to evaluate analytic solution (if defined)
  auto sol = problems::SOL();

  // Evaluate analytic solution (if defined)
  auto an = u;
  if (sol) {
    const auto& coord = d.Coord();
    const auto& x = coord[0];
    const auto& y = coord[1];
    const auto& z = coord[2];
    for (std::size_t i=0; i<u.nunk(); ++i) {
      auto s = sol( x[i], y[i], z[i], d.T()+d.Dt(), /*meshid=*/0 );
      for (std::size_t c=0; c<s.size(); ++c) an(i,c) = s[c];
    }
  }

  // Diagnostics vector (of vectors) during aggregation. See
  // Inciter/Diagnostics.h.
  std::vector< std::vector< tk::real > >
    diag( NUMDIAG, std::vector< tk::real >( ncomp+1, 0.0 ) );

  // Put in norms sweeping our mesh chunk
  for (std::size_t i=0; i<u.nunk(); ++i) {
    // Compute sum for L2 norm of the numerical solution
    diag[L2SOL][0] += p[i] * p[i] * v[i];
    for (std::size_t c=0; c<ncomp; ++c) {
      diag[L2SOL][c+1] += u(i,c) * u(i,c) * v[i];
    }
    // Compute sum for L2 norm of the residual
    diag[L2RES][0] += dp[i] * dp[i] * v[i];
    for (std::size_t c=0; c<ncomp; ++c) {
      diag[L2RES][c+1] += (u(i,c)-un(i,c)) * (u(i,c)-un(i,c)) * v[i];
    }
    // Compute sum for the total energy over the entire domain
    diag[TOTALEN][0] += 0.0 * v[i];
    // Compute sum for norms of the numerical-analytic pressure solution
    if (pressure_sol) {
      auto pd = p[i] - ap[i];
      diag[L2ERR][0] += pd * pd * v[i];
      diag[L1ERR][0] += std::abs( pd ) * v[i];
    }
    // Compute sum for norms of the numerical-analytic adv/diff solution
    if (sol) {
      for (std::size_t c=0; c<ncomp; ++c) {
        auto du = u(i,c) - an(i,c);
        diag[L2ERR][c+1] += du * du * v[i];
        diag[L1ERR][c+1] += std::abs( du ) * v[i];
      }
    }
  }

  // Append diagnostics vector with metadata on the current time step
  // ITER:: Current iteration count (only the first entry is used)
  // TIME: Current physical time (only the first entry is used)
  // DT: Current physical time step size (only the first entry is used)
  diag[ITER][0] = static_cast< tk::real >( d.It() );
  diag[TIME][0] = d.T();
  diag[DT][0] = d.Dt();

  // Contribute to diagnostics
  auto stream = serialize( d.MeshId(), ncomp+1, diag );
  d.contribute( stream.first, stream.second.get(), DiagMerger,
    CkCallback(CkIndex_Transporter::prediagnostics(nullptr), d.Tr()) );

  return true;        // diagnostics have been computed
}

bool
NodeDiagnostics::accompute( Discretization& d,
                            const tk::Fields& u,
                            const tk::Fields& un,
                            uint64_t diag_iter ) const
// *****************************************************************************
//  Compute diagnostics for artificial compressibility solvers
//! \param[in] d Discretization proxy to read from
//! \param[in] u Current solution vector
//! \param[in] un Previous solution vector
//! \param[in] diag_iter Diagnostics output frequency
//! \return True if diagnostics have been computed
//! \details Diagnostics are defined as some norm, e.g., L2 norm, of a quantity,
//!   computed in mesh nodes, A, as ||A||_2 = sqrt[ sum_i(A_i)^2 V_i ],
//!   where the sum is taken over all mesh nodes and V_i is the nodal volume.
//!   We send multiple sets of quantities to the host for aggregation across
//!   the whole mesh. The final aggregated solution will end up in
//!   Transporter::diagnostics(). Aggregation of the partially computed
//!   diagnostics is done via potentially different policies for each field.
//! \see inciter::mergeDiag(), src/Inciter/Diagnostics.hpp
// *****************************************************************************
{
  using namespace diagnostics;

  // Only compute diagnostics if needed in this time step
  if ( (d.It()+1) % diag_iter ) return false;

  auto ncomp = u.nprop();

  // Diagnostics vector (of vectors) during aggregation. See
  // Inciter/Diagnostics.h.
  std::vector< std::vector< tk::real > >
    diag( NUMDIAG, std::vector< tk::real >( ncomp, 0.0 ) );

  const auto& v = d.V();  // nodal volumes without contributions from others

  // query function to evaluate analytic solution (if defined)
  auto sol = problems::SOL();

  // Evaluate analytic solution (if defined)
  auto an = u;
  if (sol) {
    const auto& coord = d.Coord();
    const auto& x = coord[0];
    const auto& y = coord[1];
    const auto& z = coord[2];
    for (std::size_t i=0; i<u.nunk(); ++i) {
      auto s = sol( x[i], y[i], z[i], d.T()+d.Dt(), /*meshid=*/0 );
      for (std::size_t c=0; c<s.size(); ++c) an(i,c) = s[c];
    }
  }

  // Put in norms sweeping our mesh chunk
  for (std::size_t i=0; i<u.nunk(); ++i) {
    // Compute sum for L2 norm of the numerical solution
    for (std::size_t c=0; c<ncomp; ++c)
      diag[L2SOL][c] += u(i,c) * u(i,c) * v[i];
    // Compute sum for L2 norm of the residual
    for (std::size_t c=0; c<ncomp; ++c)
      diag[L2RES][c] += (u(i,c)-un(i,c)) * (u(i,c)-un(i,c)) * v[i];
    // Compute sum for the total energy over the entire domain
    diag[TOTALEN][0] += 0.0 * v[i];
    // Compute sum for norms of the numerical-analytic adv/diff solution
    if (sol) {
      for (std::size_t c=1; c<ncomp; ++c) {
        auto du = u(i,c) - an(i,c);
        diag[L2ERR][c] += du * du * v[i];
        diag[L1ERR][c] += std::abs( du ) * v[i];
      }
    }
  }

  // Append diagnostics vector with metadata on the current time step
  // ITER:: Current iteration count (only the first entry is used)
  // TIME: Current physical time (only the first entry is used)
  // DT: Current physical time step size (only the first entry is used)
  diag[ITER][0] = static_cast< tk::real >( d.It() );
  diag[TIME][0] = d.T();
  diag[DT][0] = d.Dt();

  // Contribute to diagnostics
  auto stream = serialize( d.MeshId(), ncomp, diag );
  d.contribute( stream.first, stream.second.get(), DiagMerger,
    CkCallback(CkIndex_Transporter::acdiagnostics(nullptr), d.Tr()) );

  return true;        // diagnostics have been computed
}