Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/Inciter/DG.cpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.
7 : : All rights reserved. See the LICENSE file for details.
8 : : \brief DG advances a system of PDEs with the discontinuous Galerkin scheme
9 : : \details DG advances a system of partial differential equations (PDEs) using
10 : : discontinuous Galerkin (DG) finite element (FE) spatial discretization (on
11 : : tetrahedron elements) combined with Runge-Kutta (RK) time stepping.
12 : : \see The documentation in DG.h.
13 : : */
14 : : // *****************************************************************************
15 : :
16 : : #include <algorithm>
17 : : #include <numeric>
18 : : #include <sstream>
19 : :
20 : : #include "DG.hpp"
21 : : #include "Discretization.hpp"
22 : : #include "DGPDE.hpp"
23 : : #include "DiagReducer.hpp"
24 : : #include "DerivedData.hpp"
25 : : #include "ElemDiagnostics.hpp"
26 : : #include "Inciter/InputDeck/InputDeck.hpp"
27 : : #include "Refiner.hpp"
28 : : #include "Limiter.hpp"
29 : : #include "Reorder.hpp"
30 : : #include "Vector.hpp"
31 : : #include "Around.hpp"
32 : : #include "Integrate/Basis.hpp"
33 : : #include "FieldOutput.hpp"
34 : : #include "ChareStateCollector.hpp"
35 : : #include "PDE/MultiMat/MultiMatIndexing.hpp"
36 : :
37 : : namespace inciter {
38 : :
39 : : extern ctr::InputDeck g_inputdeck;
40 : : extern std::vector< DGPDE > g_dgpde;
41 : :
42 : : //! Runge-Kutta coefficients
43 : : static const std::array< std::array< tk::real, 3 >, 2 >
44 : : rkcoef{{ {{ 0.0, 3.0/4.0, 1.0/3.0 }}, {{ 1.0, 1.0/4.0, 2.0/3.0 }} }};
45 : :
46 : : //! Implicit-Explicit Runge-Kutta Coefficients
47 : : static const tk::real rk_gamma = (2.0-std::sqrt(2.0))/2.0;
48 : : static const tk::real rk_delta = -2.0*std::sqrt(2.0)/3.0;
49 : : static const tk::real c2 =
50 : : (27.0 + std::pow(2187.0-1458.0*std::sqrt(2.0),1.0/3.0)
51 : : + 9.0*std::pow(3.0+2.0*std::sqrt(2.0),1.0/3.0))/54.0;
52 : : static const tk::real c3 = c2/(6.0*std::pow(c2,2.0)-3.0*c2+1.0);
53 : : static const tk::real b2 = (3.0*c2-1.0)/(6.0*std::pow(c2,2.0));
54 : : static const tk::real b3 =
55 : : (6.0*std::pow(c2,2.0)-3.0*c2+1.0)/(6.0*std::pow(c2,2.0));
56 : : static const tk::real a22_impl = c2;
57 : : static const tk::real a21_expl = c2;
58 : : static const tk::real a32_expl = c3;
59 : : static const tk::real a33_impl =
60 : : (1.0/6.0-b2*std::pow(c2,2.0)-b3*c2*c3)/(b3*(c3-c2));
61 : : static const tk::real a32_impl = a33_impl-c3;
62 : : static const std::array< std::array< tk::real, 3 >, 2 >
63 : : expl_rkcoef{{ {{ 0.0, 0.0, b2 }},
64 : : {{ a21_expl, a32_expl, b3 }} }};
65 : : static const std::array< std::array< tk::real, 3 >, 2>
66 : : impl_rkcoef{{ {{ 0.0, a32_impl, b2 }},
67 : : {{ a22_impl, a33_impl, b3}} }};
68 : :
69 : : } // inciter::
70 : :
71 : : extern tk::CProxy_ChareStateCollector stateProxy;
72 : :
73 : : using inciter::DG;
74 : :
75 : 889 : DG::DG( const CProxy_Discretization& disc,
76 : : const CProxy_Ghosts& ghostsproxy,
77 : : const std::map< int, std::vector< std::size_t > >& bface,
78 : : const std::map< int, std::vector< std::size_t > >& /* bnode */,
79 : 889 : const std::vector< std::size_t >& triinpoel ) :
80 : : m_disc( disc ),
81 : : m_ghosts( ghostsproxy ),
82 : : m_ndof_NodalExtrm( 3 ), // for the first order derivatives in 3 directions
83 : : m_nsol( 0 ),
84 : : m_ninitsol( 0 ),
85 : : m_nlim( 0 ),
86 : : m_nnod( 0 ),
87 : : m_nrefine( 0 ),
88 : : m_nsmooth( 0 ),
89 : : m_nreco( 0 ),
90 : : m_nnodalExtrema( 0 ),
91 [ + - ]: 889 : m_nstiffeq( g_dgpde[Disc()->MeshId()].nstiffeq() ),
92 [ + - ]: 889 : m_nnonstiffeq( g_dgpde[Disc()->MeshId()].nnonstiffeq() ),
93 [ + - ]: 889 : m_u( Disc()->Inpoel().size()/4,
94 : 889 : g_inputdeck.get< tag::rdof >()*
95 : 889 : g_inputdeck.get< tag::ncomp >() ),
96 : : m_un( m_u.nunk(), m_u.nprop() ),
97 : 889 : m_p( m_u.nunk(), g_inputdeck.get< tag::rdof >()*
98 [ + - ][ + - ]: 889 : g_dgpde[Disc()->MeshId()].nprim() ),
99 : : m_lhs( m_u.nunk(),
100 : 889 : g_inputdeck.get< tag::ndof >()*
101 : 889 : g_inputdeck.get< tag::ncomp >() ),
102 : : m_rhs( m_u.nunk(), m_lhs.nprop() ),
103 : : m_rhsprev( m_u.nunk(), m_lhs.nprop() ),
104 : 889 : m_stiffrhs( m_u.nunk(), g_inputdeck.get< tag::ndof >()*
105 [ + - ][ + - ]: 889 : g_dgpde[Disc()->MeshId()].nstiffeq() ),
106 : 889 : m_stiffrhsprev( m_u.nunk(), g_inputdeck.get< tag::ndof >()*
107 [ + - ][ + - ]: 889 : g_dgpde[Disc()->MeshId()].nstiffeq() ),
108 [ + - ]: 889 : m_stiffEqIdx( g_dgpde[Disc()->MeshId()].nstiffeq() ),
109 [ + - ]: 889 : m_nonStiffEqIdx( g_dgpde[Disc()->MeshId()].nnonstiffeq() ),
110 : : m_mtInv(
111 : : tk::invMassMatTaylorRefEl(g_inputdeck.get< tag::rdof >()) ),
112 : : m_uNodalExtrm(),
113 : : m_pNodalExtrm(),
114 : : m_uNodalExtrmc(),
115 : : m_pNodalExtrmc(),
116 [ + - ]: 889 : m_npoin( Disc()->Coord()[0].size() ),
117 : : m_diag(),
118 : : m_stage( 0 ),
119 : : m_ndof(),
120 : : m_interface(),
121 : : m_numEqDof(),
122 : : m_uc(),
123 : : m_pc(),
124 : : m_ndofc(),
125 : : m_interfacec(),
126 : : m_initial( 1 ),
127 : : m_uElemfields( m_u.nunk(),
128 : : g_inputdeck.get< tag::ncomp >() ),
129 : : m_pElemfields( m_u.nunk(),
130 : 889 : m_p.nprop() / g_inputdeck.get< tag::rdof >() ),
131 : : m_uNodefields( m_npoin,
132 : : g_inputdeck.get< tag::ncomp >() ),
133 : : m_pNodefields( m_npoin,
134 : 889 : m_p.nprop() / g_inputdeck.get< tag::rdof >() ),
135 : : m_uNodefieldsc(),
136 : : m_pNodefieldsc(),
137 : : m_outmesh(),
138 : : m_boxelems(),
139 [ + - ][ + - ]: 9779 : m_shockmarker(m_u.nunk(), 1)
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
140 : : // *****************************************************************************
141 : : // Constructor
142 : : //! \param[in] disc Discretization proxy
143 : : //! \param[in] bface Boundary-faces mapped to side set ids
144 : : //! \param[in] triinpoel Boundary-face connectivity
145 : : // *****************************************************************************
146 : : {
147 [ + + ]: 889 : if (g_inputdeck.get< tag::cmd, tag::chare >() ||
148 [ + + ]: 851 : g_inputdeck.get< tag::cmd, tag::quiescence >())
149 [ + - ][ + - ]: 1088 : stateProxy.ckLocalBranch()->insert( "DG", thisIndex, CkMyPe(), Disc()->It(),
[ + - ][ + - ]
[ - + ]
150 : : "DG" );
151 : :
152 : : // assign number of dofs for each equation in all pde systems
153 [ + - ][ + - ]: 889 : g_dgpde[Disc()->MeshId()].numEquationDofs(m_numEqDof);
154 : :
155 : : // Allocate storage for the vector of nodal extrema
156 [ + - ][ + - ]: 889 : m_uNodalExtrm.resize( Disc()->Bid().size(),
157 : 0 : std::vector<tk::real>( 2 * m_ndof_NodalExtrm *
158 [ + - ]: 889 : g_inputdeck.get< tag::ncomp >() ) );
159 [ + - ][ + - ]: 889 : m_pNodalExtrm.resize( Disc()->Bid().size(),
160 : 0 : std::vector<tk::real>( 2 * m_ndof_NodalExtrm *
161 [ + - ]: 889 : m_p.nprop() / g_inputdeck.get< tag::rdof >() ) );
162 : :
163 : : // Initialization for the buffer vector of nodal extrema
164 [ + - ]: 889 : resizeNodalExtremac();
165 : :
166 : 889 : usesAtSync = true; // enable migration at AtSync
167 : :
168 : : // Enable SDAG wait for initially building the solution vector and limiting
169 [ + - ]: 889 : if (m_initial) {
170 [ + - ][ + - ]: 889 : thisProxy[ thisIndex ].wait4sol();
171 [ + - ][ + - ]: 889 : thisProxy[ thisIndex ].wait4refine();
172 [ + - ][ + - ]: 889 : thisProxy[ thisIndex ].wait4smooth();
173 [ + - ][ + - ]: 889 : thisProxy[ thisIndex ].wait4lim();
174 [ + - ][ + - ]: 889 : thisProxy[ thisIndex ].wait4nod();
175 [ + - ][ + - ]: 889 : thisProxy[ thisIndex ].wait4reco();
176 [ + - ][ + - ]: 1778 : thisProxy[ thisIndex ].wait4nodalExtrema();
177 : : }
178 : :
179 [ + - ][ + - ]: 1778 : m_ghosts[thisIndex].insert(m_disc, bface, triinpoel, m_u.nunk(),
180 [ + - ][ + - ]: 2667 : CkCallback(CkIndex_DG::resizeSolVectors(), thisProxy[thisIndex]));
[ - + ][ - + ]
[ - - ][ - - ]
181 : :
182 : : // global-sync to call doneInserting on m_ghosts
183 [ + - ]: 889 : auto meshid = Disc()->MeshId();
184 [ + - ]: 889 : contribute( sizeof(std::size_t), &meshid, CkReduction::nop,
185 [ + - ][ - - ]: 889 : CkCallback(CkReductionTarget(Transporter,doneInsertingGhosts),
186 [ + - ]: 889 : Disc()->Tr()) );
187 : 889 : }
188 : :
189 : : void
190 : 578 : DG::registerReducers()
191 : : // *****************************************************************************
192 : : // Configure Charm++ reduction types
193 : : //! \details Since this is a [initnode] routine, the runtime system executes the
194 : : //! routine exactly once on every logical node early on in the Charm++ init
195 : : //! sequence. Must be static as it is called without an object. See also:
196 : : //! Section "Initializations at Program Startup" at in the Charm++ manual
197 : : //! http://charm.cs.illinois.edu/manuals/html/charm++/manual.html.
198 : : // *****************************************************************************
199 : : {
200 : 578 : ElemDiagnostics::registerReducers();
201 : 578 : }
202 : :
203 : : void
204 : 19926 : DG::ResumeFromSync()
205 : : // *****************************************************************************
206 : : // Return from migration
207 : : //! \details This is called when load balancing (LB) completes. The presence of
208 : : //! this function does not affect whether or not we block on LB.
209 : : // *****************************************************************************
210 : : {
211 [ - + ][ - - ]: 19926 : if (Disc()->It() == 0) Throw( "it = 0 in ResumeFromSync()" );
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ]
212 : :
213 [ + - ]: 19926 : if (!g_inputdeck.get< tag::cmd, tag::nonblocking >()) next();
214 : 19926 : }
215 : :
216 : : void
217 : 889 : DG::resizeSolVectors()
218 : : // *****************************************************************************
219 : : // Resize solution vectors after extension due to Ghosts and continue with setup
220 : : // *****************************************************************************
221 : : {
222 : : // Resize solution vectors, lhs and rhs by the number of ghost tets
223 : 889 : m_u.resize( myGhosts()->m_nunk );
224 : 889 : m_un.resize( myGhosts()->m_nunk );
225 : 889 : m_p.resize( myGhosts()->m_nunk );
226 : 889 : m_lhs.resize( myGhosts()->m_nunk );
227 : 889 : m_rhs.resize( myGhosts()->m_nunk );
228 : 889 : m_rhsprev.resize( myGhosts()->m_nunk );
229 : 889 : m_stiffrhs.resize( myGhosts()->m_nunk );
230 : 889 : m_stiffrhsprev.resize( myGhosts()->m_nunk );
231 : :
232 : : // Size communication buffer for solution and number of degrees of freedom
233 [ + + ]: 3556 : for (auto& n : m_ndofc) n.resize( myGhosts()->m_bid.size() );
234 [ + + ]: 3556 : for (auto& u : m_uc) u.resize( myGhosts()->m_bid.size() );
235 [ + + ]: 3556 : for (auto& p : m_pc) p.resize( myGhosts()->m_bid.size() );
236 [ + + ]: 1778 : for (auto& i : m_interfacec) i.resize( myGhosts()->m_bid.size() );
237 : :
238 : : // Initialize number of degrees of freedom in mesh elements
239 : 889 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
240 [ + + ]: 889 : if( pref )
241 : : {
242 : 134 : const auto ndofmax = g_inputdeck.get< tag::pref, tag::ndofmax >();
243 : 134 : m_ndof.resize( myGhosts()->m_nunk, ndofmax );
244 : : }
245 : : else
246 : : {
247 : 755 : const auto ndof = g_inputdeck.get< tag::ndof >();
248 : 755 : m_ndof.resize( myGhosts()->m_nunk, ndof );
249 : : }
250 : 889 : m_interface.resize( myGhosts()->m_nunk, 0 );
251 : :
252 : : // Ensure that we also have all the geometry and connectivity data
253 : : // (including those of ghosts)
254 : : Assert( myGhosts()->m_geoElem.nunk() == m_u.nunk(),
255 : : "GeoElem unknowns size mismatch" );
256 : :
257 : : // Signal the runtime system that all workers have received their adjacency
258 : 889 : std::vector< std::size_t > meshdata{ myGhosts()->m_initial, Disc()->MeshId() };
259 : 889 : contribute( meshdata, CkReduction::sum_ulong,
260 [ + - ][ + - ]: 2667 : CkCallback(CkReductionTarget(Transporter,comfinal), Disc()->Tr()) );
[ + - ][ - - ]
261 : 889 : }
262 : :
263 : : void
264 : 889 : DG::setup()
265 : : // *****************************************************************************
266 : : // Set initial conditions, generate lhs, output mesh
267 : : // *****************************************************************************
268 : : {
269 [ + + ]: 889 : if (g_inputdeck.get< tag::cmd, tag::chare >() ||
270 [ + + ]: 851 : g_inputdeck.get< tag::cmd, tag::quiescence >())
271 [ + - ][ + - ]: 1088 : stateProxy.ckLocalBranch()->insert( "DG", thisIndex, CkMyPe(), Disc()->It(),
[ + - ][ + - ]
[ - + ]
272 : : "setup" );
273 : :
274 : 889 : auto d = Disc();
275 : :
276 : : // Basic error checking on sizes of element geometry data and connectivity
277 : : Assert( myGhosts()->m_geoElem.nunk() == m_lhs.nunk(),
278 : : "Size mismatch in DG::setup()" );
279 : :
280 : : // Compute left-hand side of discrete PDEs
281 : 889 : lhs();
282 : :
283 : : // Determine elements inside user-defined IC box
284 : 889 : g_dgpde[d->MeshId()].IcBoxElems( myGhosts()->m_geoElem,
285 : 889 : myGhosts()->m_fd.Esuel().size()/4, m_boxelems );
286 : :
287 : : // Compute volume of user-defined box IC
288 [ + - ][ - + ]: 889 : d->boxvol( {}, {}, 0 ); // punt for now
289 : :
290 : : // Query time history field output labels from all PDEs integrated
291 : : const auto& hist_points = g_inputdeck.get< tag::history_output, tag::point >();
292 [ - + ]: 889 : if (!hist_points.empty()) {
293 : 0 : std::vector< std::string > histnames;
294 [ - - ]: 0 : auto n = g_dgpde[d->MeshId()].histNames();
295 [ - - ]: 0 : histnames.insert( end(histnames), begin(n), end(n) );
296 [ - - ]: 0 : d->histheader( std::move(histnames) );
297 : : }
298 : :
299 : : // If working with IMEX-RK, Store stiff equations into m_stiffEqIdx
300 [ - + ]: 889 : if (g_inputdeck.get< tag::imex_runge_kutta >())
301 : : {
302 : 0 : g_dgpde[Disc()->MeshId()].setStiffEqIdx(m_stiffEqIdx);
303 : 0 : g_dgpde[Disc()->MeshId()].setNonStiffEqIdx(m_nonStiffEqIdx);
304 : : }
305 : 889 : }
306 : :
307 : : void
308 : 889 : DG::box( tk::real v, const std::vector< tk::real >& )
309 : : // *****************************************************************************
310 : : // Receive total box IC volume and set conditions in box
311 : : //! \param[in] v Total volume within user-specified box
312 : : // *****************************************************************************
313 : : {
314 : 889 : auto d = Disc();
315 : :
316 : : // Store user-defined box IC volume
317 : 889 : d->Boxvol() = v;
318 : :
319 : : // Set initial conditions for all PDEs
320 : 1778 : g_dgpde[d->MeshId()].initialize( m_lhs, myGhosts()->m_inpoel,
321 : 889 : myGhosts()->m_coord, m_boxelems, d->ElemBlockId(), m_u, d->T(),
322 : 889 : myGhosts()->m_fd.Esuel().size()/4 );
323 : 889 : g_dgpde[d->MeshId()].updatePrimitives( m_u, m_lhs, myGhosts()->m_geoElem, m_p,
324 : 889 : myGhosts()->m_fd.Esuel().size()/4, m_ndof );
325 : :
326 : : m_un = m_u;
327 : :
328 : : // Output initial conditions to file (regardless of whether it was requested)
329 [ + - ][ + - ]: 2667 : startFieldOutput( CkCallback(CkIndex_DG::start(), thisProxy[thisIndex]) );
[ - + ][ - - ]
330 : 889 : }
331 : :
332 : : void
333 : 889 : DG::start()
334 : : // *****************************************************************************
335 : : // Start time stepping
336 : : // *****************************************************************************
337 : : {
338 : : // Free memory storing output mesh
339 : 889 : m_outmesh.destroy();
340 : :
341 : : // Start timer measuring time stepping wall clock time
342 : 889 : Disc()->Timer().zero();
343 : : // Zero grind-timer
344 : 889 : Disc()->grindZero();
345 : : // Start time stepping by computing the size of the next time step)
346 : 889 : next();
347 : 889 : }
348 : :
349 : : void
350 : 24824 : DG::startFieldOutput( CkCallback c )
351 : : // *****************************************************************************
352 : : // Start preparing fields for output to file
353 : : //! \param[in] c Function to continue with after the write
354 : : // *****************************************************************************
355 : : {
356 : : // No field output in benchmark mode or if field output frequency not hit
357 [ + + ][ + + ]: 24824 : if (g_inputdeck.get< tag::cmd, tag::benchmark >() || !fieldOutput()) {
358 : :
359 : 22626 : c.send();
360 : :
361 : : } else {
362 : :
363 : : // Optionally refine mesh for field output
364 : 2198 : auto d = Disc();
365 : :
366 [ + + ]: 2198 : if (refinedOutput()) {
367 : :
368 : 33 : const auto& tr = tk::remap( myGhosts()->m_fd.Triinpoel(), d->Gid() );
369 [ + - ][ + - ]: 66 : d->Ref()->outref( myGhosts()->m_fd.Bface(), {}, tr, c );
[ + - ][ + - ]
[ - - ]
370 : :
371 : : } else {
372 : :
373 : : // cut off ghosts from mesh connectivity and coordinates
374 : 2165 : const auto& tr = tk::remap( myGhosts()->m_fd.Triinpoel(), d->Gid() );
375 [ + - ][ + - ]: 4330 : extractFieldOutput( {}, d->Chunk(), d->Coord(), {}, {},
[ + + ][ - - ]
376 [ + - ]: 2165 : d->NodeCommMap(), myGhosts()->m_fd.Bface(), {}, tr, c );
377 : :
378 : : }
379 : :
380 : : }
381 : 24824 : }
382 : :
383 : : void
384 : 71805 : DG::next()
385 : : // *****************************************************************************
386 : : // Advance equations to next time step
387 : : // *****************************************************************************
388 : : {
389 : 71805 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
390 : :
391 : 71805 : auto d = Disc();
392 : :
393 [ + + ][ + + ]: 71805 : if (pref && m_stage == 0 && d->T() > 0)
[ + + ]
394 : 3272 : g_dgpde[d->MeshId()].eval_ndof( myGhosts()->m_nunk, myGhosts()->m_coord,
395 : 1636 : myGhosts()->m_inpoel,
396 : 1636 : myGhosts()->m_fd, m_u, m_p,
397 : : g_inputdeck.get< tag::pref, tag::indicator >(),
398 : : g_inputdeck.get< tag::ndof >(),
399 : : g_inputdeck.get< tag::pref, tag::ndofmax >(),
400 : : g_inputdeck.get< tag::pref, tag::tolref >(),
401 : 1636 : m_ndof );
402 : :
403 : : // communicate solution ghost data (if any)
404 [ + + ]: 71805 : if (myGhosts()->m_sendGhost.empty())
405 : 3930 : comsol_complete();
406 : : else
407 [ + + ]: 704160 : for(const auto& [cid, ghostdata] : myGhosts()->m_sendGhost) {
408 [ + - ]: 568410 : std::vector< std::size_t > tetid( ghostdata.size() );
409 [ + - ][ + - ]: 1136820 : std::vector< std::vector< tk::real > > u( ghostdata.size() ),
[ - - ]
410 [ + - ][ + - ]: 1136820 : prim( ghostdata.size() );
411 [ + - ][ + - ]: 568410 : std::vector< std::size_t > interface( ghostdata.size() );
412 [ + - ][ - - ]: 568410 : std::vector< std::size_t > ndof( ghostdata.size() );
413 : : std::size_t j = 0;
414 [ + + ]: 10896600 : for(const auto& i : ghostdata) {
415 : : Assert( i < myGhosts()->m_fd.Esuel().size()/4,
416 : : "Sending solution ghost data" );
417 [ + - ]: 10328190 : tetid[j] = i;
418 [ + - ][ - + ]: 10328190 : u[j] = m_u[i];
419 [ + - ][ - + ]: 10328190 : prim[j] = m_p[i];
420 [ + + ][ + + ]: 10328190 : if (pref && m_stage == 0) {
421 : 395110 : ndof[j] = m_ndof[i];
422 : 395110 : interface[j] = m_interface[i];
423 : : }
424 : 10328190 : ++j;
425 : : }
426 [ + - ][ + - ]: 1136820 : thisProxy[ cid ].comsol( thisIndex, m_stage, tetid, u, prim, interface, ndof );
[ + - ][ - - ]
427 : : }
428 : :
429 : 71805 : ownsol_complete();
430 : 71805 : }
431 : :
432 : : void
433 : 568410 : DG::comsol( int fromch,
434 : : std::size_t fromstage,
435 : : const std::vector< std::size_t >& tetid,
436 : : const std::vector< std::vector< tk::real > >& u,
437 : : const std::vector< std::vector< tk::real > >& prim,
438 : : const std::vector< std::size_t >& interface,
439 : : const std::vector< std::size_t >& ndof )
440 : : // *****************************************************************************
441 : : // Receive chare-boundary solution ghost data from neighboring chares
442 : : //! \param[in] fromch Sender chare id
443 : : //! \param[in] fromstage Sender chare time step stage
444 : : //! \param[in] tetid Ghost tet ids we receive solution data for
445 : : //! \param[in] u Solution ghost data
446 : : //! \param[in] prim Primitive variables in ghost cells
447 : : //! \param[in] interface Interface marker in ghost cells
448 : : //! \param[in] ndof Number of degrees of freedom for chare-boundary elements
449 : : //! \details This function receives contributions to the unlimited solution
450 : : //! from fellow chares.
451 : : // *****************************************************************************
452 : : {
453 : : Assert( u.size() == tetid.size(), "Size mismatch in DG::comsol()" );
454 : : Assert( prim.size() == tetid.size(), "Size mismatch in DG::comsol()" );
455 : :
456 : 568410 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
457 : :
458 : 568410 : if (pref && fromstage == 0) {
459 : : Assert( ndof.size() == tetid.size(), "Size mismatch in DG::comsol()" );
460 : : Assert( interface.size() == tetid.size(), "Size mismatch in DG::comsol()" );
461 : : }
462 : :
463 : : // Find local-to-ghost tet id map for sender chare
464 : 568410 : const auto& n = tk::cref_find( myGhosts()->m_ghost, fromch );
465 : :
466 [ + + ]: 10896600 : for (std::size_t i=0; i<tetid.size(); ++i) {
467 : 10328190 : auto j = tk::cref_find( n, tetid[i] );
468 : : Assert( j >= myGhosts()->m_fd.Esuel().size()/4,
469 : : "Receiving solution non-ghost data" );
470 : 10328190 : auto b = tk::cref_find( myGhosts()->m_bid, j );
471 : : Assert( b < m_uc[0].size(), "Indexing out of bounds" );
472 : : Assert( b < m_pc[0].size(), "Indexing out of bounds" );
473 : 10328190 : m_uc[0][b] = u[i];
474 : 10328190 : m_pc[0][b] = prim[i];
475 [ + + ]: 10328190 : if (pref && fromstage == 0) {
476 : : Assert( b < m_ndofc[0].size(), "Indexing out of bounds" );
477 : 395110 : m_ndofc[0][b] = ndof[i];
478 : : Assert( b < m_interfacec[0].size(), "Indexing out of bounds" );
479 : 395110 : m_interfacec[0][b] = interface[i];
480 : : }
481 : : }
482 : :
483 : : // if we have received all solution ghost contributions from neighboring
484 : : // chares (chares we communicate along chare-boundary faces with), and
485 : : // contributed our solution to these neighbors, proceed to reconstructions
486 [ + + ]: 568410 : if (++m_nsol == myGhosts()->m_sendGhost.size()) {
487 : 67875 : m_nsol = 0;
488 : 67875 : comsol_complete();
489 : : }
490 : 568410 : }
491 : :
492 : : void
493 : 2198 : DG::extractFieldOutput(
494 : : const std::vector< std::size_t >& /*ginpoel*/,
495 : : const tk::UnsMesh::Chunk& chunk,
496 : : const tk::UnsMesh::Coords& coord,
497 : : const std::unordered_map< std::size_t, tk::UnsMesh::Edge >& /*addedNodes*/,
498 : : const std::unordered_map< std::size_t, std::size_t >& addedTets,
499 : : const tk::NodeCommMap& nodeCommMap,
500 : : const std::map< int, std::vector< std::size_t > >& bface,
501 : : const std::map< int, std::vector< std::size_t > >& /* bnode */,
502 : : const std::vector< std::size_t >& triinpoel,
503 : : CkCallback c )
504 : : // *****************************************************************************
505 : : // Extract field output going to file
506 : : //! \param[in] chunk Field-output mesh chunk (connectivity and global<->local
507 : : //! id maps)
508 : : //! \param[in] coord Field-output mesh node coordinates
509 : : //! \param[in] addedTets Field-output mesh cells and their parents (local ids)
510 : : //! \param[in] nodeCommMap Field-output mesh node communication map
511 : : //! \param[in] bface Field-output meshndary-faces mapped to side set ids
512 : : //! \param[in] triinpoel Field-output mesh boundary-face connectivity
513 : : //! \param[in] c Function to continue with after the write
514 : : // *****************************************************************************
515 : : {
516 : : m_outmesh.chunk = chunk;
517 : : m_outmesh.coord = coord;
518 : 2198 : m_outmesh.triinpoel = triinpoel;
519 : : m_outmesh.bface = bface;
520 : : m_outmesh.nodeCommMap = nodeCommMap;
521 : :
522 : : const auto& inpoel = std::get< 0 >( chunk );
523 : :
524 : : // Evaluate element solution on incoming mesh
525 : 2198 : evalSolution( *Disc(), inpoel, coord, addedTets, m_ndof, m_u, m_p,
526 : 2198 : m_uElemfields, m_pElemfields, m_uNodefields, m_pNodefields );
527 : :
528 : : // Send node fields contributions to neighbor chares
529 [ + + ]: 2198 : if (nodeCommMap.empty())
530 : 174 : comnodeout_complete();
531 : : else {
532 : : const auto& lid = std::get< 2 >( chunk );
533 : 4048 : auto esup = tk::genEsup( inpoel, 4 );
534 [ + + ]: 17386 : for(const auto& [ch,nodes] : nodeCommMap) {
535 : : // Pack node field data in chare boundary nodes
536 : : std::vector< std::vector< tk::real > >
537 [ + - ][ + - ]: 46086 : lu( m_uNodefields.nprop(), std::vector< tk::real >( nodes.size() ) );
[ + - ]
538 : : std::vector< std::vector< tk::real > >
539 [ + - ][ + - ]: 46086 : lp( m_pNodefields.nprop(), std::vector< tk::real >( nodes.size() ) );
540 [ + + ]: 78560 : for (std::size_t f=0; f<m_uNodefields.nprop(); ++f) {
541 : : std::size_t j = 0;
542 [ + + ]: 635548 : for (auto g : nodes)
543 : 572350 : lu[f][j++] = m_uNodefields(tk::cref_find(lid,g),f);
544 : : }
545 [ + + ]: 25630 : for (std::size_t f=0; f<m_pNodefields.nprop(); ++f) {
546 : : std::size_t j = 0;
547 [ + + ]: 106908 : for (auto g : nodes)
548 : 96640 : lp[f][j++] = m_pNodefields(tk::cref_find(lid,g),f);
549 : : }
550 : : // Pack (partial) number of elements surrounding chare boundary nodes
551 [ + - ]: 15362 : std::vector< std::size_t > nesup( nodes.size() );
552 : : std::size_t j = 0;
553 [ + + ]: 160790 : for (auto g : nodes) {
554 : 145428 : auto i = tk::cref_find( lid, g );
555 : 145428 : nesup[j++] = esup.second[i+1] - esup.second[i];
556 : : }
557 [ + - ][ + - ]: 46086 : thisProxy[ch].comnodeout(
[ + - ][ - - ]
558 [ + - ][ - + ]: 30724 : std::vector<std::size_t>(begin(nodes),end(nodes)), nesup, lu, lp );
559 : : }
560 : : }
561 : :
562 [ + - ]: 2198 : ownnod_complete( c, addedTets );
563 : 2198 : }
564 : :
565 : : void
566 : 889 : DG::lhs()
567 : : // *****************************************************************************
568 : : // Compute left-hand side of discrete transport equations
569 : : // *****************************************************************************
570 : : {
571 : 889 : g_dgpde[Disc()->MeshId()].lhs( myGhosts()->m_geoElem, m_lhs );
572 : :
573 [ - + ]: 889 : if (!m_initial) stage();
574 : 889 : }
575 : :
576 : 71805 : void DG::refine()
577 : : // *****************************************************************************
578 : : // Add the protective layer for ndof refinement
579 : : // *****************************************************************************
580 : : {
581 : 71805 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
582 : :
583 : : // Combine own and communicated contributions of unreconstructed solution and
584 : : // degrees of freedom in cells (if p-adaptive)
585 [ + + ]: 10471800 : for (const auto& b : myGhosts()->m_bid) {
586 : : Assert( m_uc[0][b.second].size() == m_u.nprop(), "ncomp size mismatch" );
587 : : Assert( m_pc[0][b.second].size() == m_p.nprop(), "ncomp size mismatch" );
588 [ + + ]: 180763245 : for (std::size_t c=0; c<m_u.nprop(); ++c) {
589 : 170435055 : m_u(b.first,c) = m_uc[0][b.second][c];
590 : : }
591 [ + + ]: 36390615 : for (std::size_t c=0; c<m_p.nprop(); ++c) {
592 : 26062425 : m_p(b.first,c) = m_pc[0][b.second][c];
593 : : }
594 [ + + ][ + + ]: 10328190 : if (pref && m_stage == 0) {
595 : 395110 : m_ndof[ b.first ] = m_ndofc[0][ b.second ];
596 : 395110 : m_interface[ b.first ] = m_interfacec[0][ b.second ];
597 : : }
598 : : }
599 : :
600 [ + + ][ + + ]: 71805 : if (pref && m_stage==0) refine_ndof();
601 : :
602 [ + + ]: 71805 : if (myGhosts()->m_sendGhost.empty())
603 : 3930 : comrefine_complete();
604 : : else
605 [ + + ]: 704160 : for(const auto& [cid, ghostdata] : myGhosts()->m_sendGhost) {
606 [ + - ]: 568410 : std::vector< std::size_t > tetid( ghostdata.size() );
607 [ + - ][ + - ]: 1136820 : std::vector< std::vector< tk::real > > u( ghostdata.size() ),
[ - - ]
608 [ + - ][ + - ]: 1136820 : prim( ghostdata.size() );
609 [ + - ]: 568410 : std::vector< std::size_t > ndof( ghostdata.size() );
610 : : std::size_t j = 0;
611 [ + + ]: 10896600 : for(const auto& i : ghostdata) {
612 : : Assert( i < myGhosts()->m_fd.Esuel().size()/4, "Sending refined ndof "
613 : : "data" );
614 [ + + ]: 10328190 : tetid[j] = i;
615 [ + + ][ + + ]: 10328190 : if (pref && m_stage == 0) ndof[j] = m_ndof[i];
616 : 10328190 : ++j;
617 : : }
618 [ + - ][ + - ]: 1136820 : thisProxy[ cid ].comrefine( thisIndex, tetid, ndof );
[ + - ][ - - ]
619 : : }
620 : :
621 : 71805 : ownrefine_complete();
622 : 71805 : }
623 : :
624 : : void
625 : 568410 : DG::comrefine( int fromch,
626 : : const std::vector< std::size_t >& tetid,
627 : : const std::vector< std::size_t >& ndof )
628 : : // *****************************************************************************
629 : : // Receive chare-boundary ghost data from neighboring chares
630 : : //! \param[in] fromch Sender chare id
631 : : //! \param[in] tetid Ghost tet ids we receive solution data for
632 : : //! \param[in] ndof Number of degrees of freedom for chare-boundary elements
633 : : //! \details This function receives contributions to the refined ndof data
634 : : //! from fellow chares.
635 : : // *****************************************************************************
636 : : {
637 : 568410 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
638 : :
639 : : if (pref && m_stage == 0)
640 : : Assert( ndof.size() == tetid.size(), "Size mismatch in DG::comrefine()" );
641 : :
642 : : // Find local-to-ghost tet id map for sender chare
643 : 568410 : const auto& n = tk::cref_find( myGhosts()->m_ghost, fromch );
644 : :
645 [ + + ]: 10896600 : for (std::size_t i=0; i<tetid.size(); ++i) {
646 : 10328190 : auto j = tk::cref_find( n, tetid[i] );
647 : : Assert( j >= myGhosts()->m_fd.Esuel().size()/4,
648 : : "Receiving solution non-ghost data" );
649 : 10328190 : auto b = tk::cref_find( myGhosts()->m_bid, j );
650 [ + + ][ + + ]: 10328190 : if (pref && m_stage == 0) {
651 : : Assert( b < m_ndofc[1].size(), "Indexing out of bounds" );
652 : 395110 : m_ndofc[1][b] = ndof[i];
653 : : }
654 : : }
655 : :
656 : : // if we have received all solution ghost contributions from neighboring
657 : : // chares (chares we communicate along chare-boundary faces with), and
658 : : // contributed our solution to these neighbors, proceed to limiting
659 [ + + ]: 568410 : if (++m_nrefine == myGhosts()->m_sendGhost.size()) {
660 : 67875 : m_nrefine = 0;
661 : 67875 : comrefine_complete();
662 : : }
663 : 568410 : }
664 : :
665 : : void
666 : 71805 : DG::smooth()
667 : : // *****************************************************************************
668 : : // Smooth the refined ndof distribution
669 : : // *****************************************************************************
670 : : {
671 : 71805 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
672 : :
673 [ + + ]: 10471800 : for (const auto& b : myGhosts()->m_bid) {
674 [ + + ][ + + ]: 10328190 : if (pref && m_stage == 0)
675 : 395110 : m_ndof[ b.first ] = m_ndofc[1][ b.second ];
676 : : }
677 : :
678 [ + + ][ + + ]: 71805 : if (pref && m_stage==0) smooth_ndof();
679 : :
680 [ + + ]: 71805 : if (myGhosts()->m_sendGhost.empty())
681 : 3930 : comsmooth_complete();
682 : : else
683 [ + + ]: 704160 : for(const auto& [cid, ghostdata] : myGhosts()->m_sendGhost) {
684 : 568410 : std::vector< std::size_t > tetid( ghostdata.size() );
685 : : std::vector< std::size_t > ndof;
686 : : std::size_t j = 0;
687 [ + + ]: 10896600 : for(const auto& i : ghostdata) {
688 : : Assert( i < myGhosts()->m_fd.Esuel().size()/4, "Sending ndof data" );
689 [ + + ]: 10328190 : tetid[j] = i;
690 [ + + ][ + + ]: 10328190 : if (pref && m_stage == 0) ndof.push_back( m_ndof[i] );
[ + - ]
691 : 10328190 : ++j;
692 : : }
693 [ + - ][ + - ]: 1136820 : thisProxy[ cid ].comsmooth( thisIndex, tetid, ndof );
[ + + ][ - - ]
694 : : }
695 : :
696 : 71805 : ownsmooth_complete();
697 : 71805 : }
698 : :
699 : : void
700 : 568410 : DG::comsmooth( int fromch,
701 : : const std::vector< std::size_t >& tetid,
702 : : const std::vector< std::size_t >& ndof )
703 : : // *****************************************************************************
704 : : // Receive chare-boundary ghost data from neighboring chares
705 : : //! \param[in] fromch Sender chare id
706 : : //! \param[in] tetid Ghost tet ids we receive solution data for
707 : : //! \param[in] ndof Number of degrees of freedom for chare-boundary elements
708 : : //! \details This function receives contributions to the smoothed ndof data
709 : : //! from fellow chares.
710 : : // *****************************************************************************
711 : : {
712 : 568410 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
713 : :
714 : : if (pref && m_stage == 0)
715 : : Assert( ndof.size() == tetid.size(), "Size mismatch in DG::comsmooth()" );
716 : :
717 : 568410 : const auto& n = tk::cref_find( myGhosts()->m_ghost, fromch );
718 : :
719 [ + + ]: 10896600 : for (std::size_t i=0; i<tetid.size(); ++i) {
720 : 10328190 : auto j = tk::cref_find( n, tetid[i] );
721 : : Assert( j >= myGhosts()->m_fd.Esuel().size()/4, "Receiving ndof data" );
722 : 10328190 : auto b = tk::cref_find( myGhosts()->m_bid, j );
723 [ + + ][ + + ]: 10328190 : if (pref && m_stage == 0) {
724 : : Assert( b < m_ndofc[2].size(), "Indexing out of bounds" );
725 : 395110 : m_ndofc[2][b] = ndof[i];
726 : : }
727 : : }
728 : :
729 [ + + ]: 568410 : if (++m_nsmooth == myGhosts()->m_sendGhost.size()) {
730 : 67875 : m_nsmooth = 0;
731 : 67875 : comsmooth_complete();
732 : : }
733 : 568410 : }
734 : :
735 : : void
736 : 71805 : DG::reco()
737 : : // *****************************************************************************
738 : : // Compute reconstructions
739 : : // *****************************************************************************
740 : : {
741 : 71805 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
742 : 71805 : const auto rdof = g_inputdeck.get< tag::rdof >();
743 : :
744 : : // Combine own and communicated contributions of unreconstructed solution and
745 : : // degrees of freedom in cells (if p-adaptive)
746 [ + + ]: 10471800 : for (const auto& b : myGhosts()->m_bid) {
747 [ + + ][ + + ]: 10328190 : if (pref && m_stage == 0) {
748 : 395110 : m_ndof[ b.first ] = m_ndofc[2][ b.second ];
749 : : }
750 : : }
751 : :
752 : 71805 : auto d = Disc();
753 [ + + ][ + + ]: 71805 : if (pref && m_stage==0) {
754 : 1770 : g_dgpde[d->MeshId()].resetAdapSol( myGhosts()->m_fd, m_u, m_p, m_ndof );
755 : : }
756 : :
757 [ + + ]: 71805 : if (rdof > 1)
758 : : // Reconstruct second-order solution and primitive quantities
759 : 87930 : g_dgpde[d->MeshId()].reconstruct( d->T(), myGhosts()->m_geoFace,
760 : 43965 : myGhosts()->m_geoElem,
761 : 43965 : myGhosts()->m_fd, myGhosts()->m_esup, myGhosts()->m_inpoel,
762 : 43965 : myGhosts()->m_coord, m_u, m_p, pref, m_ndof );
763 : :
764 : : // Send reconstructed solution to neighboring chares
765 [ + + ]: 71805 : if (myGhosts()->m_sendGhost.empty())
766 : 3930 : comreco_complete();
767 : : else
768 [ + + ]: 704160 : for(const auto& [cid, ghostdata] : myGhosts()->m_sendGhost) {
769 [ + - ]: 568410 : std::vector< std::size_t > tetid( ghostdata.size() );
770 [ + - ][ + - ]: 1136820 : std::vector< std::vector< tk::real > > u( ghostdata.size() ),
[ - - ]
771 [ + - ]: 568410 : prim( ghostdata.size() );
772 : : std::size_t j = 0;
773 [ + + ]: 10896600 : for(const auto& i : ghostdata) {
774 : : Assert( i < myGhosts()->m_fd.Esuel().size()/4, "Sending reconstructed ghost "
775 : : "data" );
776 [ + - ]: 10328190 : tetid[j] = i;
777 [ + - ][ - + ]: 10328190 : u[j] = m_u[i];
778 [ + - ][ - + ]: 10328190 : prim[j] = m_p[i];
779 : 10328190 : ++j;
780 : : }
781 [ + - ][ + - ]: 1136820 : thisProxy[ cid ].comreco( thisIndex, tetid, u, prim );
782 : : }
783 : :
784 : 71805 : ownreco_complete();
785 : 71805 : }
786 : :
787 : : void
788 : 568410 : DG::comreco( int fromch,
789 : : const std::vector< std::size_t >& tetid,
790 : : const std::vector< std::vector< tk::real > >& u,
791 : : const std::vector< std::vector< tk::real > >& prim )
792 : : // *****************************************************************************
793 : : // Receive chare-boundary reconstructed ghost data from neighboring chares
794 : : //! \param[in] fromch Sender chare id
795 : : //! \param[in] tetid Ghost tet ids we receive solution data for
796 : : //! \param[in] u Reconstructed high-order solution
797 : : //! \param[in] prim Limited high-order primitive quantities
798 : : //! \details This function receives contributions to the reconstructed solution
799 : : //! from fellow chares.
800 : : // *****************************************************************************
801 : : {
802 : : Assert( u.size() == tetid.size(), "Size mismatch in DG::comreco()" );
803 : : Assert( prim.size() == tetid.size(), "Size mismatch in DG::comreco()" );
804 : :
805 : : // Find local-to-ghost tet id map for sender chare
806 : 568410 : const auto& n = tk::cref_find( myGhosts()->m_ghost, fromch );
807 : :
808 [ + + ]: 10896600 : for (std::size_t i=0; i<tetid.size(); ++i) {
809 : 10328190 : auto j = tk::cref_find( n, tetid[i] );
810 : : Assert( j >= myGhosts()->m_fd.Esuel().size()/4,
811 : : "Receiving solution non-ghost data" );
812 : 10328190 : auto b = tk::cref_find( myGhosts()->m_bid, j );
813 : : Assert( b < m_uc[1].size(), "Indexing out of bounds" );
814 : : Assert( b < m_pc[1].size(), "Indexing out of bounds" );
815 : 10328190 : m_uc[1][b] = u[i];
816 : 10328190 : m_pc[1][b] = prim[i];
817 : : }
818 : :
819 : : // if we have received all solution ghost contributions from neighboring
820 : : // chares (chares we communicate along chare-boundary faces with), and
821 : : // contributed our solution to these neighbors, proceed to limiting
822 [ + + ]: 568410 : if (++m_nreco == myGhosts()->m_sendGhost.size()) {
823 : 67875 : m_nreco = 0;
824 : 67875 : comreco_complete();
825 : : }
826 : 568410 : }
827 : :
828 : : void
829 : 71805 : DG::nodalExtrema()
830 : : // *****************************************************************************
831 : : // Compute nodal extrema at chare-boundary nodes. Extrema at internal nodes
832 : : // are calculated in limiter function.
833 : : // *****************************************************************************
834 : : {
835 : 71805 : auto d = Disc();
836 : 71805 : auto gid = d->Gid();
837 : : auto bid = d->Bid();
838 : 71805 : const auto rdof = g_inputdeck.get< tag::rdof >();
839 : 71805 : const auto ncomp = m_u.nprop() / rdof;
840 : 71805 : const auto nprim = m_p.nprop() / rdof;
841 : :
842 : : // Combine own and communicated contributions of unlimited solution, and
843 : : // if a p-adaptive algorithm is used, degrees of freedom in cells
844 [ + - ][ + + ]: 10471800 : for (const auto& [boundary, localtet] : myGhosts()->m_bid) {
845 : : Assert( m_uc[1][localtet].size() == m_u.nprop(), "ncomp size mismatch" );
846 : : Assert( m_pc[1][localtet].size() == m_p.nprop(), "ncomp size mismatch" );
847 [ + + ]: 180763245 : for (std::size_t c=0; c<m_u.nprop(); ++c) {
848 : 170435055 : m_u(boundary,c) = m_uc[1][localtet][c];
849 : : }
850 [ + + ]: 36390615 : for (std::size_t c=0; c<m_p.nprop(); ++c) {
851 : 26062425 : m_p(boundary,c) = m_pc[1][localtet][c];
852 : : }
853 : : }
854 : :
855 : : // Initialize nodal extrema vector
856 : : auto large = std::numeric_limits< tk::real >::max();
857 [ + + ]: 2409255 : for(std::size_t i = 0; i<bid.size(); i++)
858 : : {
859 [ + + ]: 9559740 : for (std::size_t c=0; c<ncomp; ++c)
860 : : {
861 [ + + ]: 28889160 : for(std::size_t idof=0; idof<m_ndof_NodalExtrm; idof++)
862 : : {
863 : 21666870 : auto max_mark = 2*c*m_ndof_NodalExtrm + 2*idof;
864 : 21666870 : auto min_mark = max_mark + 1;
865 : 21666870 : m_uNodalExtrm[i][max_mark] = -large;
866 : 21666870 : m_uNodalExtrm[i][min_mark] = large;
867 : : }
868 : : }
869 [ + + ]: 3753300 : for (std::size_t c=0; c<nprim; ++c)
870 : : {
871 [ + + ]: 5663400 : for(std::size_t idof=0; idof<m_ndof_NodalExtrm; idof++)
872 : : {
873 : 4247550 : auto max_mark = 2*c*m_ndof_NodalExtrm + 2*idof;
874 : 4247550 : auto min_mark = max_mark + 1;
875 : 4247550 : m_pNodalExtrm[i][max_mark] = -large;
876 : 4247550 : m_pNodalExtrm[i][min_mark] = large;
877 : : }
878 : : }
879 : : }
880 : :
881 : : // Evaluate the max/min value for the chare-boundary nodes
882 [ + + ]: 71805 : if(rdof > 4) {
883 [ + - ]: 14970 : evalNodalExtrmRefEl(ncomp, nprim, m_ndof_NodalExtrm, d->bndel(),
884 [ + - ][ + - ]: 7485 : myGhosts()->m_inpoel, gid, bid, m_u, m_p, m_uNodalExtrm, m_pNodalExtrm);
885 : : }
886 : :
887 : : // Communicate extrema at nodes to other chares on chare-boundary
888 [ + + ]: 71805 : if (d->NodeCommMap().empty()) // in serial we are done
889 [ + - ]: 3930 : comnodalExtrema_complete();
890 : : else // send nodal extrema to chare-boundary nodes to fellow chares
891 : : {
892 [ + + ]: 636285 : for (const auto& [c,n] : d->NodeCommMap()) {
893 [ + - ][ + - ]: 1136820 : std::vector< std::vector< tk::real > > g1( n.size() ), g2( n.size() );
894 : : std::size_t j = 0;
895 [ + + ]: 4870500 : for (auto i : n)
896 : : {
897 : 4302090 : auto p = tk::cref_find(d->Bid(),i);
898 [ + - ]: 4302090 : g1[ j ] = m_uNodalExtrm[ p ];
899 [ + - ]: 4302090 : g2[ j++ ] = m_pNodalExtrm[ p ];
900 : : }
901 [ + - ][ + - ]: 1136820 : thisProxy[c].comnodalExtrema( std::vector<std::size_t>(begin(n),end(n)),
[ + - ][ - + ]
902 : : g1, g2 );
903 : : }
904 : : }
905 [ + - ]: 71805 : ownnodalExtrema_complete();
906 : 71805 : }
907 : :
908 : : void
909 : 568410 : DG::comnodalExtrema( const std::vector< std::size_t >& gid,
910 : : const std::vector< std::vector< tk::real > >& G1,
911 : : const std::vector< std::vector< tk::real > >& G2 )
912 : : // *****************************************************************************
913 : : // Receive contributions to nodal extrema on chare-boundaries
914 : : //! \param[in] gid Global mesh node IDs at which we receive grad contributions
915 : : //! \param[in] G1 Partial contributions of extrema for conservative variables to
916 : : //! chare-boundary nodes
917 : : //! \param[in] G2 Partial contributions of extrema for primitive variables to
918 : : //! chare-boundary nodes
919 : : //! \details This function receives contributions to m_uNodalExtrm/m_pNodalExtrm
920 : : //! , which stores nodal extrems at mesh chare-boundary nodes. While
921 : : //! m_uNodalExtrm/m_pNodalExtrm stores own contributions, m_uNodalExtrmc
922 : : //! /m_pNodalExtrmc collects the neighbor chare contributions during
923 : : //! communication.
924 : : // *****************************************************************************
925 : : {
926 : : Assert( G1.size() == gid.size(), "Size mismatch" );
927 : : Assert( G2.size() == gid.size(), "Size mismatch" );
928 : :
929 : 568410 : const auto rdof = g_inputdeck.get< tag::rdof >();
930 : 568410 : const auto ncomp = m_u.nprop() / rdof;
931 : 568410 : const auto nprim = m_p.nprop() / rdof;
932 : :
933 [ + + ]: 4870500 : for (std::size_t i=0; i<gid.size(); ++i)
934 : : {
935 : : auto& u = m_uNodalExtrmc[gid[i]];
936 : 4302090 : auto& p = m_pNodalExtrmc[gid[i]];
937 [ + + ]: 20510040 : for (std::size_t c=0; c<ncomp; ++c)
938 : : {
939 [ + + ]: 64831800 : for(std::size_t idof=0; idof<m_ndof_NodalExtrm; idof++)
940 : : {
941 : 48623850 : auto max_mark = 2*c*m_ndof_NodalExtrm + 2*idof;
942 : 48623850 : auto min_mark = max_mark + 1;
943 [ + + ][ + + ]: 49173216 : u[max_mark] = std::max( G1[i][max_mark], u[max_mark] );
944 : 48623850 : u[min_mark] = std::min( G1[i][min_mark], u[min_mark] );
945 : : }
946 : : }
947 [ + + ]: 7907040 : for (std::size_t c=0; c<nprim; ++c)
948 : : {
949 [ + + ]: 14419800 : for(std::size_t idof=0; idof<m_ndof_NodalExtrm; idof++)
950 : : {
951 : 10814850 : auto max_mark = 2*c*m_ndof_NodalExtrm + 2*idof;
952 : 10814850 : auto min_mark = max_mark + 1;
953 [ - + ][ - + ]: 10814850 : p[max_mark] = std::max( G2[i][max_mark], p[max_mark] );
954 : 10814850 : p[min_mark] = std::min( G2[i][min_mark], p[min_mark] );
955 : : }
956 : : }
957 : : }
958 : :
959 [ + + ]: 568410 : if (++m_nnodalExtrema == Disc()->NodeCommMap().size())
960 : : {
961 : 67875 : m_nnodalExtrema = 0;
962 : 67875 : comnodalExtrema_complete();
963 : : }
964 : 568410 : }
965 : :
966 : 72694 : void DG::resizeNodalExtremac()
967 : : // *****************************************************************************
968 : : // Resize the buffer vector of nodal extrema
969 : : // *****************************************************************************
970 : : {
971 : 72694 : const auto rdof = g_inputdeck.get< tag::rdof >();
972 : 72694 : const auto ncomp = m_u.nprop() / rdof;
973 : 72694 : const auto nprim = m_p.nprop() / rdof;
974 : :
975 : 72694 : auto large = std::numeric_limits< tk::real >::max();
976 [ + - ][ + + ]: 722206 : for (const auto& [c,n] : Disc()->NodeCommMap())
977 : : {
978 [ + + ][ + - ]: 4936514 : for (auto i : n) {
979 : : auto& u = m_uNodalExtrmc[i];
980 : : auto& p = m_pNodalExtrmc[i];
981 [ + - ]: 4359696 : u.resize( 2*m_ndof_NodalExtrm*ncomp, large );
982 [ + - ]: 4359696 : p.resize( 2*m_ndof_NodalExtrm*nprim, large );
983 : :
984 : : // Initialize the minimum nodal extrema
985 [ + + ]: 17438784 : for(std::size_t idof=0; idof<m_ndof_NodalExtrm; idof++)
986 : : {
987 [ + + ]: 62362950 : for(std::size_t k = 0; k < ncomp; k++)
988 : 49283862 : u[2*k*m_ndof_NodalExtrm+2*idof] = -large;
989 [ + + ]: 24033780 : for(std::size_t k = 0; k < nprim; k++)
990 : 10954692 : p[2*k*m_ndof_NodalExtrm+2*idof] = -large;
991 : : }
992 : : }
993 : : }
994 : 72694 : }
995 : :
996 : 7485 : void DG::evalNodalExtrmRefEl(
997 : : const std::size_t ncomp,
998 : : const std::size_t nprim,
999 : : const std::size_t ndof_NodalExtrm,
1000 : : const std::vector< std::size_t >& bndel,
1001 : : const std::vector< std::size_t >& inpoel,
1002 : : const std::vector< std::size_t >& gid,
1003 : : const std::unordered_map< std::size_t, std::size_t >& bid,
1004 : : const tk::Fields& U,
1005 : : const tk::Fields& P,
1006 : : std::vector< std::vector<tk::real> >& uNodalExtrm,
1007 : : std::vector< std::vector<tk::real> >& pNodalExtrm )
1008 : : // *****************************************************************************
1009 : : // Compute the nodal extrema of ref el derivatives for chare-boundary nodes
1010 : : //! \param[in] ncomp Number of conservative variables
1011 : : //! \param[in] nprim Number of primitive variables
1012 : : //! \param[in] ndof_NodalExtrm Degree of freedom for nodal extrema
1013 : : //! \param[in] bndel List of elements contributing to chare-boundary nodes
1014 : : //! \param[in] inpoel Element-node connectivity for element e
1015 : : //! \param[in] gid Local->global node id map
1016 : : //! \param[in] bid Local chare-boundary node ids (value) associated to
1017 : : //! global node ids (key)
1018 : : //! \param[in] U Vector of conservative variables
1019 : : //! \param[in] P Vector of primitive variables
1020 : : //! \param[in,out] uNodalExtrm Chare-boundary nodal extrema for conservative
1021 : : //! variables
1022 : : //! \param[in,out] pNodalExtrm Chare-boundary nodal extrema for primitive
1023 : : //! variables
1024 : : // *****************************************************************************
1025 : : {
1026 : 7485 : const auto rdof = g_inputdeck.get< tag::rdof >();
1027 : :
1028 [ + + ]: 702165 : for (auto e : bndel)
1029 : : {
1030 : : // access node IDs
1031 : : const std::vector<std::size_t> N
1032 [ + - ]: 694680 : { inpoel[e*4+0], inpoel[e*4+1], inpoel[e*4+2], inpoel[e*4+3] };
1033 : :
1034 : : // Loop over nodes of element e
1035 [ + + ]: 3473400 : for(std::size_t ip=0; ip<4; ++ip)
1036 : : {
1037 : 2778720 : auto i = bid.find( gid[N[ip]] );
1038 [ + + ]: 2778720 : if (i != end(bid)) // If ip is the chare boundary point
1039 : : {
1040 : : // If DG(P2) is applied, find the nodal extrema of the gradients of
1041 : : // conservative/primitive variables in the reference element
1042 : :
1043 : : // Vector used to store the first order derivatives for both
1044 : : // conservative and primitive variables
1045 [ + - ][ - - ]: 1861455 : std::vector< std::array< tk::real, 3 > > gradc(ncomp, {0.0, 0.0, 0.0});
1046 [ + - ][ - - ]: 1861455 : std::vector< std::array< tk::real, 3 > > gradp(ncomp, {0.0, 0.0, 0.0});
1047 : :
1048 : : // Derivatives of the Dubiner basis
1049 : 1861455 : std::array< tk::real, 3 > center {{0.25, 0.25, 0.25}};
1050 [ + - ]: 1861455 : auto dBdxi = tk::eval_dBdxi(rdof, center);
1051 : :
1052 : : // Evaluate the first order derivative
1053 [ + + ]: 10741830 : for(std::size_t icomp = 0; icomp < ncomp; icomp++)
1054 : : {
1055 : 8880375 : auto mark = icomp * rdof;
1056 [ + + ]: 35521500 : for(std::size_t idir = 0; idir < 3; idir++)
1057 : : {
1058 : 26641125 : gradc[icomp][idir] = 0;
1059 [ + + ]: 266411250 : for(std::size_t idof = 1; idof < rdof; idof++)
1060 : 239770125 : gradc[icomp][idir] += U(e, mark+idof) * dBdxi[idir][idof];
1061 : : }
1062 : : }
1063 [ - + ]: 1861455 : for(std::size_t icomp = 0; icomp < nprim; icomp++)
1064 : : {
1065 : 0 : auto mark = icomp * rdof;
1066 [ - - ]: 0 : for(std::size_t idir = 0; idir < 3; idir++)
1067 : : {
1068 : 0 : gradp[icomp][idir] = 0;
1069 [ - - ]: 0 : for(std::size_t idof = 1; idof < rdof; idof++)
1070 : 0 : gradp[icomp][idir] += P(e, mark+idof) * dBdxi[idir][idof];
1071 : : }
1072 : : }
1073 : :
1074 : : // Store the extrema for the gradients
1075 [ + + ]: 10741830 : for (std::size_t c=0; c<ncomp; ++c)
1076 : : {
1077 [ + + ]: 35521500 : for (std::size_t idof = 0; idof < ndof_NodalExtrm; idof++)
1078 : : {
1079 : 26641125 : auto max_mark = 2*c*m_ndof_NodalExtrm + 2*idof;
1080 : 26641125 : auto min_mark = max_mark + 1;
1081 [ + + ]: 26641125 : auto& ex = uNodalExtrm[i->second];
1082 [ + + ][ + + ]: 33418446 : ex[max_mark] = std::max(ex[max_mark], gradc[c][idof]);
1083 : 26641125 : ex[min_mark] = std::min(ex[min_mark], gradc[c][idof]);
1084 : : }
1085 : : }
1086 [ - + ]: 1861455 : for (std::size_t c=0; c<nprim; ++c)
1087 : : {
1088 [ - - ]: 0 : for (std::size_t idof = 0; idof < ndof_NodalExtrm; idof++)
1089 : : {
1090 : 0 : auto max_mark = 2*c*m_ndof_NodalExtrm + 2*idof;
1091 : 0 : auto min_mark = max_mark + 1;
1092 [ - - ]: 0 : auto& ex = pNodalExtrm[i->second];
1093 [ - - ][ - - ]: 0 : ex[max_mark] = std::max(ex[max_mark], gradp[c][idof]);
1094 : 0 : ex[min_mark] = std::min(ex[min_mark], gradp[c][idof]);
1095 : : }
1096 : : }
1097 : : }
1098 : : }
1099 : : }
1100 : 7485 : }
1101 : :
1102 : : void
1103 : 71805 : DG::lim()
1104 : : // *****************************************************************************
1105 : : // Compute limiter function
1106 : : // *****************************************************************************
1107 : : {
1108 : 71805 : auto d = Disc();
1109 : 71805 : const auto rdof = g_inputdeck.get< tag::rdof >();
1110 : 71805 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
1111 : 71805 : const auto ncomp = m_u.nprop() / rdof;
1112 : 71805 : const auto nprim = m_p.nprop() / rdof;
1113 : :
1114 : : // Combine own and communicated contributions to nodal extrema
1115 [ + + ]: 2409255 : for (const auto& [gid,g] : m_uNodalExtrmc) {
1116 : 2337450 : auto bid = tk::cref_find( d->Bid(), gid );
1117 [ + + ]: 9559740 : for (ncomp_t c=0; c<ncomp; ++c)
1118 : : {
1119 [ + + ]: 28889160 : for(std::size_t idof=0; idof<m_ndof_NodalExtrm; idof++)
1120 : : {
1121 : 21666870 : auto max_mark = 2*c*m_ndof_NodalExtrm + 2*idof;
1122 : 21666870 : auto min_mark = max_mark + 1;
1123 : 21666870 : m_uNodalExtrm[bid][max_mark] =
1124 [ + + ][ + + ]: 22413928 : std::max(g[max_mark], m_uNodalExtrm[bid][max_mark]);
1125 : 21666870 : m_uNodalExtrm[bid][min_mark] =
1126 : 21666870 : std::min(g[min_mark], m_uNodalExtrm[bid][min_mark]);
1127 : : }
1128 : : }
1129 : : }
1130 [ + + ]: 2409255 : for (const auto& [gid,g] : m_pNodalExtrmc) {
1131 : 2337450 : auto bid = tk::cref_find( d->Bid(), gid );
1132 [ + + ]: 3753300 : for (ncomp_t c=0; c<nprim; ++c)
1133 : : {
1134 [ + + ]: 5663400 : for(std::size_t idof=0; idof<m_ndof_NodalExtrm; idof++)
1135 : : {
1136 : 4247550 : auto max_mark = 2*c*m_ndof_NodalExtrm + 2*idof;
1137 : 4247550 : auto min_mark = max_mark + 1;
1138 : 4247550 : m_pNodalExtrm[bid][max_mark] =
1139 [ - + ][ - + ]: 4247550 : std::max(g[max_mark], m_pNodalExtrm[bid][max_mark]);
1140 : 4247550 : m_pNodalExtrm[bid][min_mark] =
1141 : 4247550 : std::min(g[min_mark], m_pNodalExtrm[bid][min_mark]);
1142 : : }
1143 : : }
1144 : : }
1145 : :
1146 : : // clear gradients receive buffer
1147 : 71805 : tk::destroy(m_uNodalExtrmc);
1148 : 71805 : tk::destroy(m_pNodalExtrmc);
1149 : :
1150 [ + + ]: 71805 : if (rdof > 1) {
1151 : 87930 : g_dgpde[d->MeshId()].limit( d->T(), pref, myGhosts()->m_geoFace,
1152 : 43965 : myGhosts()->m_geoElem, myGhosts()->m_fd, myGhosts()->m_esup,
1153 : 43965 : myGhosts()->m_inpoel, myGhosts()->m_coord, m_ndof, d->Gid(),
1154 : 43965 : d->Bid(), m_uNodalExtrm, m_pNodalExtrm, m_mtInv, m_u, m_p,
1155 : 43965 : m_shockmarker );
1156 : :
1157 [ + + ]: 43965 : if (g_inputdeck.get< tag::limsol_projection >())
1158 : 79830 : g_dgpde[d->MeshId()].CPL(m_p, myGhosts()->m_geoElem,
1159 : 39915 : myGhosts()->m_inpoel, myGhosts()->m_coord, m_u,
1160 : 39915 : myGhosts()->m_fd.Esuel().size()/4);
1161 : : }
1162 : :
1163 : : // Send limited solution to neighboring chares
1164 [ + + ]: 71805 : if (myGhosts()->m_sendGhost.empty())
1165 : 3930 : comlim_complete();
1166 : : else
1167 [ + + ]: 704160 : for(const auto& [cid, ghostdata] : myGhosts()->m_sendGhost) {
1168 [ + - ]: 568410 : std::vector< std::size_t > tetid( ghostdata.size() );
1169 [ + - ][ + - ]: 1136820 : std::vector< std::vector< tk::real > > u( ghostdata.size() ),
[ - - ]
1170 [ + - ]: 568410 : prim( ghostdata.size() );
1171 : : std::vector< std::size_t > ndof;
1172 : : std::size_t j = 0;
1173 [ + + ]: 10896600 : for(const auto& i : ghostdata) {
1174 : : Assert( i < myGhosts()->m_fd.Esuel().size()/4,
1175 : : "Sending limiter ghost data" );
1176 [ + - ]: 10328190 : tetid[j] = i;
1177 [ + - ][ - + ]: 10328190 : u[j] = m_u[i];
1178 [ + - ][ - + ]: 10328190 : prim[j] = m_p[i];
1179 : 10328190 : ++j;
1180 : : }
1181 [ + - ][ + - ]: 1136820 : thisProxy[ cid ].comlim( thisIndex, tetid, u, prim );
1182 : : }
1183 : :
1184 : 71805 : ownlim_complete();
1185 : 71805 : }
1186 : :
1187 : : void
1188 : 1770 : DG::refine_ndof()
1189 : : // *****************************************************************************
1190 : : // p-refine all elements that are adjacent to p-refined elements
1191 : : //! \details This function p-refines all the neighbors of an element that has
1192 : : //! been p-refined as a result of an error indicator.
1193 : : // *****************************************************************************
1194 : : {
1195 : 1770 : auto d = Disc();
1196 : : const auto& coord = d->Coord();
1197 : 1770 : const auto& inpoel = d->Inpoel();
1198 : 1770 : const auto npoin = coord[0].size();
1199 : 1770 : const auto nelem = myGhosts()->m_fd.Esuel().size()/4;
1200 : 1770 : std::vector<std::size_t> node_ndof(npoin, 1);
1201 : :
1202 : : // Mark the max ndof for each node and store in node_ndof
1203 [ + + ]: 188540 : for(std::size_t ip=0; ip<npoin; ip++)
1204 : : {
1205 [ + - ]: 186770 : const auto& pesup = tk::cref_find(myGhosts()->m_esup, ip);
1206 [ + + ]: 2641810 : for(auto er : pesup)
1207 [ + + ]: 2536198 : node_ndof[ip] = std::max(m_ndof[er], node_ndof[ip]);
1208 : : }
1209 : :
1210 [ + + ]: 414490 : for(std::size_t e = 0; e < nelem; e++)
1211 : : {
1212 : : // Find if any node of this element has p1/p2 ndofs
1213 : : std::size_t counter_p2(0);
1214 : : std::size_t counter_p1(0);
1215 [ + + ]: 2063600 : for(std::size_t inode = 0; inode < 4; inode++)
1216 : : {
1217 [ + + ]: 1650880 : auto node = inpoel[4*e+inode];
1218 [ + + ]: 1650880 : if(node_ndof[node] == 10)
1219 : 320808 : counter_p2++;
1220 [ + + ]: 1330072 : else if (node_ndof[node] == 4)
1221 : 180140 : counter_p1++;
1222 : : }
1223 : :
1224 : : // If there is at least one node with p1/p2 ndofs, all of the elements
1225 : : // around this node are refined to p1/p2.
1226 [ + + ][ + + ]: 412720 : if(counter_p2 > 0 && m_ndof[e] < 10)
1227 : : {
1228 [ + + ]: 16717 : if(m_ndof[e] == 4)
1229 : 15693 : m_ndof[e] = 10;
1230 [ + + ]: 16717 : if(m_ndof[e] == 1)
1231 : 1024 : m_ndof[e] = 4;
1232 : : }
1233 [ + + ][ + + ]: 396003 : else if(counter_p1 > 0 && m_ndof[e] < 4)
1234 : 13452 : m_ndof[e] = 4;
1235 : : }
1236 : 1770 : }
1237 : :
1238 : 1770 : void DG::smooth_ndof()
1239 : : // *****************************************************************************
1240 : : // Smooth the refined ndof distribution to avoid zigzag refinement
1241 : : // *****************************************************************************
1242 : : {
1243 : 1770 : auto d = Disc();
1244 : 1770 : const auto& inpoel = d->Inpoel();
1245 : : const auto& coord = d->Coord();
1246 : 1770 : const auto npoin = coord[0].size();
1247 : 1770 : const auto nelem = myGhosts()->m_fd.Esuel().size()/4;
1248 : 1770 : std::vector<std::size_t> node_ndof(npoin, 1);
1249 : :
1250 : : // Mark the max ndof for each node and store in node_ndof
1251 [ + + ]: 188540 : for(std::size_t ip=0; ip<npoin; ip++)
1252 : : {
1253 [ + - ]: 186770 : const auto& pesup = tk::cref_find(myGhosts()->m_esup, ip);
1254 [ + + ]: 2641810 : for(auto er : pesup)
1255 [ + + ]: 2542889 : node_ndof[ip] = std::max(m_ndof[er], node_ndof[ip]);
1256 : : }
1257 : :
1258 [ + + ]: 414490 : for(std::size_t e = 0; e < nelem; e++)
1259 : : {
1260 : : // Find if any node of this element has p1/p2 ndofs
1261 : : std::size_t counter_p2(0);
1262 : : std::size_t counter_p1(0);
1263 [ + + ]: 2063600 : for(std::size_t inode = 0; inode < 4; inode++)
1264 : : {
1265 [ + + ]: 1650880 : auto node = inpoel[4*e+inode];
1266 [ + + ]: 1650880 : if(node_ndof[node] == 10)
1267 : 382503 : counter_p2++;
1268 [ + + ]: 1268377 : else if (node_ndof[node] == 4)
1269 : 182564 : counter_p1++;
1270 : : }
1271 : :
1272 : : // If all the nodes in the element are p1/p2, this element is refined to
1273 : : // p1/p2.
1274 [ + + ][ + + ]: 412720 : if(counter_p2 == 4 && m_ndof[e] == 4)
1275 : 1469 : m_ndof[e] = 10;
1276 [ + + ][ + + ]: 411251 : else if(counter_p1 == 4 && m_ndof[e] == 1)
1277 : 1553 : m_ndof[e] = 4;
1278 : : }
1279 : 1770 : }
1280 : :
1281 : : void
1282 : 568410 : DG::comlim( int fromch,
1283 : : const std::vector< std::size_t >& tetid,
1284 : : const std::vector< std::vector< tk::real > >& u,
1285 : : const std::vector< std::vector< tk::real > >& prim )
1286 : : // *****************************************************************************
1287 : : // Receive chare-boundary limiter ghost data from neighboring chares
1288 : : //! \param[in] fromch Sender chare id
1289 : : //! \param[in] tetid Ghost tet ids we receive solution data for
1290 : : //! \param[in] u Limited high-order solution
1291 : : //! \param[in] prim Limited high-order primitive quantities
1292 : : //! \details This function receives contributions to the limited solution from
1293 : : //! fellow chares.
1294 : : // *****************************************************************************
1295 : : {
1296 : : Assert( u.size() == tetid.size(), "Size mismatch in DG::comlim()" );
1297 : : Assert( prim.size() == tetid.size(), "Size mismatch in DG::comlim()" );
1298 : :
1299 : : // Find local-to-ghost tet id map for sender chare
1300 : 568410 : const auto& n = tk::cref_find( myGhosts()->m_ghost, fromch );
1301 : :
1302 [ + + ]: 10896600 : for (std::size_t i=0; i<tetid.size(); ++i) {
1303 : 10328190 : auto j = tk::cref_find( n, tetid[i] );
1304 : : Assert( j >= myGhosts()->m_fd.Esuel().size()/4,
1305 : : "Receiving solution non-ghost data" );
1306 : 10328190 : auto b = tk::cref_find( myGhosts()->m_bid, j );
1307 : : Assert( b < m_uc[2].size(), "Indexing out of bounds" );
1308 : : Assert( b < m_pc[2].size(), "Indexing out of bounds" );
1309 : 10328190 : m_uc[2][b] = u[i];
1310 : 10328190 : m_pc[2][b] = prim[i];
1311 : : }
1312 : :
1313 : : // if we have received all solution ghost contributions from neighboring
1314 : : // chares (chares we communicate along chare-boundary faces with), and
1315 : : // contributed our solution to these neighbors, proceed to limiting
1316 [ + + ]: 568410 : if (++m_nlim == myGhosts()->m_sendGhost.size()) {
1317 : 67875 : m_nlim = 0;
1318 : 67875 : comlim_complete();
1319 : : }
1320 : 568410 : }
1321 : :
1322 : : void
1323 : 71805 : DG::dt()
1324 : : // *****************************************************************************
1325 : : // Compute time step size
1326 : : // *****************************************************************************
1327 : : {
1328 : 71805 : auto d = Disc();
1329 : :
1330 : : // Combine own and communicated contributions of limited solution and degrees
1331 : : // of freedom in cells (if p-adaptive)
1332 [ + + ]: 10471800 : for (const auto& b : myGhosts()->m_bid) {
1333 : : Assert( m_uc[2][b.second].size() == m_u.nprop(), "ncomp size mismatch" );
1334 : : Assert( m_pc[2][b.second].size() == m_p.nprop(), "ncomp size mismatch" );
1335 [ + + ]: 180763245 : for (std::size_t c=0; c<m_u.nprop(); ++c) {
1336 : 170435055 : m_u(b.first,c) = m_uc[2][b.second][c];
1337 : : }
1338 [ + + ]: 36390615 : for (std::size_t c=0; c<m_p.nprop(); ++c) {
1339 : 26062425 : m_p(b.first,c) = m_pc[2][b.second][c];
1340 : : }
1341 : : }
1342 : :
1343 : 71805 : auto mindt = std::numeric_limits< tk::real >::max();
1344 : :
1345 [ + + ]: 71805 : if (m_stage == 0)
1346 : : {
1347 [ + + ]: 23935 : auto const_dt = g_inputdeck.get< tag::dt >();
1348 : : auto eps = std::numeric_limits< tk::real >::epsilon();
1349 : :
1350 : : // use constant dt if configured
1351 [ + + ]: 23935 : if (std::abs(const_dt) > eps) {
1352 : :
1353 : 21605 : mindt = const_dt;
1354 : :
1355 : : } else { // compute dt based on CFL
1356 : :
1357 : : // find the minimum dt across all PDEs integrated
1358 : : auto eqdt =
1359 : 4660 : g_dgpde[d->MeshId()].dt( myGhosts()->m_coord, myGhosts()->m_inpoel,
1360 : 2330 : myGhosts()->m_fd,
1361 : 2330 : myGhosts()->m_geoFace, myGhosts()->m_geoElem, m_ndof, m_u, m_p,
1362 : 2330 : myGhosts()->m_fd.Esuel().size()/4 );
1363 [ + - ]: 2330 : if (eqdt < mindt) mindt = eqdt;
1364 : :
1365 : 2330 : mindt *= g_inputdeck.get< tag::cfl >();
1366 : : }
1367 : : }
1368 : : else
1369 : : {
1370 : 47870 : mindt = d->Dt();
1371 : : }
1372 : :
1373 : : // Resize the buffer vector of nodal extrema
1374 : 71805 : resizeNodalExtremac();
1375 : :
1376 : : // Contribute to minimum dt across all chares then advance to next step
1377 [ + - ]: 71805 : contribute( sizeof(tk::real), &mindt, CkReduction::min_double,
1378 : 71805 : CkCallback(CkReductionTarget(DG,solve), thisProxy) );
1379 : 71805 : }
1380 : :
1381 : : void
1382 : 71805 : DG::solve( tk::real newdt )
1383 : : // *****************************************************************************
1384 : : // Compute right-hand side of discrete transport equations
1385 : : //! \param[in] newdt Size of this new time step
1386 : : // *****************************************************************************
1387 : : {
1388 : : // Enable SDAG wait for building the solution vector during the next stage
1389 [ + - ]: 71805 : thisProxy[ thisIndex ].wait4sol();
1390 [ + - ]: 71805 : thisProxy[ thisIndex ].wait4refine();
1391 [ + - ]: 71805 : thisProxy[ thisIndex ].wait4smooth();
1392 [ + - ]: 71805 : thisProxy[ thisIndex ].wait4reco();
1393 [ + - ]: 71805 : thisProxy[ thisIndex ].wait4nodalExtrema();
1394 [ + - ]: 71805 : thisProxy[ thisIndex ].wait4lim();
1395 [ + - ]: 71805 : thisProxy[ thisIndex ].wait4nod();
1396 : :
1397 : 71805 : auto d = Disc();
1398 : 71805 : const auto rdof = g_inputdeck.get< tag::rdof >();
1399 : 71805 : const auto ndof = g_inputdeck.get< tag::ndof >();
1400 : 71805 : const auto neq = m_u.nprop()/rdof;
1401 : 71805 : const auto pref = g_inputdeck.get< tag::pref, tag::pref >();
1402 : :
1403 : : // Set new time step size
1404 [ + + ]: 71805 : if (m_stage == 0) d->setdt( newdt );
1405 : :
1406 : : // Update Un
1407 [ + + ]: 71805 : if (m_stage == 0) m_un = m_u;
1408 : :
1409 : : // Explicit or IMEX
1410 : 71805 : const auto imex_runge_kutta = g_inputdeck.get< tag::imex_runge_kutta >();
1411 : :
1412 : : // physical time at time-stage for computing exact source terms
1413 : 71805 : tk::real physT(d->T());
1414 [ + + ]: 71805 : if (m_stage == 1) {
1415 : 23935 : physT += d->Dt();
1416 : : }
1417 [ + + ]: 47870 : else if (m_stage == 2) {
1418 : 23935 : physT += 0.5*d->Dt();
1419 : : }
1420 : :
1421 [ - + ]: 71805 : if (imex_runge_kutta) {
1422 [ - - ]: 0 : if (m_stage == 0)
1423 : : {
1424 : : // Save previous rhs
1425 : : m_rhsprev = m_rhs;
1426 : : // Initialize m_stiffrhs to zero
1427 : : m_stiffrhs.fill(0.0);
1428 : : m_stiffrhsprev.fill(0.0);
1429 : : }
1430 : : }
1431 : :
1432 : 143610 : g_dgpde[d->MeshId()].rhs( physT, pref, myGhosts()->m_geoFace,
1433 : 71805 : myGhosts()->m_geoElem, myGhosts()->m_fd, myGhosts()->m_inpoel, m_boxelems,
1434 : 71805 : myGhosts()->m_coord, m_u, m_p, m_ndof, d->Dt(), m_rhs );
1435 : :
1436 [ + - ]: 71805 : if (!imex_runge_kutta) {
1437 : : // Explicit time-stepping using RK3 to discretize time-derivative
1438 [ + + ]: 27426315 : for(std::size_t e=0; e<myGhosts()->m_nunk; ++e)
1439 [ + + ]: 122084055 : for(std::size_t c=0; c<neq; ++c)
1440 : : {
1441 [ + + ]: 393027120 : for (std::size_t k=0; k<m_numEqDof[c]; ++k)
1442 : : {
1443 [ + + ]: 298297575 : if(k < m_ndof[e]) {
1444 : 213545115 : auto rmark = c*rdof+k;
1445 : 213545115 : auto mark = c*ndof+k;
1446 [ + + ]: 213545115 : m_u(e, rmark) = rkcoef[0][m_stage] * m_un(e, rmark)
1447 : 213545115 : + rkcoef[1][m_stage] * ( m_u(e, rmark)
1448 : 213545115 : + d->Dt() * m_rhs(e, mark)/m_lhs(e, mark) );
1449 [ + + ]: 213545115 : if(fabs(m_u(e, rmark)) < 1e-16)
1450 : 51984049 : m_u(e, rmark) = 0;
1451 : : }
1452 : : }
1453 : : }
1454 : : }
1455 : : else {
1456 : : // Implicit-Explicit time-stepping using RK3 to discretize time-derivative
1457 : 0 : DG::imex_integrate();
1458 : : }
1459 : :
1460 [ + + ]: 27426315 : for(std::size_t e=0; e<myGhosts()->m_nunk; ++e)
1461 [ + + ]: 122084055 : for(std::size_t c=0; c<neq; ++c)
1462 : : {
1463 : : // zero out unused/reconstructed dofs of equations using reduced dofs
1464 : : // (see DGMultiMat::numEquationDofs())
1465 [ + + ]: 94729545 : if (m_numEqDof[c] < rdof) {
1466 [ + + ]: 96926640 : for (std::size_t k=m_numEqDof[c]; k<rdof; ++k)
1467 : : {
1468 : 72694980 : auto rmark = c*rdof+k;
1469 : 72694980 : m_u(e, rmark) = 0.0;
1470 : : }
1471 : : }
1472 : : }
1473 : :
1474 : : // Update primitives based on the evolved solution
1475 : 71805 : g_dgpde[d->MeshId()].updateInterfaceCells( m_u,
1476 : 71805 : myGhosts()->m_fd.Esuel().size()/4, m_ndof, m_interface );
1477 : 71805 : g_dgpde[d->MeshId()].updatePrimitives( m_u, m_lhs, myGhosts()->m_geoElem, m_p,
1478 : 71805 : myGhosts()->m_fd.Esuel().size()/4, m_ndof );
1479 [ + - ]: 71805 : if (!g_inputdeck.get< tag::accuracy_test >()) {
1480 : 71805 : g_dgpde[d->MeshId()].cleanTraceMaterial( physT, myGhosts()->m_geoElem, m_u,
1481 : 71805 : m_p, myGhosts()->m_fd.Esuel().size()/4 );
1482 : : }
1483 : :
1484 [ + + ]: 71805 : if (m_stage < 2) {
1485 : :
1486 : : // continue with next time step stage
1487 : 47870 : stage();
1488 : :
1489 : : } else {
1490 : :
1491 : : // Increase number of iterations and physical time
1492 : 23935 : d->next();
1493 : :
1494 : : // Compute diagnostics, e.g., residuals
1495 : 23935 : auto diag_computed = m_diag.compute( *d,
1496 : 23935 : m_u.nunk()-myGhosts()->m_fd.Esuel().size()/4, myGhosts()->m_geoElem,
1497 : 23935 : m_ndof, m_u, m_un );
1498 : :
1499 : : // Continue to mesh refinement (if configured)
1500 [ + + ][ + - ]: 33863 : if (!diag_computed) refine( std::vector< tk::real >( m_u.nprop(), 0.0 ) );
[ + - ]
1501 : :
1502 : : }
1503 : 71805 : }
1504 : :
1505 : : void
1506 : 23935 : DG::refine( [[maybe_unused]] const std::vector< tk::real >& l2res )
1507 : : // *****************************************************************************
1508 : : // Optionally refine/derefine mesh
1509 : : //! \param[in] l2res L2-norms of the residual for each scalar component
1510 : : //! computed across the whole problem
1511 : : // *****************************************************************************
1512 : : {
1513 : 23935 : auto d = Disc();
1514 : :
1515 : 23935 : auto dtref = g_inputdeck.get< tag::amr, tag::dtref >();
1516 : 23935 : auto dtfreq = g_inputdeck.get< tag::amr, tag::dtfreq >();
1517 : :
1518 : : // if t>0 refinement enabled and we hit the dtref frequency
1519 [ - + ][ - - ]: 23935 : if (dtref && !(d->It() % dtfreq)) { // refine
1520 : :
1521 : 0 : d->startvol();
1522 [ - - ][ - - ]: 0 : d->Ref()->dtref( myGhosts()->m_fd.Bface(), {},
[ - - ][ - - ]
1523 : 0 : tk::remap(myGhosts()->m_fd.Triinpoel(),d->Gid()) );
1524 : 0 : d->refined() = 1;
1525 : :
1526 : : } else { // do not refine
1527 : :
1528 : 23935 : d->refined() = 0;
1529 : 23935 : stage();
1530 : :
1531 : : }
1532 : 23935 : }
1533 : :
1534 : : void
1535 : 0 : DG::resizePostAMR(
1536 : : const std::vector< std::size_t >& /*ginpoel*/,
1537 : : const tk::UnsMesh::Chunk& chunk,
1538 : : const tk::UnsMesh::Coords& coord,
1539 : : const std::unordered_map< std::size_t, tk::UnsMesh::Edge >& /*addedNodes*/,
1540 : : const std::unordered_map< std::size_t, std::size_t >& addedTets,
1541 : : const std::set< std::size_t >& removedNodes,
1542 : : const std::unordered_map< std::size_t, std::size_t >& amrNodeMap,
1543 : : const tk::NodeCommMap& nodeCommMap,
1544 : : const std::map< int, std::vector< std::size_t > >& bface,
1545 : : const std::map< int, std::vector< std::size_t > >& /* bnode */,
1546 : : const std::vector< std::size_t >& triinpoel,
1547 : : const std::unordered_map< std::size_t, std::set< std::size_t > >& elemblockid )
1548 : : // *****************************************************************************
1549 : : // Receive new mesh from Refiner
1550 : : //! \param[in] chunk New mesh chunk (connectivity and global<->local id maps)
1551 : : //! \param[in] coord New mesh node coordinates
1552 : : //! \param[in] addedTets Newly added mesh cells and their parents (local ids)
1553 : : //! \param[in] removedNodes Newly removed mesh node local ids
1554 : : //! \param[in] amrNodeMap Node id map after amr (local ids)
1555 : : //! \param[in] nodeCommMap New node communication map
1556 : : //! \param[in] bface Boundary-faces mapped to side set ids
1557 : : //! \param[in] triinpoel Boundary-face connectivity
1558 : : //! \param[in] elemblockid Local tet ids associated with mesh block ids
1559 : : // *****************************************************************************
1560 : : {
1561 : 0 : auto d = Disc();
1562 : :
1563 : : // Set flag that indicates that we are during time stepping
1564 : 0 : m_initial = 0;
1565 : 0 : myGhosts()->m_initial = 0;
1566 : :
1567 : : // Zero field output iteration count between two mesh refinement steps
1568 : 0 : d->Itf() = 0;
1569 : :
1570 : : // Increase number of iterations with mesh refinement
1571 : 0 : ++d->Itr();
1572 : :
1573 : : // Save old number of elements
1574 : 0 : [[maybe_unused]] auto old_nelem = myGhosts()->m_inpoel.size()/4;
1575 : :
1576 : : // Resize mesh data structures
1577 : 0 : d->resizePostAMR( chunk, coord, amrNodeMap, nodeCommMap, removedNodes,
1578 : : elemblockid );
1579 : :
1580 : : // Update state
1581 : 0 : myGhosts()->m_inpoel = d->Inpoel();
1582 : 0 : myGhosts()->m_coord = d->Coord();
1583 : 0 : auto nelem = myGhosts()->m_inpoel.size()/4;
1584 : : m_p.resize( nelem );
1585 : : m_u.resize( nelem );
1586 : : m_un.resize( nelem );
1587 : : m_lhs.resize( nelem );
1588 : : m_rhs.resize( nelem );
1589 : : m_rhsprev.resize( nelem );
1590 : : m_stiffrhs.resize( nelem );
1591 : : m_stiffrhsprev.resize( nelem );
1592 [ - - ][ - - ]: 0 : m_uNodalExtrm.resize( Disc()->Bid().size(), std::vector<tk::real>( 2*
1593 [ - - ]: 0 : m_ndof_NodalExtrm*g_inputdeck.get< tag::ncomp >() ) );
1594 [ - - ][ - - ]: 0 : m_pNodalExtrm.resize( Disc()->Bid().size(), std::vector<tk::real>( 2*
1595 [ - - ]: 0 : m_ndof_NodalExtrm*m_p.nprop()/g_inputdeck.get< tag::rdof >()));
1596 : :
1597 : : // Resize the buffer vector of nodal extrema
1598 : 0 : resizeNodalExtremac();
1599 : :
1600 [ - - ][ - - ]: 0 : myGhosts()->m_fd = FaceData( myGhosts()->m_inpoel, bface,
[ - - ][ - - ]
[ - - ]
1601 : 0 : tk::remap(triinpoel,d->Lid()) );
1602 : :
1603 [ - - ]: 0 : myGhosts()->m_geoFace =
1604 : 0 : tk::Fields( tk::genGeoFaceTri( myGhosts()->m_fd.Nipfac(),
1605 : 0 : myGhosts()->m_fd.Inpofa(), coord ) );
1606 [ - - ]: 0 : myGhosts()->m_geoElem = tk::Fields( tk::genGeoElemTet( myGhosts()->m_inpoel,
1607 : 0 : coord ) );
1608 : :
1609 : 0 : myGhosts()->m_nfac = myGhosts()->m_fd.Inpofa().size()/3;
1610 : 0 : myGhosts()->m_nunk = nelem;
1611 : 0 : m_npoin = coord[0].size();
1612 : 0 : myGhosts()->m_bndFace.clear();
1613 : 0 : myGhosts()->m_exptGhost.clear();
1614 : 0 : myGhosts()->m_sendGhost.clear();
1615 : 0 : myGhosts()->m_ghost.clear();
1616 : 0 : myGhosts()->m_esup.clear();
1617 : :
1618 : : // Update solution on new mesh, P0 (cell center value) only for now
1619 : : m_un = m_u;
1620 : : auto pn = m_p;
1621 : 0 : auto unprop = m_u.nprop();
1622 : : auto pnprop = m_p.nprop();
1623 [ - - ]: 0 : for (const auto& [child,parent] : addedTets) {
1624 : : Assert( child < nelem, "Indexing out of new solution vector" );
1625 : : Assert( parent < old_nelem, "Indexing out of old solution vector" );
1626 [ - - ]: 0 : for (std::size_t i=0; i<unprop; ++i) m_u(child,i) = m_un(parent,i);
1627 [ - - ]: 0 : for (std::size_t i=0; i<pnprop; ++i) m_p(child,i) = pn(parent,i);
1628 : : }
1629 : : m_un = m_u;
1630 : :
1631 : : // Resize communication buffers
1632 [ - - ][ - - ]: 0 : m_ghosts[thisIndex].resizeComm();
[ - - ][ - - ]
1633 : 0 : }
1634 : :
1635 : : bool
1636 : 21612 : DG::fieldOutput() const
1637 : : // *****************************************************************************
1638 : : // Decide wether to output field data
1639 : : //! \return True if field data is output in this step
1640 : : // *****************************************************************************
1641 : : {
1642 : 21612 : auto d = Disc();
1643 : :
1644 : : // Output field data
1645 [ + + ][ + - ]: 21612 : return d->fielditer() or d->fieldtime() or d->fieldrange() or d->finished();
[ + - ][ + + ]
1646 : : }
1647 : :
1648 : : bool
1649 : 2198 : DG::refinedOutput() const
1650 : : // *****************************************************************************
1651 : : // Decide if we write field output using a refined mesh
1652 : : //! \return True if field output will use a refined mesh
1653 : : // *****************************************************************************
1654 : : {
1655 [ + + ]: 2198 : return g_inputdeck.get< tag::field_output, tag::refined >() &&
1656 [ - + ]: 33 : g_inputdeck.get< tag::scheme >() != ctr::SchemeType::DG;
1657 : : }
1658 : :
1659 : : void
1660 : 2198 : DG::writeFields(
1661 : : CkCallback c,
1662 : : const std::unordered_map< std::size_t, std::size_t >& addedTets )
1663 : : // *****************************************************************************
1664 : : // Output mesh field data
1665 : : //! \param[in] c Function to continue with after the write
1666 : : //! \param[in] addedTets Newly added mesh cells and their parents (local ids)
1667 : : // *****************************************************************************
1668 : : {
1669 : 2198 : auto d = Disc();
1670 : :
1671 : : const auto& inpoel = std::get< 0 >( m_outmesh.chunk );
1672 : 4396 : auto esup = tk::genEsup( inpoel, 4 );
1673 : 2198 : auto nelem = inpoel.size() / 4;
1674 : :
1675 : : // Combine own and communicated contributions and finish averaging of node
1676 : : // field output in chare boundary nodes
1677 : : const auto& lid = std::get< 2 >( m_outmesh.chunk );
1678 [ + + ]: 89733 : for (const auto& [g,f] : m_uNodefieldsc) {
1679 : : Assert( m_uNodefields.nprop() == f.first.size(), "Size mismatch" );
1680 : 87535 : auto p = tk::cref_find( lid, g );
1681 [ + + ]: 374640 : for (std::size_t i=0; i<f.first.size(); ++i) {
1682 : 287105 : m_uNodefields(p,i) += f.first[i];
1683 : 287105 : m_uNodefields(p,i) /= static_cast< tk::real >(
1684 : 287105 : esup.second[p+1] - esup.second[p] + f.second );
1685 : : }
1686 : : }
1687 : 2198 : tk::destroy( m_uNodefieldsc );
1688 [ + + ]: 89733 : for (const auto& [g,f] : m_pNodefieldsc) {
1689 : : Assert( m_pNodefields.nprop() == f.first.size(), "Size mismatch" );
1690 : 87535 : auto p = tk::cref_find( lid, g );
1691 [ + + ]: 125895 : for (std::size_t i=0; i<f.first.size(); ++i) {
1692 : 38360 : m_pNodefields(p,i) += f.first[i];
1693 : 38360 : m_pNodefields(p,i) /= static_cast< tk::real >(
1694 : 38360 : esup.second[p+1] - esup.second[p] + f.second );
1695 : : }
1696 : : }
1697 : 2198 : tk::destroy( m_pNodefieldsc );
1698 : :
1699 : : // Lambda to decide if a node (global id) is on a chare boundary of the field
1700 : : // output mesh. p - global node id, return true if node is on the chare
1701 : : // boundary.
1702 : : auto chbnd = [ this ]( std::size_t p ) {
1703 : : return
1704 : : std::any_of( m_outmesh.nodeCommMap.cbegin(), m_outmesh.nodeCommMap.cend(),
1705 : : [&](const auto& s) { return s.second.find(p) != s.second.cend(); } );
1706 : : };
1707 : :
1708 : : // Finish computing node field output averages in internal nodes
1709 : 2198 : auto npoin = m_outmesh.coord[0].size();
1710 : : auto& gid = std::get< 1 >( m_outmesh.chunk );
1711 [ + + ]: 409460 : for (std::size_t p=0; p<npoin; ++p) {
1712 [ + + ]: 407262 : if (!chbnd(gid[p])) {
1713 : 319727 : auto n = static_cast< tk::real >( esup.second[p+1] - esup.second[p] );
1714 [ + + ]: 1268653 : for (std::size_t i=0; i<m_uNodefields.nprop(); ++i)
1715 : 948926 : m_uNodefields(p,i) /= n;
1716 [ + + ]: 424041 : for (std::size_t i=0; i<m_pNodefields.nprop(); ++i)
1717 : 104314 : m_pNodefields(p,i) /= n;
1718 : : }
1719 : : }
1720 : :
1721 : : // Collect field output from numerical solution requested by user
1722 : 2198 : auto elemfields = numericFieldOutput( m_uElemfields, tk::Centering::ELEM,
1723 [ + - ][ + - ]: 4396 : g_dgpde[Disc()->MeshId()].OutVarFn(), m_pElemfields );
[ + - ]
1724 : 2198 : auto nodefields = numericFieldOutput( m_uNodefields, tk::Centering::NODE,
1725 [ + - ][ + - ]: 4396 : g_dgpde[Disc()->MeshId()].OutVarFn(), m_pNodefields );
[ + - ]
1726 : :
1727 : : // Collect field output from analytical solutions (if exist)
1728 : 2198 : const auto& coord = m_outmesh.coord;
1729 [ + - ]: 2198 : auto geoElem = tk::genGeoElemTet( inpoel, coord );
1730 [ + - ]: 2198 : auto t = Disc()->T();
1731 [ + - ]: 2198 : analyticFieldOutput( g_dgpde[d->MeshId()], tk::Centering::ELEM,
1732 [ + - ][ + - ]: 8792 : geoElem.extract_comp(1), geoElem.extract_comp(2), geoElem.extract_comp(3),
[ + - ][ + - ]
[ + - ][ + - ]
[ - - ][ - - ]
1733 : : t, elemfields );
1734 [ + - ]: 2198 : analyticFieldOutput( g_dgpde[d->MeshId()], tk::Centering::NODE, coord[0],
1735 : : coord[1], coord[2], t, nodefields );
1736 : :
1737 : : // Add adaptive indicator array to element-centered field output
1738 [ + + ]: 2198 : if (g_inputdeck.get< tag::pref, tag::pref >()) {
1739 [ + - ]: 402 : std::vector< tk::real > ndof( begin(m_ndof), end(m_ndof) );
1740 [ + - ]: 402 : ndof.resize( nelem );
1741 [ + + ]: 182529 : for(std::size_t k = 0; k < nelem; k++) {
1742 : : // Mark the cell with THINC reconstruction as 0 for output
1743 [ + + ]: 182127 : if(m_interface[k] == 1) ndof[k] = 0;
1744 : : }
1745 [ + + ]: 87834 : for (const auto& [child,parent] : addedTets)
1746 : 87432 : ndof[child] = static_cast< tk::real >( m_ndof[parent] );
1747 [ + - ]: 402 : elemfields.push_back( ndof );
1748 : : }
1749 : :
1750 : : // Add shock detection marker array to element-centered field output
1751 [ + - ][ - - ]: 2198 : std::vector< tk::real > shockmarker( begin(m_shockmarker), end(m_shockmarker) );
1752 : : // Here m_shockmarker has a size of m_u.nunk() which is the number of the
1753 : : // elements within this partition (nelem) plus the ghost partition cells. In
1754 : : // terms of output purpose, we only need the solution data within this
1755 : : // partition. Therefore, resizing it to nelem removes the extra partition
1756 : : // boundary allocations in the shockmarker vector. Since the code assumes that
1757 : : // the boundary elements are on the top, the resize operation keeps the lower
1758 : : // portion.
1759 [ + - ]: 2198 : shockmarker.resize( nelem );
1760 [ + + ]: 159710 : for (const auto& [child,parent] : addedTets)
1761 : 157512 : shockmarker[child] = static_cast< tk::real >(m_shockmarker[parent]);
1762 [ + - ]: 2198 : elemfields.push_back( shockmarker );
1763 : :
1764 : : // Add rho0*det(g)/rho to make sure it is staying close to 1,
1765 : : // averaged for all materials
1766 [ + - ][ - - ]: 2198 : std::vector< tk::real > densityConstr(nelem);
1767 [ + - ]: 2198 : g_dgpde[d->MeshId()].computeDensityConstr(nelem, m_u, densityConstr);
1768 [ + + ]: 159710 : for (const auto& [child,parent] : addedTets)
1769 : 157512 : densityConstr[child] = 0.0;
1770 [ + + ][ + - ]: 2198 : if (densityConstr.size() > 0) elemfields.push_back( densityConstr );
1771 : :
1772 : : // Query fields names requested by user
1773 [ + - ]: 4396 : auto elemfieldnames = numericFieldNames( tk::Centering::ELEM );
1774 [ + - ]: 2198 : auto nodefieldnames = numericFieldNames( tk::Centering::NODE );
1775 : :
1776 : : // Collect field output names for analytical solutions
1777 [ + - ]: 2198 : analyticFieldNames( g_dgpde[d->MeshId()], tk::Centering::ELEM, elemfieldnames );
1778 [ + - ]: 2198 : analyticFieldNames( g_dgpde[d->MeshId()], tk::Centering::NODE, nodefieldnames );
1779 : :
1780 [ + + ]: 2198 : if (g_inputdeck.get< tag::pref, tag::pref >()) {
1781 [ + - ]: 804 : elemfieldnames.push_back( "NDOF" );
1782 : : }
1783 : :
1784 [ + - ]: 2198 : elemfieldnames.push_back( "shock_marker" );
1785 : :
1786 [ + + ]: 2198 : if (densityConstr.size() > 0)
1787 [ + - ]: 310 : elemfieldnames.push_back( "density_constraint" );
1788 : :
1789 : : Assert( elemfieldnames.size() == elemfields.size(), "Size mismatch" );
1790 : : Assert( nodefieldnames.size() == nodefields.size(), "Size mismatch" );
1791 : :
1792 : : // Output chare mesh and fields metadata to file
1793 : 2198 : const auto& triinpoel = m_outmesh.triinpoel;
1794 [ + - ][ + - ]: 6594 : d->write( inpoel, m_outmesh.coord, m_outmesh.bface, {},
[ + + ][ - - ]
1795 [ + - ]: 4396 : tk::remap( triinpoel, lid ), elemfieldnames, nodefieldnames,
1796 : : {}, {}, elemfields, nodefields, {}, {}, c );
1797 : 2198 : }
1798 : :
1799 : : void
1800 : 15362 : DG::comnodeout( const std::vector< std::size_t >& gid,
1801 : : const std::vector< std::size_t >& nesup,
1802 : : const std::vector< std::vector< tk::real > >& Lu,
1803 : : const std::vector< std::vector< tk::real > >& Lp )
1804 : : // *****************************************************************************
1805 : : // Receive chare-boundary nodal solution (for field output) contributions from
1806 : : // neighboring chares
1807 : : //! \param[in] gid Global mesh node IDs at which we receive contributions
1808 : : //! \param[in] nesup Number of elements surrounding points
1809 : : //! \param[in] Lu Partial contributions of solution nodal fields to
1810 : : //! chare-boundary nodes
1811 : : //! \param[in] Lp Partial contributions of primitive quantity nodal fields to
1812 : : //! chare-boundary nodes
1813 : : // *****************************************************************************
1814 : : {
1815 : : Assert( gid.size() == nesup.size(), "Size mismatch" );
1816 : : Assert(Lu.size() == m_uNodefields.nprop(), "Fields size mismatch");
1817 : : Assert(Lp.size() == m_pNodefields.nprop(), "Fields size mismatch");
1818 [ + + ]: 78560 : for (std::size_t f=0; f<Lu.size(); ++f)
1819 : : Assert( gid.size() == Lu[f].size(), "Size mismatch" );
1820 [ + + ]: 25630 : for (std::size_t f=0; f<Lp.size(); ++f)
1821 : : Assert( gid.size() == Lp[f].size(), "Size mismatch" );
1822 : :
1823 [ + + ]: 160790 : for (std::size_t i=0; i<gid.size(); ++i) {
1824 : : auto& nfu = m_uNodefieldsc[ gid[i] ];
1825 : 145428 : nfu.first.resize( Lu.size() );
1826 [ + + ]: 717778 : for (std::size_t f=0; f<Lu.size(); ++f) nfu.first[f] += Lu[f][i];
1827 : 145428 : nfu.second += nesup[i];
1828 : 145428 : auto& nfp = m_pNodefieldsc[ gid[i] ];
1829 : 145428 : nfp.first.resize( Lp.size() );
1830 [ + + ]: 242068 : for (std::size_t f=0; f<Lp.size(); ++f) nfp.first[f] += Lp[f][i];
1831 : 145428 : nfp.second += nesup[i];
1832 : : }
1833 : :
1834 : : // When we have heard from all chares we communicate with, this chare is done
1835 [ + + ]: 15362 : if (++m_nnod == Disc()->NodeCommMap().size()) {
1836 : 2024 : m_nnod = 0;
1837 : 2024 : comnodeout_complete();
1838 : : }
1839 : 15362 : }
1840 : :
1841 : : void
1842 : 71805 : DG::stage()
1843 : : // *****************************************************************************
1844 : : // Evaluate whether to continue with next time step stage
1845 : : // *****************************************************************************
1846 : : {
1847 : : // Increment Runge-Kutta stage counter
1848 : 71805 : ++m_stage;
1849 : :
1850 : : // if not all Runge-Kutta stages complete, continue to next time stage,
1851 : : // otherwise prepare for nodal field output
1852 [ + + ]: 71805 : if (m_stage < 3)
1853 : 47870 : next();
1854 : : else
1855 [ + - ][ + - ]: 71805 : startFieldOutput( CkCallback(CkIndex_DG::step(), thisProxy[thisIndex]) );
[ - + ][ - - ]
1856 : 71805 : }
1857 : :
1858 : : void
1859 : 23046 : DG::evalLB( int nrestart )
1860 : : // *****************************************************************************
1861 : : // Evaluate whether to do load balancing
1862 : : //! \param[in] nrestart Number of times restarted
1863 : : // *****************************************************************************
1864 : : {
1865 : 23046 : auto d = Disc();
1866 : :
1867 : : // Detect if just returned from a checkpoint and if so, zero timers
1868 : 23046 : d->restarted( nrestart );
1869 : :
1870 : 23046 : const auto lbfreq = g_inputdeck.get< tag::cmd, tag::lbfreq >();
1871 : 23046 : const auto nonblocking = g_inputdeck.get< tag::cmd, tag::nonblocking >();
1872 : :
1873 : : // Load balancing if user frequency is reached or after the second time-step
1874 [ + + ][ + + ]: 23046 : if ( (d->It()) % lbfreq == 0 || d->It() == 2 ) {
1875 : :
1876 : 19926 : AtSync();
1877 [ - + ]: 19926 : if (nonblocking) next();
1878 : :
1879 : : } else {
1880 : :
1881 : 3120 : next();
1882 : :
1883 : : }
1884 : 23046 : }
1885 : :
1886 : : void
1887 : 23046 : DG::evalRestart()
1888 : : // *****************************************************************************
1889 : : // Evaluate whether to save checkpoint/restart
1890 : : // *****************************************************************************
1891 : : {
1892 : 23046 : auto d = Disc();
1893 : :
1894 : 23046 : const auto rsfreq = g_inputdeck.get< tag::cmd, tag::rsfreq >();
1895 : 23046 : const auto benchmark = g_inputdeck.get< tag::cmd, tag::benchmark >();
1896 : :
1897 [ + + ][ - + ]: 23046 : if (not benchmark and not (d->It() % rsfreq)) {
1898 : :
1899 : 0 : std::vector< std::size_t > meshdata{ /* finished = */ 0, d->MeshId() };
1900 [ - - ]: 0 : contribute( meshdata, CkReduction::nop,
1901 [ - - ][ - - ]: 0 : CkCallback(CkReductionTarget(Transporter,checkpoint), d->Tr()) );
[ - - ]
1902 : :
1903 : : } else {
1904 : :
1905 : 23046 : evalLB( /* nrestart = */ -1 );
1906 : :
1907 : : }
1908 : 23046 : }
1909 : :
1910 : : void
1911 : 23935 : DG::step()
1912 : : // *****************************************************************************
1913 : : // Evaluate wether to continue with next time step
1914 : : // *****************************************************************************
1915 : : {
1916 : 23935 : auto d = Disc();
1917 : :
1918 : : // Output time history
1919 [ + - ][ + - ]: 23935 : if (d->histiter() or d->histtime() or d->histrange()) {
[ - + ]
1920 : 0 : std::vector< std::vector< tk::real > > hist;
1921 [ - - ][ - - ]: 0 : auto h = g_dgpde[d->MeshId()].histOutput( d->Hist(), myGhosts()->m_inpoel,
1922 [ - - ][ - - ]: 0 : myGhosts()->m_coord, m_u, m_p );
1923 [ - - ]: 0 : hist.insert( end(hist), begin(h), end(h) );
1924 [ - - ]: 0 : d->history( std::move(hist) );
1925 : : }
1926 : :
1927 : : // Free memory storing output mesh
1928 : 23935 : m_outmesh.destroy();
1929 : :
1930 : : // Output one-liner status report to screen
1931 : 23935 : d->status();
1932 : : // Reset Runge-Kutta stage counter
1933 : 23935 : m_stage = 0;
1934 : :
1935 : 23935 : const auto term = g_inputdeck.get< tag::term >();
1936 : 23935 : const auto nstep = g_inputdeck.get< tag::nstep >();
1937 : : const auto eps = std::numeric_limits< tk::real >::epsilon();
1938 : :
1939 : : // If neither max iterations nor max time reached, continue, otherwise finish
1940 [ + - ][ + + ]: 23935 : if (std::fabs(d->T()-term) > eps && d->It() < nstep) {
1941 : :
1942 : 23046 : evalRestart();
1943 : :
1944 : : } else {
1945 : :
1946 : 889 : auto meshid = d->MeshId();
1947 [ + - ]: 1778 : d->contribute( sizeof(std::size_t), &meshid, CkReduction::nop,
1948 : 1778 : CkCallback(CkReductionTarget(Transporter,finish), d->Tr()) );
1949 : :
1950 : : }
1951 : 23935 : }
1952 : :
1953 : : void
1954 : 0 : DG::imex_integrate()
1955 : : {
1956 : : /*****************************************************************************
1957 : : Performs the Implicit-Explicit Runge-Kutta step.
1958 : :
1959 : : \details Performs the Implicit-Explicit Runge-Kutta step. Scheme taken from
1960 : : Cavaglieri, D., & Bewley, T. (2015). Low-storage implicit/explicit Runge–Kutta
1961 : : schemes for the simulation of stiff high-dimensional ODE systems. Journal of
1962 : : Computational Physics, 286, 172-193.
1963 : :
1964 : : Scheme given by equations (25a,b):
1965 : :
1966 : : u[0] = u[n] + dt * (expl_rkcoef[1,0]*R_ex(u[n])+impl_rkcoef[1,1]*R_im(u[0]))
1967 : :
1968 : : u[1] = u[n] + dt * (expl_rkcoef[2,1]*R_ex(u[0])+impl_rkcoef[2,1]*R_im(u[0])
1969 : : +impl_rkcoef[2,2]*R_im(u[1]))
1970 : :
1971 : : u[n+1] = u[n] + dt * (expl_rkcoef[3,1]*R_ex(u[0])+impl_rkcoef[3,1]*R_im(u[0])
1972 : : expl_rkcoef[3,2]*R_ex(u[1])+impl_rkcoef[3,2]*R_im(u[1]))
1973 : :
1974 : : In order to solve the first two equations we need to solve a series of systems
1975 : : of non-linear equations:
1976 : :
1977 : : F1(u[0]) = B1 + R1(u[0]) = 0, and
1978 : : F2(u[1]) = B2 + R2(u[1]) = 0,
1979 : :
1980 : : where
1981 : :
1982 : : B1 = u[n] + dt * expl_rkcoef[1,0]*R_ex(u[n]),
1983 : : R1 = dt * impl_rkcoef[1,1]*R_im(u[0]) - u([0]),
1984 : : B2 = u[n] + dt * (expl_rkcoef[2,1]*R_ex(u[0])+impl_rkcoef[2,1]*R_im(u[0])),
1985 : : R2 = dt * impl_rkcoef[2,2]*R_im(u[1]) - u([1]).
1986 : :
1987 : : In order to solve the non-linear system F(U) = 0, we employ Broyden's method.
1988 : : Taken from https://en.wikipedia.org/wiki/Broyden%27s_method.
1989 : : The method consists in obtaining an approximation for the inverse of the
1990 : : Jacobian H = J^(-1) and advancing in a quasi-newton step:
1991 : :
1992 : : U[k+1] = U[k] - H[k]*F(U[k]),
1993 : :
1994 : : until F(U) is close enough to zero.
1995 : :
1996 : : The approximation H[k] is improved at every iteration following
1997 : :
1998 : : H[k] = H[k-1] + (DU[k]-H[k-1]*DF[k])/(DU[k]^T*H[k-1]*DF[k]) * DU[k]^T*H[k-1],
1999 : :
2000 : : where DU[k] = U[k] - U[k-1] and DF[k] = F(U[k]) - F(U[k-1)).
2001 : :
2002 : : This function performs the following main algorithmic steps:
2003 : : - If stage == 0 or stage == 1:
2004 : : - Take Initial value:
2005 : : U[0] = U[n] + dt * expl_rkcoef[1,0]*R_ex(U[n]) (for stage 0)
2006 : : U[1] = U[n] + dt * (expl_rkcoef[2,1]*R_ex(U[0])
2007 : : +impl_rkcoef[2,1]*R_im(U[0])) (for stage 1)
2008 : : - Loop over the Elements (e++)
2009 : : - Initialize Jacobian inverse approximation as the identity
2010 : : - Compute implicit right-hand-side (F_im) with current U
2011 : : - Iterate for the solution (iter++)
2012 : : - Compute new solution U[k+1] = U[k] - H[k]*F(U[k])
2013 : : - Compute implicit right-hand-side (F_im) with current U
2014 : : - Compute DU and DF
2015 : : - Update inverse Jacobian approximation by:
2016 : : - Compute V1 = H[k-1]*DF[k] and V2 = DU[k]^T*H[k-1]
2017 : : - Compute d = DU[k]^T*V1 and V3 = DU[k]-V1
2018 : : - Compute V4 = V3/d
2019 : : - Update H[k] = H[k-1] + V4*V2
2020 : : - Save old U and F
2021 : : - Compute absolute and relative errors
2022 : : - Break iterations if error < tol or iter == max_iter
2023 : : - Update explicit equations using only the explicit terms.
2024 : : - Else if stage == 2:
2025 : : - Update explicit equations using only the explicit terms.
2026 : : - Update implicit equations using:
2027 : : u[n+1] = u[n]+dt*(expl_rkcoef[3,1]*R_ex(u[0])+impl_rkcoef[3,1]*R_im(u[0])
2028 : : expl_rkcoef[3,2]*R_ex(u[1])+impl_rkcoef[3,2]*R_im(u[1]))
2029 : :
2030 : : ******************************************************************************/
2031 : 0 : auto d = Disc();
2032 : 0 : const auto rdof = g_inputdeck.get< tag::rdof >();
2033 : 0 : const auto ndof = g_inputdeck.get< tag::ndof >();
2034 [ - - ]: 0 : if (m_stage < 2) {
2035 : : // Save previous stiff_rhs
2036 : : m_stiffrhsprev = m_stiffrhs;
2037 : :
2038 : : // Compute the imex update
2039 : :
2040 : : // Integrate explicitly on the imex equations
2041 : : // (To use as initial values)
2042 [ - - ]: 0 : for (std::size_t e=0; e<myGhosts()->m_nunk; ++e)
2043 [ - - ]: 0 : for (std::size_t c=0; c<m_nstiffeq; ++c)
2044 : : {
2045 [ - - ]: 0 : for (std::size_t k=0; k<m_numEqDof[c]; ++k)
2046 : : {
2047 [ - - ]: 0 : auto rmark = m_stiffEqIdx[c]*rdof+k;
2048 [ - - ]: 0 : auto mark = m_stiffEqIdx[c]*ndof+k;
2049 : 0 : m_u(e, rmark) = m_un(e, rmark) + d->Dt() * (
2050 : 0 : expl_rkcoef[0][m_stage] * m_rhsprev(e, mark)/m_lhs(e, mark)
2051 : 0 : + expl_rkcoef[1][m_stage] * m_rhs(e, mark)/m_lhs(e, mark)
2052 : 0 : + impl_rkcoef[0][m_stage]
2053 : 0 : * m_stiffrhsprev(e,c*ndof+k)/m_lhs(e, mark) );
2054 [ - - ]: 0 : if(fabs(m_u(e, rmark)) < 1e-16)
2055 : 0 : m_u(e, rmark) = 0;
2056 : : }
2057 : : }
2058 : :
2059 : : // Solve for implicit-explicit equations
2060 : 0 : const auto nelem = myGhosts()->m_fd.Esuel().size()/4;
2061 [ - - ]: 0 : for (std::size_t e=0; e<nelem; ++e)
2062 : : {
2063 : : // Non-linear system f(u) = 0 to be solved
2064 : : // Broyden's method
2065 : : // Control parameters
2066 : 0 : std::size_t max_iter = g_inputdeck.get< tag::imex_maxiter >();
2067 : 0 : tk::real rel_tol = g_inputdeck.get< tag::imex_reltol >();
2068 : 0 : tk::real abs_tol = g_inputdeck.get< tag::imex_abstol >();
2069 : : tk::real rel_err = rel_tol+1;
2070 : : tk::real abs_err = abs_tol+1;
2071 : 0 : std::size_t nstiff = m_nstiffeq*ndof;
2072 : :
2073 : : // Initialize Jacobian to be the identity
2074 : : std::vector< std::vector< tk::real > >
2075 [ - - ][ - - ]: 0 : approx_jacob(nstiff, std::vector< tk::real >(nstiff, 0.0));
2076 [ - - ]: 0 : for (std::size_t i=0; i<nstiff; ++i)
2077 : 0 : approx_jacob[i][i] = 1.0e+00;
2078 : :
2079 : : // Save explicit terms to be re-used
2080 [ - - ]: 0 : std::vector< tk::real > expl_terms(nstiff, 0.0);
2081 [ - - ]: 0 : for (size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2082 [ - - ]: 0 : for (size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2083 : : {
2084 : 0 : auto stiffmark = m_stiffEqIdx[ieq]*ndof+idof;
2085 : 0 : auto stiffrmark = m_stiffEqIdx[ieq]*rdof+idof;
2086 : 0 : expl_terms[ieq*ndof+idof] = m_un(e, stiffrmark)
2087 : 0 : + d->Dt() * ( expl_rkcoef[0][m_stage]
2088 : 0 : * m_rhsprev(e,stiffmark)/m_lhs(e,stiffmark)
2089 : 0 : + expl_rkcoef[1][m_stage]
2090 : 0 : * m_rhs(e,stiffmark)/m_lhs(e,stiffmark)
2091 : 0 : + impl_rkcoef[0][m_stage]
2092 : 0 : * m_stiffrhsprev(e,ieq*ndof+idof)/m_lhs(e,stiffmark) );
2093 : : }
2094 : :
2095 : : // Compute stiff_rhs with initial u
2096 [ - - ][ - - ]: 0 : g_dgpde[d->MeshId()].stiff_rhs( e, myGhosts()->m_geoElem,
2097 [ - - ][ - - ]: 0 : myGhosts()->m_inpoel, myGhosts()->m_coord,
2098 [ - - ]: 0 : m_u, m_p, m_ndof, m_stiffrhs );
2099 : :
2100 : : // Make auxiliary u_old and f_old to store previous values
2101 [ - - ][ - - ]: 0 : std::vector< tk::real > u_old(nstiff, 0.0), f_old(nstiff, 0.0);
[ - - ][ - - ]
2102 : : // Make delta_u and delta_f
2103 [ - - ][ - - ]: 0 : std::vector< tk::real > delta_u(nstiff, 0.0), delta_f(nstiff, 0.0);
[ - - ][ - - ]
2104 : : // Store f
2105 [ - - ][ - - ]: 0 : std::vector< tk::real > f(nstiff, 0.0);
2106 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2107 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2108 : : {
2109 : 0 : auto stiffrmark = m_stiffEqIdx[ieq]*rdof+idof;
2110 : 0 : auto stiffmark = m_stiffEqIdx[ieq]*ndof+idof;
2111 : 0 : f[ieq*ndof+idof] = expl_terms[ieq*ndof+idof]
2112 : 0 : + d->Dt() * impl_rkcoef[1][m_stage]
2113 : 0 : * m_stiffrhs(e,ieq*ndof+idof)/m_lhs(e,stiffmark)
2114 : 0 : - m_u(e, stiffrmark);
2115 : : }
2116 : :
2117 : : // Initialize u_old and f_old
2118 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2119 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2120 : : {
2121 : 0 : u_old[ieq*ndof+idof] = m_u(e, m_stiffEqIdx[ieq]*rdof+idof);
2122 : 0 : f_old[ieq*ndof+idof] = f[ieq*ndof+idof];
2123 : : }
2124 : :
2125 : : // Store the norm of f initially, for relative error measure
2126 : : tk::real err0 = 0.0;
2127 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2128 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2129 : 0 : err0 += f[ieq*ndof+idof]*f[ieq*ndof+idof];
2130 : 0 : err0 = std::sqrt(err0);
2131 : :
2132 : : // Iterate for the solution if err0 > 0
2133 [ - - ]: 0 : if (err0 > abs_tol)
2134 [ - - ]: 0 : for (size_t iter=0; iter<max_iter; ++iter)
2135 : : {
2136 : :
2137 : : // Compute new solution
2138 : : tk::real delta;
2139 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2140 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2141 : : {
2142 : : delta = 0.0;
2143 [ - - ]: 0 : for (std::size_t jeq=0; jeq<m_nstiffeq; ++jeq)
2144 [ - - ]: 0 : for (std::size_t jdof=0; jdof<m_numEqDof[jeq]; ++jdof)
2145 : 0 : delta +=
2146 : 0 : approx_jacob[ieq*ndof+idof][jeq*ndof+jdof] * f[jeq*ndof+jdof];
2147 : : // Update u
2148 : 0 : auto stiffrmark = m_stiffEqIdx[ieq]*rdof+idof;
2149 : 0 : m_u(e, stiffrmark) -= delta;
2150 : : }
2151 : :
2152 : : // Compute new stiff_rhs
2153 [ - - ][ - - ]: 0 : g_dgpde[d->MeshId()].stiff_rhs( e, myGhosts()->m_geoElem,
2154 [ - - ][ - - ]: 0 : myGhosts()->m_inpoel, myGhosts()->m_coord,
[ - - ]
2155 : : m_u, m_p, m_ndof, m_stiffrhs );
2156 : :
2157 : : // Compute new f(u)
2158 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2159 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2160 : : {
2161 : 0 : auto stiffrmark = m_stiffEqIdx[ieq]*rdof+idof;
2162 : 0 : auto stiffmark = m_stiffEqIdx[ieq]*ndof+idof;
2163 : 0 : f[ieq*ndof+idof] = expl_terms[ieq*ndof+idof]
2164 : 0 : + d->Dt() * impl_rkcoef[1][m_stage]
2165 : 0 : * m_stiffrhs(e,ieq*ndof+idof)/m_lhs(e,stiffmark)
2166 : 0 : - m_u(e, stiffrmark);
2167 : : }
2168 : :
2169 : : // Compute delta_u and delta_f
2170 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2171 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2172 : : {
2173 : 0 : auto stiffrmark = m_stiffEqIdx[ieq]*rdof+idof;
2174 : 0 : delta_u[ieq*ndof+idof] = m_u(e, stiffrmark) - u_old[ieq*ndof+idof];
2175 : 0 : delta_f[ieq*ndof+idof] = f[ieq*ndof+idof] - f_old[ieq*ndof+idof];
2176 : : }
2177 : :
2178 : : // Update inverse Jacobian approximation
2179 : :
2180 : : // 1. Compute approx_jacob*delta_f and delta_u*jacob_approx
2181 : : tk::real sum1, sum2;
2182 [ - - ][ - - ]: 0 : std::vector< tk::real > auxvec1(nstiff, 0.0), auxvec2(nstiff, 0.0);
[ - - ][ - - ]
2183 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2184 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2185 : : {
2186 : : sum1 = 0.0;
2187 : : sum2 = 0.0;
2188 [ - - ]: 0 : for (std::size_t jeq=0; jeq<m_nstiffeq; ++jeq)
2189 [ - - ]: 0 : for (std::size_t jdof=0; jdof<m_numEqDof[jeq]; ++jdof)
2190 : : {
2191 : 0 : sum1 += approx_jacob[ieq*ndof+idof][jeq*ndof+jdof] *
2192 : 0 : delta_f[jeq*ndof+jdof];
2193 : 0 : sum2 += delta_u[jeq*ndof+jdof] *
2194 : 0 : approx_jacob[jeq*ndof+jdof][ieq*ndof+idof];
2195 : : }
2196 : 0 : auxvec1[ieq*ndof+idof] = sum1;
2197 : 0 : auxvec2[ieq*ndof+idof] = sum2;
2198 : : }
2199 : :
2200 : : // 2. Compute delta_u*approx_jacob*delta_f
2201 : : // and delta_u-approx_jacob*delta_f
2202 : : tk::real denom = 0.0;
2203 [ - - ]: 0 : for (std::size_t jeq=0; jeq<m_nstiffeq; ++jeq)
2204 [ - - ]: 0 : for (std::size_t jdof=0; jdof<m_numEqDof[jeq]; ++jdof)
2205 : : {
2206 : 0 : denom += delta_u[jeq*ndof+jdof]*auxvec1[jeq*ndof+jdof];
2207 : 0 : auxvec1[jeq*ndof+jdof] =
2208 : 0 : delta_u[jeq*ndof+jdof]-auxvec1[jeq*ndof+jdof];
2209 : : }
2210 : :
2211 : : // 3. Divide delta_u+approx_jacob*delta_f
2212 : : // by delta_u*(approx_jacob*delta_f)
2213 [ - - ]: 0 : if (std::abs(denom) < 1.0e-18)
2214 : : {
2215 [ - - ]: 0 : if (denom < 0.0)
2216 : : {
2217 [ - - ]: 0 : for (std::size_t jeq=0; jeq<m_nstiffeq; ++jeq)
2218 [ - - ]: 0 : for (std::size_t jdof=0; jdof<m_numEqDof[jeq]; ++jdof)
2219 : 0 : auxvec1[jeq*ndof+jdof] /= -1.0e-18;
2220 : : }
2221 : : else
2222 : : {
2223 [ - - ]: 0 : for (std::size_t jeq=0; jeq<m_nstiffeq; ++jeq)
2224 [ - - ]: 0 : for (std::size_t jdof=0; jdof<m_numEqDof[jeq]; ++jdof)
2225 : 0 : auxvec1[jeq*ndof+jdof] /= 1.0e-18;
2226 : : }
2227 : : }
2228 : : else
2229 : : {
2230 [ - - ]: 0 : for (std::size_t jeq=0; jeq<m_nstiffeq; ++jeq)
2231 [ - - ]: 0 : for (std::size_t jdof=0; jdof<m_numEqDof[jeq]; ++jdof)
2232 : 0 : auxvec1[jeq*ndof+jdof] /= denom;
2233 : : }
2234 : :
2235 : : // 4. Perform outter product between the two arrays and
2236 : : // add that quantity to the new jacobian approximation
2237 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2238 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2239 [ - - ]: 0 : for (std::size_t jeq=0; jeq<m_nstiffeq; ++jeq)
2240 [ - - ]: 0 : for (std::size_t jdof=0; jdof<m_numEqDof[jeq]; ++jdof)
2241 : 0 : approx_jacob[ieq*ndof+idof][jeq*ndof+jdof] +=
2242 : 0 : auxvec1[ieq*ndof+idof] * auxvec2[jeq*ndof+jdof];
2243 : :
2244 : : // Save solution and f
2245 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2246 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2247 : : {
2248 : 0 : u_old[ieq*ndof+idof] = m_u(e, m_stiffEqIdx[ieq]*rdof+idof);
2249 : 0 : f_old[ieq*ndof+idof] = f[ieq*ndof+idof];
2250 : : }
2251 : :
2252 : : // Compute a measure of error, use norm of f
2253 : : tk::real err = 0.0;
2254 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2255 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2256 : 0 : err += f[ieq*ndof+idof]*f[ieq*ndof+idof];
2257 : 0 : abs_err = std::sqrt(err);
2258 : 0 : rel_err = abs_err/err0;
2259 : :
2260 : : // Check if error condition is met and loop back
2261 [ - - ][ - - ]: 0 : if (rel_err < rel_tol || abs_err < abs_tol)
2262 : : break;
2263 : :
2264 : : // If we did not converge, print a message
2265 [ - - ]: 0 : if (iter == max_iter-1)
2266 : : {
2267 [ - - ]: 0 : printf("\nIMEX-RK: Non-linear solver did not converge in %lu iterations\n", max_iter);
2268 [ - - ]: 0 : printf("Element #%lu\n", e);
2269 [ - - ]: 0 : printf("Relative error: %e\n", rel_err);
2270 [ - - ]: 0 : printf("Absolute error: %e\n\n", abs_err);
2271 : : }
2272 : : }
2273 : : }
2274 : :
2275 : : // Then, integrate explicitly on the remaining equations
2276 [ - - ]: 0 : for (std::size_t e=0; e<nelem; ++e)
2277 [ - - ]: 0 : for (std::size_t c=0; c<m_nnonstiffeq; ++c)
2278 : : {
2279 [ - - ]: 0 : for (std::size_t k=0; k<m_numEqDof[c]; ++k)
2280 : : {
2281 [ - - ]: 0 : auto rmark = m_nonStiffEqIdx[c]*rdof+k;
2282 [ - - ]: 0 : auto mark = m_nonStiffEqIdx[c]*ndof+k;
2283 : 0 : m_u(e, rmark) = m_un(e, rmark) + d->Dt() * (
2284 : 0 : expl_rkcoef[0][m_stage] * m_rhsprev(e, mark)/m_lhs(e, mark)
2285 : 0 : + expl_rkcoef[1][m_stage] * m_rhs(e, mark)/m_lhs(e, mark));
2286 [ - - ]: 0 : if(fabs(m_u(e, rmark)) < 1e-16)
2287 : 0 : m_u(e, rmark) = 0;
2288 : : }
2289 : : }
2290 : : }
2291 : : else {
2292 : : // For last stage just use all previously computed stages
2293 : 0 : const auto nelem = myGhosts()->m_fd.Esuel().size()/4;
2294 [ - - ]: 0 : for (std::size_t e=0; e<nelem; ++e)
2295 : : {
2296 : : // First integrate explicitly on nonstiff equations
2297 [ - - ]: 0 : for (std::size_t c=0; c<m_nnonstiffeq; ++c)
2298 : : {
2299 [ - - ]: 0 : for (std::size_t k=0; k<m_numEqDof[c]; ++k)
2300 : : {
2301 [ - - ]: 0 : auto rmark = m_nonStiffEqIdx[c]*rdof+k;
2302 [ - - ]: 0 : auto mark = m_nonStiffEqIdx[c]*ndof+k;
2303 : 0 : m_u(e, rmark) = m_un(e, rmark) + d->Dt() * (
2304 : 0 : expl_rkcoef[0][m_stage] * m_rhsprev(e, mark)/m_lhs(e, mark)
2305 : 0 : + expl_rkcoef[1][m_stage] * m_rhs(e, mark)/m_lhs(e, mark));
2306 [ - - ]: 0 : if(fabs(m_u(e, rmark)) < 1e-16)
2307 : 0 : m_u(e, rmark) = 0;
2308 : : }
2309 : : }
2310 : : // Then, integrate the imex-equations
2311 [ - - ]: 0 : for (std::size_t ieq=0; ieq<m_nstiffeq; ++ieq)
2312 [ - - ]: 0 : for (std::size_t idof=0; idof<m_numEqDof[ieq]; ++idof)
2313 : : {
2314 [ - - ]: 0 : auto rmark = m_stiffEqIdx[ieq]*rdof+idof;
2315 [ - - ]: 0 : auto mark = m_stiffEqIdx[ieq]*ndof+idof;
2316 : 0 : m_u(e, rmark) = m_un(e, rmark)
2317 : 0 : + d->Dt() * (expl_rkcoef[0][m_stage]
2318 : 0 : * m_rhsprev(e,mark)/m_lhs(e,mark)
2319 : 0 : + expl_rkcoef[1][m_stage]
2320 : 0 : * m_rhs(e,mark)/m_lhs(e,mark)
2321 : 0 : + impl_rkcoef[0][m_stage]
2322 : 0 : * m_stiffrhsprev(e,ieq*ndof+idof)/m_lhs(e,mark)
2323 : 0 : + impl_rkcoef[1][m_stage]
2324 : 0 : * m_stiffrhs(e,ieq*ndof+idof)/m_lhs(e,mark) );
2325 [ - - ]: 0 : if(fabs(m_u(e, rmark)) < 1e-16)
2326 : 0 : m_u(e, rmark) = 0;
2327 : : }
2328 : : }
2329 : : }
2330 : 0 : }
2331 : :
2332 : : #include "NoWarning/dg.def.h"
|