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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
// *****************************************************************************
/*!
  \file      src/PDE/MultiMat/MiscMultiMatFns.hpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.
             All rights reserved. See the LICENSE file for details.
  \brief     Misc multi-material system functions
  \details   This file defines functions that required for multi-material
    compressible hydrodynamics.
*/
// *****************************************************************************

#include <iostream>

#include "MiscMultiMatFns.hpp"
#include "Inciter/InputDeck/InputDeck.hpp"
#include "Integrate/Basis.hpp"
#include "MultiMat/MultiMatIndexing.hpp"
#include "EoS/GetMatProp.hpp"

namespace inciter {

extern ctr::InputDeck g_inputdeck;

void initializeMaterialEoS( std::vector< EOS >& mat_blk )
// *****************************************************************************
//  Initialize the material block with configured EOS
//! \param[in,out] mat_blk Material block that gets initialized
// *****************************************************************************
{
  // EoS initialization
  // ---------------------------------------------------------------------------
  auto nmat = g_inputdeck.get< tag::multimat, tag::nmat >();
  const auto& matprop = g_inputdeck.get< tag::material >();
  const auto& matidxmap = g_inputdeck.get< tag::matidxmap >();
  for (std::size_t k=0; k<nmat; ++k) {
    auto mateos = matprop[matidxmap.get< tag::eosidx >()[k]].get<tag::eos>();
    mat_blk.emplace_back(mateos, EqType::multimat, k);
  }

  // set rho0 for all materials
  // ---------------------------------------------------------------------------
  std::vector< tk::real > rho0mat( nmat, 0.0 );
  const auto& ic = g_inputdeck.get< tag::ic >();
  const auto& icbox = ic.get< tag::box >();
  const auto& icmbk = ic.get< tag::meshblock >();
  // Get background properties
  std::size_t k = ic.get< tag::materialid >() - 1;
  tk::real pr = ic.get< tag::pressure >();
  tk::real tmp = ic.get< tag::temperature >();
  rho0mat[k] = mat_blk[k].compute< EOS::density >(pr, tmp);

  // Check inside used defined box
  if (!icbox.empty())
  {
    for (const auto& b : icbox) {   // for all boxes
      k = b.get< tag::materialid >() - 1;
      auto boxmas = b.get< tag::mass >();
      // if mass and volume are given, compute density as mass/volume
      if (boxmas > 0.0) {
        std::vector< tk::real > box
          { b.get< tag::xmin >(), b.get< tag::xmax >(),
            b.get< tag::ymin >(), b.get< tag::ymax >(),
            b.get< tag::zmin >(), b.get< tag::zmax >() };
        auto V_ex = (box[1]-box[0]) * (box[3]-box[2]) * (box[5]-box[4]);
        rho0mat[k] = boxmas / V_ex;
      }
      // else compute density from eos
      else {
        pr = b.get< tag::pressure >();
        tmp = b.get< tag::temperature >();
        rho0mat[k] = mat_blk[k].compute< EOS::density >(pr, tmp);
      }
    }
  }

  // Check inside user-specified mesh blocks
  if (!icmbk.empty())
  {
    for (const auto& b : icmbk) { // for all blocks
      k = b.get< tag::materialid >() - 1;
      auto boxmas = b.get< tag::mass >();
      // if mass and volume are given, compute density as mass/volume
      if (boxmas > 0.0) {
        auto V_ex = b.get< tag::volume >();
        rho0mat[k] = boxmas / V_ex;
      }
      // else compute density from eos
      else {
        pr = b.get< tag::pressure >();
        tmp = b.get< tag::temperature >();
        rho0mat[k] = mat_blk[k].compute< EOS::density >(pr, tmp);
      }
    }
  }

  // Store computed rho0's in the EOS-block
  for (std::size_t i=0; i<nmat; ++i) {
    mat_blk[i].set< EOS::setRho0 >(rho0mat[i]);
  }
}

bool
cleanTraceMultiMat(
  tk::real t,
  std::size_t nelem,
  const std::vector< EOS >& mat_blk,
  const tk::Fields& geoElem,
  std::size_t nmat,
  tk::Fields& U,
  tk::Fields& P )
// *****************************************************************************
//  Clean up the state of trace materials for multi-material PDE system
//! \param[in] t Physical time
//! \param[in] nelem Number of elements
//! \param[in] mat_blk EOS material block
//! \param[in] geoElem Element geometry array
//! \param[in] nmat Number of materials in this PDE system
//! \param[in/out] U High-order solution vector which gets modified
//! \param[in/out] P High-order vector of primitives which gets modified
//! \return Boolean indicating if an unphysical material state was found
// *****************************************************************************
{
  const auto ndof = g_inputdeck.get< tag::ndof >();
  const auto rdof = g_inputdeck.get< tag::rdof >();
  const auto& solidx = g_inputdeck.get< tag::matidxmap, tag::solidx >();
  std::size_t ncomp = U.nprop()/rdof;
  auto neg_density = false;

  std::vector< tk::real > ugp(ncomp, 0.0);

  for (std::size_t e=0; e<nelem; ++e)
  {
    // find material in largest quantity.
    auto almax(0.0);
    std::size_t kmax = 0;
    for (std::size_t k=0; k<nmat; ++k)
    {
      auto al = U(e, volfracDofIdx(nmat, k, rdof, 0));
      if (al > almax)
      {
        almax = al;
        kmax = k;
      }
    }

    // get conserved quantities
    std::vector< tk::real > B(rdof, 0.0);
    B[0] = 1.0;
    ugp = eval_state(ncomp, rdof, ndof, e, U, B);

    auto u = P(e, velocityDofIdx(nmat, 0, rdof, 0));
    auto v = P(e, velocityDofIdx(nmat, 1, rdof, 0));
    auto w = P(e, velocityDofIdx(nmat, 2, rdof, 0));
    auto pmax = P(e, pressureDofIdx(nmat, kmax, rdof, 0))/almax;
    auto gmax = getDeformGrad(nmat, kmax, ugp);
    auto tmax = mat_blk[kmax].compute< EOS::temperature >(
      U(e, densityDofIdx(nmat, kmax, rdof, 0)), u, v, w,
      U(e, energyDofIdx(nmat, kmax, rdof, 0)), almax, gmax );

    tk::real p_target(0.0), d_al(0.0), d_arE(0.0);
    //// get equilibrium pressure
    //std::vector< tk::real > kmat(nmat, 0.0);
    //tk::real ratio(0.0);
    //for (std::size_t k=0; k<nmat; ++k)
    //{
    //  auto arhok = U(e, densityDofIdx(nmat,k,rdof,0));
    //  auto alk = U(e, volfracDofIdx(nmat,k,rdof,0));
    //  auto apk = P(e, pressureDofIdx(nmat,k,rdof,0)3);
    //  auto ak = mat_blk[k].compute< EOS::soundspeed >(arhok, apk, alk, k);
    //  kmat[k] = arhok * ak * ak;

    //  p_target += alk * apk / kmat[k];
    //  ratio += alk * alk / kmat[k];
    //}
    //p_target /= ratio;
    //p_target = std::max(p_target, 1e-14);
    p_target = std::max(pmax, 1e-14);

    // 1. Correct minority materials and store volume/energy changes
    for (std::size_t k=0; k<nmat; ++k)
    {
      auto alk = U(e, volfracDofIdx(nmat, k, rdof, 0));
      auto pk = P(e, pressureDofIdx(nmat, k, rdof, 0)) / alk;
      // for positive volume fractions
      if (solidx[k] == 0 && solidx[kmax] == 0 && matExists(alk))
      {
        // check if volume fraction is lesser than threshold (volfracPRelaxLim)
        // and if the material (effective) pressure is negative. If either of
        // these conditions is true, perform pressure relaxation.
        if ((alk < volfracPRelaxLim()) ||
          (pk < mat_blk[k].compute< EOS::min_eff_pressure >(1e-12,
          U(e, densityDofIdx(nmat, k, rdof, 0)), alk))
          /*&& (std::fabs((pk-pmax)/pmax) > 1e-08)*/)
        {
          // determine target relaxation pressure
          auto prelax = mat_blk[k].compute< EOS::min_eff_pressure >(1e-10,
            U(e, densityDofIdx(nmat, k, rdof, 0)), alk);
          prelax = std::max(prelax, p_target);

          // energy change
          auto rhomat = U(e, densityDofIdx(nmat, k, rdof, 0))
            / alk;
          auto gmat = getDeformGrad(nmat, k, ugp);
          auto rhoEmat = mat_blk[k].compute< EOS::totalenergy >(rhomat, u, v, w,
            prelax, gmat);

          // total energy flux into majority material
          d_arE += (U(e, energyDofIdx(nmat, k, rdof, 0))
            - alk * rhoEmat);

          // update state of trace material
          U(e, volfracDofIdx(nmat, k, rdof, 0)) = alk;
          U(e, energyDofIdx(nmat, k, rdof, 0)) = alk*rhoEmat;
          P(e, pressureDofIdx(nmat, k, rdof, 0)) = alk*prelax;
        }
      }
      // check for unbounded volume fractions
      else if (alk < 0.0 || !std::isfinite(alk))
      {
        auto rhok = mat_blk[k].compute< EOS::density >(p_target,
          std::max(1e-8,tmax));
        if (std::isfinite(alk)) d_al += (alk - 1e-14);
        // update state of trace material
        U(e, volfracDofIdx(nmat, k, rdof, 0)) = 1e-14;
        U(e, densityDofIdx(nmat, k, rdof, 0)) = 1e-14 * rhok;
        auto gk = std::array< std::array< tk::real, 3 >, 3 >
          {{ {{1, 0, 0}},
             {{0, 1, 0}},
             {{0, 0, 1}} }};
        U(e, energyDofIdx(nmat, k, rdof, 0)) = 1e-14
          * mat_blk[k].compute< EOS::totalenergy >(rhok, u, v, w, p_target,
          gk);
        P(e, pressureDofIdx(nmat, k, rdof, 0)) = 1e-14 *
          p_target;
        resetSolidTensors(nmat, k, e, U, P);
        for (std::size_t i=1; i<rdof; ++i) {
          U(e, volfracDofIdx(nmat, k, rdof, i)) = 0.0;
          U(e, densityDofIdx(nmat, k, rdof, i)) = 0.0;
          U(e, energyDofIdx(nmat, k, rdof, i)) = 0.0;
          P(e, pressureDofIdx(nmat, k, rdof, i)) = 0.0;
        }
      }
      else if (!matExists(alk)) {  // condition so that else-branch not exec'ed for solids
        // determine target relaxation pressure
        auto prelax = mat_blk[k].compute< EOS::min_eff_pressure >(1e-10,
          U(e, densityDofIdx(nmat, k, rdof, 0)), alk);
        prelax = std::max(prelax, p_target);
        auto rhok = U(e, densityDofIdx(nmat, k, rdof, 0)) / alk;
        auto gk = std::array< std::array< tk::real, 3 >, 3 >
          {{ {{1, 0, 0}},
             {{0, 1, 0}},
             {{0, 0, 1}} }};
        // update state of trace material
        U(e, energyDofIdx(nmat, k, rdof, 0)) = alk
          * mat_blk[k].compute< EOS::totalenergy >( rhok, u, v, w, prelax, gk );
        P(e, pressureDofIdx(nmat, k, rdof, 0)) = alk *
          prelax;
        resetSolidTensors(nmat, k, e, U, P);
        for (std::size_t i=1; i<rdof; ++i) {
          U(e, energyDofIdx(nmat, k, rdof, i)) = 0.0;
          P(e, pressureDofIdx(nmat, k, rdof, i)) = 0.0;
        }
      }
    }

    U(e, volfracDofIdx(nmat, kmax, rdof, 0)) += d_al;

    // 2. Flux energy change into majority material
    U(e, energyDofIdx(nmat, kmax, rdof, 0)) += d_arE;
    P(e, pressureDofIdx(nmat, kmax, rdof, 0)) =
      mat_blk[kmax].compute< EOS::pressure >(
      U(e, densityDofIdx(nmat, kmax, rdof, 0)), u, v, w,
      U(e, energyDofIdx(nmat, kmax, rdof, 0)),
      U(e, volfracDofIdx(nmat, kmax, rdof, 0)), kmax, gmax );

    // 3. enforce unit sum of volume fractions
    auto alsum = 0.0;
    for (std::size_t k=0; k<nmat; ++k)
      alsum += U(e, volfracDofIdx(nmat, k, rdof, 0));

    for (std::size_t k=0; k<nmat; ++k) {
      U(e, volfracDofIdx(nmat, k, rdof, 0)) /= alsum;
      U(e, densityDofIdx(nmat, k, rdof, 0)) /= alsum;
      U(e, energyDofIdx(nmat, k, rdof, 0)) /= alsum;
      P(e, pressureDofIdx(nmat, k, rdof, 0)) /= alsum;
      if (solidx[k] > 0) {
        for (std::size_t i=0; i<6; ++i)
          P(e, stressDofIdx(nmat, solidx[k], i, rdof, 0)) /= alsum;
      }
    }

    pmax = P(e, pressureDofIdx(nmat, kmax, rdof, 0)) /
      U(e, volfracDofIdx(nmat, kmax, rdof, 0));

    // check for unphysical state
    for (std::size_t k=0; k<nmat; ++k)
    {
      auto alpha = U(e, volfracDofIdx(nmat, k, rdof, 0));
      auto arho = U(e, densityDofIdx(nmat, k, rdof, 0));
      auto apr = P(e, pressureDofIdx(nmat, k, rdof, 0));

      // lambda for screen outputs
      auto screenout = [&]()
      {
        std::cout << "Physical time:     " << t << std::endl;
        std::cout << "Element centroid:  " << geoElem(e,1) << ", "
          << geoElem(e,2) << ", " << geoElem(e,3) << std::endl;
        std::cout << "Material-id:       " << k << std::endl;
        std::cout << "Volume-fraction:   " << alpha << std::endl;
        std::cout << "Partial density:   " << arho << std::endl;
        std::cout << "Partial pressure:  " << apr << std::endl;
        std::cout << "Major pressure:    " << pmax << " (mat " << kmax << ")"
          << std::endl;
        std::cout << "Major temperature: " << tmax << " (mat " << kmax << ")"
          << std::endl;
        std::cout << "Velocity:          " << u << ", " << v << ", " << w
          << std::endl;
      };

      if (arho < 0.0)
      {
        neg_density = true;
        screenout();
      }
    }
  }
  return neg_density;
}

tk::real
timeStepSizeMultiMat(
  const std::vector< EOS >& mat_blk,
  const std::vector< int >& esuf,
  const tk::Fields& geoFace,
  const tk::Fields& geoElem,
  const std::size_t nelem,
  std::size_t nmat,
  const tk::Fields& U,
  const tk::Fields& P )
// *****************************************************************************
//  Time step restriction for multi material cell-centered schemes
//! \param[in] mat_blk EOS material block
//! \param[in] esuf Elements surrounding elements array
//! \param[in] geoFace Face geometry array
//! \param[in] geoElem Element geometry array
//! \param[in] nelem Number of elements
//! \param[in] nmat Number of materials in this PDE system
//! \param[in] U High-order solution vector
//! \param[in] P High-order vector of primitives
//! \return Maximum allowable time step based on cfl criterion
// *****************************************************************************
{
  const auto ndof = g_inputdeck.get< tag::ndof >();
  const auto rdof = g_inputdeck.get< tag::rdof >();
  std::size_t ncomp = U.nprop()/rdof;
  std::size_t nprim = P.nprop()/rdof;

  tk::real u, v, w, a, vn, dSV_l, dSV_r;
  std::vector< tk::real > delt(U.nunk(), 0.0);
  std::vector< tk::real > ugp(ncomp, 0.0), pgp(nprim, 0.0);<--- Variable 'ugp' is assigned a value that is never used.<--- Variable 'pgp' is assigned a value that is never used.

  // compute maximum characteristic speed at all internal element faces
  for (std::size_t f=0; f<esuf.size()/2; ++f)
  {
    std::size_t el = static_cast< std::size_t >(esuf[2*f]);
    auto er = esuf[2*f+1];

    std::array< tk::real, 3 > fn;
    fn[0] = geoFace(f,1);
    fn[1] = geoFace(f,2);
    fn[2] = geoFace(f,3);

    // left element

    // Compute the basis function for the left element
    std::vector< tk::real > B_l(rdof, 0.0);
    B_l[0] = 1.0;

    // get conserved quantities
    ugp = eval_state(ncomp, rdof, ndof, el, U, B_l);
    // get primitive quantities
    pgp = eval_state(nprim, rdof, ndof, el, P, B_l);

    // advection velocity
    u = pgp[velocityIdx(nmat, 0)];
    v = pgp[velocityIdx(nmat, 1)];
    w = pgp[velocityIdx(nmat, 2)];

    vn = u*geoFace(f,1) + v*geoFace(f,2) + w*geoFace(f,3);

    // acoustic speed
    a = 0.0;
    for (std::size_t k=0; k<nmat; ++k)
    {
      if (ugp[volfracIdx(nmat, k)] > 1.0e-04) {
        auto gk = getDeformGrad(nmat, k, ugp);
        gk = tk::rotateTensor(gk, fn);
        a = std::max( a, mat_blk[k].compute< EOS::soundspeed >(
          ugp[densityIdx(nmat, k)],
          pgp[pressureIdx(nmat, k)], ugp[volfracIdx(nmat, k)], k, gk ) );
      }
    }

    dSV_l = geoFace(f,0) * (std::fabs(vn) + a);

    // right element
    if (er > -1) {
      std::size_t eR = static_cast< std::size_t >( er );

      // Compute the basis function for the right element
      std::vector< tk::real > B_r(rdof, 0.0);
      B_r[0] = 1.0;

      // get conserved quantities
      ugp = eval_state( ncomp, rdof, ndof, eR, U, B_r);
      // get primitive quantities
      pgp = eval_state( nprim, rdof, ndof, eR, P, B_r);

      // advection velocity
      u = pgp[velocityIdx(nmat, 0)];
      v = pgp[velocityIdx(nmat, 1)];
      w = pgp[velocityIdx(nmat, 2)];

      vn = u*geoFace(f,1) + v*geoFace(f,2) + w*geoFace(f,3);

      // acoustic speed
      a = 0.0;
      for (std::size_t k=0; k<nmat; ++k)
      {
        if (ugp[volfracIdx(nmat, k)] > 1.0e-04) {
          auto gk = getDeformGrad(nmat, k, ugp);
          gk = tk::rotateTensor(gk, fn);
          a = std::max( a, mat_blk[k].compute< EOS::soundspeed >(
            ugp[densityIdx(nmat, k)],
            pgp[pressureIdx(nmat, k)], ugp[volfracIdx(nmat, k)], k, gk ) );
        }
      }

      dSV_r = geoFace(f,0) * (std::fabs(vn) + a);

      delt[eR] += std::max( dSV_l, dSV_r );
    } else {
      dSV_r = dSV_l;
    }

    delt[el] += std::max( dSV_l, dSV_r );
  }

  tk::real mindt = std::numeric_limits< tk::real >::max();

  // compute allowable dt
  for (std::size_t e=0; e<nelem; ++e)
  {
    mindt = std::min( mindt, geoElem(e,0)/delt[e] );
  }

  return mindt;
}

tk::real
timeStepSizeMultiMatFV(
  const std::vector< EOS >& mat_blk,
  const tk::Fields& geoElem,
  std::size_t nelem,
  std::size_t nmat,
  const tk::Fields& U,
  const tk::Fields& P,
  std::vector< tk::real >& local_dte )
// *****************************************************************************
//  Time step restriction for multi material cell-centered FV scheme
//! \param[in] mat_blk Material EOS block
//! \param[in] geoElem Element geometry array
//! \param[in] nelem Number of elements
//! \param[in] nmat Number of materials in this PDE system
//! \param[in] U High-order solution vector
//! \param[in] P High-order vector of primitives
//! \param[in,out] local_dte Time step size for each element (for local
//!   time stepping)
//! \return Maximum allowable time step based on cfl criterion
// *****************************************************************************
{
  const auto ndof = g_inputdeck.get< tag::ndof >();
  const auto rdof = g_inputdeck.get< tag::rdof >();
  const auto use_mass_avg =
    g_inputdeck.get< tag::multimat, tag::dt_sos_massavg >();
  std::size_t ncomp = U.nprop()/rdof;
  std::size_t nprim = P.nprop()/rdof;

  std::vector< tk::real > ugp(ncomp, 0.0), pgp(nprim, 0.0);<--- Variable 'ugp' is assigned a value that is never used.<--- Variable 'pgp' is assigned a value that is never used.
  tk::real mindt = std::numeric_limits< tk::real >::max();

  // loop over all elements
  for (std::size_t e=0; e<nelem; ++e)
  {
    // basis function at centroid
    std::vector< tk::real > B(rdof, 0.0);
    B[0] = 1.0;

    // get conserved quantities
    ugp = eval_state(ncomp, rdof, ndof, e, U, B);
    // get primitive quantities
    pgp = eval_state(nprim, rdof, ndof, e, P, B);

    // magnitude of advection velocity
    auto u = pgp[velocityIdx(nmat, 0)];
    auto v = pgp[velocityIdx(nmat, 1)];
    auto w = pgp[velocityIdx(nmat, 2)];
    auto vmag = std::sqrt(tk::dot({u, v, w}, {u, v, w}));

    // acoustic speed
    tk::real a = 0.0;
    tk::real mixtureDensity = 0.0;
    for (std::size_t k=0; k<nmat; ++k)
    {
      if (use_mass_avg > 0)
      {
        // mass averaging SoS
        a += ugp[densityIdx(nmat,k)]*mat_blk[k].compute< EOS::soundspeed >(
          ugp[densityIdx(nmat, k)], pgp[pressureIdx(nmat, k)],
          ugp[volfracIdx(nmat, k)], k );

        mixtureDensity += ugp[densityIdx(nmat,k)];
      }
      else
      {
        if (ugp[volfracIdx(nmat, k)] > 1.0e-04)
        {
          a = std::max( a, mat_blk[k].compute< EOS::soundspeed >(
            ugp[densityIdx(nmat, k)], pgp[pressureIdx(nmat, k)],
            ugp[volfracIdx(nmat, k)], k ) );
        }
      }
    }
    if (use_mass_avg > 0) a /= mixtureDensity;

    // characteristic wave speed
    auto v_char = vmag + a;

    // characteristic length (radius of insphere)
    auto dx = std::min(std::cbrt(geoElem(e,0)), geoElem(e,4))
      /std::sqrt(24.0);

    // element dt
    local_dte[e] = dx/v_char;
    mindt = std::min(mindt, local_dte[e]);
  }

  return mindt;
}

tk::real
timeStepSizeViscousFV(
  const tk::Fields& geoElem,
  std::size_t nelem,
  std::size_t nmat,
  const tk::Fields& U )
// *****************************************************************************
//  Compute the time step size restriction based on this physics
//! \param[in] geoElem Element geometry array
//! \param[in] nelem Number of elements
//! \param[in] nmat Number of materials
//! \param[in] U Solution vector
//! \return Maximum allowable time step based on viscosity
// *****************************************************************************
{
  const auto& ndof = g_inputdeck.get< tag::ndof >();
  const auto& rdof = g_inputdeck.get< tag::rdof >();
  std::size_t ncomp = U.nprop()/rdof;

  auto mindt = std::numeric_limits< tk::real >::max();

  for (std::size_t e=0; e<nelem; ++e)
  {
    // get conserved quantities at centroid
    std::vector< tk::real > B(rdof, 0.0);
    B[0] = 1.0;
    auto ugp = eval_state(ncomp, rdof, ndof, e, U, B);

    // Kinematic viscosity
    tk::real nu(0.0);
    for (std::size_t k=0; k<nmat; ++k)
    {
      auto alk = ugp[volfracIdx(nmat, k)];
      auto mu = getmatprop< tag::mu >(k);
      if (alk > 1.0e-04) nu = std::max(nu, alk*mu/ugp[densityIdx(nmat,k)]);
    }

    // characteristic length (radius of insphere)
    auto dx = std::min(std::cbrt(geoElem(e,0)), geoElem(e,4))
      /std::sqrt(24.0);

    // element dt
    mindt = std::min(mindt, dx*dx/nu);
  }

  return mindt;
}

void
resetSolidTensors(
  std::size_t nmat,
  std::size_t k,
  std::size_t e,
  tk::Fields& U,<--- Parameter 'U' can be declared with const
  tk::Fields& P )<--- Parameter 'P' can be declared with const
// *****************************************************************************
//  Reset the solid tensors
//! \param[in] nmat Number of materials in this PDE system
//! \param[in] k Material id whose deformation gradient is required
//! \param[in] e Id of element whose solution is to be limited
//! \param[in/out] U High-order solution vector which gets modified
//! \param[in/out] P High-order vector of primitives which gets modified
// *****************************************************************************
{
  const auto& solidx = g_inputdeck.get< tag::matidxmap, tag::solidx >();
  const auto rdof = g_inputdeck.get< tag::rdof >();

  if (solidx[k] > 0) {
    for (std::size_t i=0; i<3; ++i) {
      for (std::size_t j=0; j<3; ++j) {
        // deformation gradient reset
        if (i==j) U(e, deformDofIdx(nmat, solidx[k], i, j, rdof, 0)) = 1.0;
        else U(e, deformDofIdx(nmat, solidx[k], i, j, rdof, 0)) = 0.0;

        // elastic Cauchy-stress reset
        P(e, stressDofIdx(nmat, solidx[k], stressCmp[i][j], rdof, 0)) = 0.0;

        // high-order reset
        for (std::size_t l=1; l<rdof; ++l) {
          U(e, deformDofIdx(nmat, solidx[k], i, j, rdof, l)) = 0.0;
          P(e, stressDofIdx(nmat, solidx[k], stressCmp[i][j], rdof, l)) = 0.0;
        }
      }
    }
  }
}

std::array< std::array< tk::real, 3 >, 3 >
getDeformGrad(
  std::size_t nmat,
  std::size_t k,
  const std::vector< tk::real >& state )
// *****************************************************************************
//  Get the inverse deformation gradient tensor for a material at given location
//! \param[in] nmat Number of materials in this PDE system
//! \param[in] k Material id whose deformation gradient is required
//! \param[in] state State vector at location
//! \return Inverse deformation gradient tensor (g_k) of material k
// *****************************************************************************
{
  const auto& solidx = g_inputdeck.get< tag::matidxmap, tag::solidx >();
  std::array< std::array< tk::real, 3 >, 3 > gk;

  if (solidx[k] > 0) {
    // deformation gradient for solids
    for (std::size_t i=0; i<3; ++i) {
      for (std::size_t j=0; j<3; ++j)
        gk[i][j] = state[deformIdx(nmat,solidx[k],i,j)];
    }
  }
  else {
    // empty vector for fluids
    gk = {{}};
  }

  return gk;
}

std::array< std::array< tk::real, 3 >, 3 >
getCauchyStress(
  std::size_t nmat,
  std::size_t k,
  std::size_t ncomp,
  const std::vector< tk::real >& state )
// *****************************************************************************
//  Get the elastic Cauchy stress tensor for a material at given location
//! \param[in] nmat Number of materials in this PDE system
//! \param[in] k Material id whose deformation gradient is required
//! \param[in] ncomp Number of components in the PDE system
//! \param[in] state State vector at location
//! \return Elastic Cauchy stress tensor (alpha * \sigma_ij) of material k
// *****************************************************************************
{
  const auto& solidx = g_inputdeck.get< tag::matidxmap, tag::solidx >();
  std::array< std::array< tk::real, 3 >, 3 >
    asigk{{ {{0,0,0}}, {{0,0,0}}, {{0,0,0}} }};

  // elastic Cauchy stress for solids
  if (solidx[k] > 0) {
    for (std::size_t i=0; i<3; ++i) {
      for (std::size_t j=0; j<3; ++j)
        asigk[i][j] = state[ncomp +
          stressIdx(nmat, solidx[k], stressCmp[i][j])];
    }
  }

  return asigk;
}

bool
haveSolid(
  std::size_t nmat,
  const std::vector< std::size_t >& solidx )
// *****************************************************************************
//  Check whether we have solid materials in our problem
//! \param[in] nmat Number of materials in this PDE system
//! \param[in] solidx Material index indicator
//! \return true if we have at least one solid, false otherwise.
// *****************************************************************************
{
  bool haveSolid = false;
  for (std::size_t k=0; k<nmat; ++k)
    if (solidx[k] > 0) haveSolid = true;

  return haveSolid;
}

std::size_t numSolids(
  std::size_t nmat,
  const std::vector< std::size_t >& solidx )
// *****************************************************************************
//  Count total number of solid materials in the problem
//! \param[in] nmat Number of materials in this PDE system
//! \param[in] solidx Material index indicator
//! \return Total number of solid materials in the problem
// *****************************************************************************
{
  // count number of solid materials
  std::size_t nsld(0);
  for (std::size_t k=0; k<nmat; ++k)
    if (solidx[k] > 0) ++nsld;

  return nsld;
}

} //inciter::