Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/PDE/FVPDE.hpp
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 Partial differential equation base for finite volume PDEs
9 : : \details This file defines a generic partial differential equation (PDE)
10 : : class for PDEs that use finite volume spatial discretization.
11 : : The class uses runtime polymorphism without client-side inheritance:
12 : : inheritance is confined to the internals of the class, invisible to
13 : : client-code. The class exclusively deals with ownership enabling client-side
14 : : value semantics. Credit goes to Sean Parent at Adobe:
15 : : https://github.com/sean-parent/sean-parent.github.com/wiki/
16 : : Papers-and-Presentations.
17 : : */
18 : : // *****************************************************************************
19 : : #ifndef FVPDE_h
20 : : #define FVPDE_h
21 : :
22 : : #include <array>
23 : : #include <string>
24 : : #include <vector>
25 : : #include <memory>
26 : : #include <unordered_set>
27 : : #include <functional>
28 : :
29 : : #include "Types.hpp"
30 : : #include "Fields.hpp"
31 : : #include "FaceData.hpp"
32 : : #include "UnsMesh.hpp"
33 : : #include "Inciter/InputDeck/InputDeck.hpp"
34 : : #include "FunctionPrototypes.hpp"
35 : : #include "History.hpp"
36 : :
37 : : namespace inciter {
38 : :
39 : : extern ctr::InputDeck g_inputdeck;
40 : :
41 : : using ncomp_t = tk::ncomp_t;
42 : :
43 : : //! \brief Partial differential equation base for discontinuous Galerkin PDEs
44 : : //! \details This class uses runtime polymorphism without client-side
45 : : //! inheritance: inheritance is confined to the internals of the this class,
46 : : //! invisible to client-code. The class exclusively deals with ownership
47 : : //! enabling client-side value semantics. Credit goes to Sean Parent at Adobe:
48 : : //! https://github.com/sean-parent/sean-parent.github.com/wiki/
49 : : //! Papers-and-Presentations. For example client code that models a FVPDE,
50 : : //! see inciter::CompFlow.
51 : : class FVPDE {
52 : :
53 : : private:
54 : : using ncomp_t = tk::ncomp_t;
55 : :
56 : : public:
57 : : //! Default constructor taking no arguments for Charm++
58 : 0 : explicit FVPDE() = default;
59 : :
60 : : //! \brief Constructor taking an object modeling Concept.
61 : : //! \details The object of class T comes pre-constructed.
62 : : //! \param[in] x Instantiated object of type T given by the template
63 : : //! argument.
64 : : template< typename T > explicit FVPDE( T x ) :
65 : : self( std::make_unique< Model<T> >( std::move(x) ) ) {}
66 : :
67 : : //! \brief Constructor taking a function pointer to a constructor of an
68 : : //! object modeling Concept.
69 : : //! \details Passing std::function allows late execution of the constructor,
70 : : //! i.e., as late as inside this class' constructor, and thus usage from
71 : : //! a factory. Note that there are at least two different ways of using
72 : : //! this constructor:
73 : : //! - Bind T's constructor arguments and place it in std::function<T()>
74 : : //! and passing no arguments as args.... This case then instantiates the
75 : : //! model via its constructor and stores it in here.
76 : : //! - Bind a single placeholder argument to T's constructor and pass it in
77 : : //! as host's args..., which then forwards it to model's constructor. This
78 : : //! allows late binding, i.e., binding the argument only here.
79 : : //! \see See also the wrapper tk::recordModel() which does the former and
80 : : //! tk::recordModelLate() which does the latter, both defined in
81 : : //! src/Base/Factory.h.
82 : : //! \param[in] x Function pointer to a constructor of an object modeling
83 : : //! Concept.
84 : : //! \param[in] args Zero or more constructor arguments
85 : : template< typename T, typename...Args >
86 : 95 : explicit FVPDE( std::function<T(Args...)> x, Args&&... args ) :
87 : : self( std::make_unique< Model<T> >(
88 [ + - ]: 95 : std::move( x( std::forward<Args>(args)... ) ) ) ) {}
89 : :
90 : : //! Public interface to find number of primitive quantities for the diff eq
91 : 299 : std::size_t nprim() const
92 : 299 : { return self->nprim(); }
93 : :
94 : : //! Public interface to find number of materials for the diff eq
95 : : std::size_t nmat() const
96 : : { return self->nmat(); }
97 : :
98 : : //! Public interface to determine elements that lie inside the IC box
99 : 299 : void IcBoxElems( const tk::Fields& geoElem,
100 : : std::size_t nielem,
101 : : std::vector< std::unordered_set< std::size_t > >& inbox ) const
102 : 299 : { self->IcBoxElems( geoElem, nielem, inbox ); }
103 : :
104 : : //! Public interface to setting the initial conditions for the diff eq
105 : 299 : void initialize(
106 : : const tk::Fields& L,
107 : : const std::vector< std::size_t >& inpoel,
108 : : const tk::UnsMesh::Coords& coord,
109 : : const std::vector< std::unordered_set< std::size_t > >& inbox,
110 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&
111 : : elemblkid,
112 : : tk::Fields& unk,
113 : : tk::real t,
114 : : const std::size_t nielem ) const
115 : 299 : { self->initialize( L, inpoel, coord, inbox, elemblkid, unk, t, nielem ); }
116 : :
117 : : //! Public interface to updating the primitives for the diff eq
118 : 1667 : void updatePrimitives( const tk::Fields& unk,
119 : : tk::Fields& prim,
120 : : std::size_t nielem ) const
121 : 1667 : { self->updatePrimitives( unk, prim, nielem ); }
122 : :
123 : : //! Public interface to cleaning up trace materials for the diff eq
124 : 1368 : void cleanTraceMaterial( tk::real t,
125 : : const tk::Fields& geoElem,
126 : : tk::Fields& unk,
127 : : tk::Fields& prim,
128 : : std::size_t nielem ) const
129 : 1368 : { self->cleanTraceMaterial( t, geoElem, unk, prim, nielem ); }
130 : :
131 : : //! Public interface to reconstructing the second-order solution
132 : 1368 : void reconstruct( const tk::Fields& geoElem,
133 : : const inciter::FaceData& fd,
134 : : const std::map< std::size_t, std::vector< std::size_t > >&
135 : : esup,
136 : : const std::vector< std::size_t >& inpoel,
137 : : const tk::UnsMesh::Coords& coord,
138 : : tk::Fields& U,
139 : : tk::Fields& P ) const
140 : : {
141 : 1368 : self->reconstruct( geoElem, fd, esup, inpoel, coord, U, P );
142 : 1368 : }
143 : :
144 : : //! Public interface to limiting the second-order solution
145 : 1368 : void limit( const tk::Fields& geoFace,
146 : : const inciter::FaceData& fd,
147 : : const std::map< std::size_t, std::vector< std::size_t > >& esup,
148 : : const std::vector< std::size_t >& inpoel,
149 : : const tk::UnsMesh::Coords& coord,
150 : : const std::vector< int >& srcFlag,
151 : : tk::Fields& U,
152 : : tk::Fields& P ) const
153 : : {
154 : 1368 : self->limit( geoFace, fd, esup, inpoel, coord, srcFlag, U, P );
155 : 1368 : }
156 : :
157 : : //! Public interface to computing the P1 right-hand side vector
158 : 1368 : void rhs( tk::real t,
159 : : const tk::Fields& geoFace,
160 : : const tk::Fields& geoElem,
161 : : const inciter::FaceData& fd,
162 : : const std::vector< std::size_t >& inpoel,
163 : : const tk::UnsMesh::Coords& coord,
164 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&
165 : : elemblkid,
166 : : const tk::Fields& U,
167 : : const tk::Fields& P,
168 : : tk::Fields& R,
169 : : std::vector< int >& srcFlag ) const
170 : : {
171 : 1368 : self->rhs( t, geoFace, geoElem, fd, inpoel, coord, elemblkid, U, P, R,
172 : 1368 : srcFlag );
173 : 1368 : }
174 : :
175 : : //! Public interface for computing the minimum time step size
176 : 100 : tk::real dt( const inciter::FaceData& fd,
177 : : const tk::Fields& geoFace,
178 : : const tk::Fields& geoElem,
179 : : const tk::Fields& U,
180 : : const tk::Fields& P,
181 : : const std::size_t nielem,
182 : : const std::vector< int >& srcFlag,
183 : : std::vector< tk::real >& local_dte ) const
184 : 100 : { return self->dt( fd, geoFace, geoElem, U, P, nielem, srcFlag, local_dte );
185 : : }
186 : :
187 : : //! Public interface to returning maps of output var functions
188 : 118 : std::map< std::string, tk::GetVarFn > OutVarFn() const
189 : 118 : { return self->OutVarFn(); }
190 : :
191 : : //! Public interface to returning analytic field output labels
192 : 0 : std::vector< std::string > analyticFieldNames() const
193 : 0 : { return self->analyticFieldNames(); }
194 : :
195 : : //! Public interface to returning surface output labels
196 : 59 : std::vector< std::string > surfNames() const { return self->surfNames(); }
197 : :
198 : : //! Public interface to returning time history field output labels
199 : 0 : std::vector< std::string > histNames() const { return self->histNames(); }
200 : :
201 : : //! Public interface to returning variable names
202 : 22 : std::vector< std::string > names() const { return self->names(); }
203 : :
204 : : //! Public interface to returning surface field output
205 : : std::vector< std::vector< tk::real > >
206 : 59 : surfOutput( const inciter::FaceData& fd,
207 : : const tk::Fields& U,
208 : : const tk::Fields& P ) const
209 : 59 : { return self->surfOutput( fd, U, P ); }
210 : :
211 : : //! Public interface to return point history output
212 : : std::vector< std::vector< tk::real > >
213 : 0 : histOutput( const std::vector< HistData >& h,
214 : : const std::vector< std::size_t >& inpoel,
215 : : const tk::UnsMesh::Coords& coord,
216 : : const tk::Fields& U,
217 : : const tk::Fields& P ) const
218 : 0 : { return self->histOutput( h, inpoel, coord, U, P ); }
219 : :
220 : : //! Public interface to returning analytic solution
221 : : tk::InitializeFn::result_type
222 : 0 : analyticSolution( tk::real xi, tk::real yi, tk::real zi, tk::real t ) const
223 : 0 : { return self->analyticSolution( xi, yi, zi, t ); }
224 : :
225 : : //! Public interface to returning the analytic solution for conserved vars
226 : : tk::InitializeFn::result_type
227 : : solution( tk::real xi, tk::real yi, tk::real zi, tk::real t ) const
228 : : { return self->solution( xi, yi, zi, t ); }
229 : :
230 : : //! Public interface to returning the specific total energy
231 : : tk::real
232 : : sp_totalenergy( std::size_t e, const tk::Fields& unk ) const
233 : : { return self->sp_totalenergy( e, unk ); }
234 : :
235 : : //! Public interface to returning the relevant sound speed in each cell
236 : : void
237 : 59 : soundspeed(
238 : : std::size_t nielem,
239 : : const tk::Fields& U,
240 : : const tk::Fields& P,
241 : : std::vector< tk::real >& ss ) const
242 : 59 : { return self->soundspeed( nielem, U, P, ss ); }
243 : :
244 : : //! Copy assignment
245 : : FVPDE& operator=( const FVPDE& x )
246 : : { FVPDE tmp(x); *this = std::move(tmp); return *this; }
247 : : //! Copy constructor
248 : : FVPDE( const FVPDE& x ) : self( x.self->copy() ) {}
249 : : //! Move assignment
250 : : FVPDE& operator=( FVPDE&& ) noexcept = default;
251 : : //! Move constructor
252 : 95 : FVPDE( FVPDE&& ) noexcept = default;
253 : :
254 : : private:
255 : : //! \brief Concept is a pure virtual base class specifying the requirements
256 : : //! of polymorphic objects deriving from it
257 : : struct Concept {
258 : 95 : Concept() = default;
259 : 0 : Concept( const Concept& ) = default;
260 : 95 : virtual ~Concept() = default;
261 : : virtual Concept* copy() const = 0;
262 : : virtual std::size_t nprim() const = 0;
263 : : virtual std::size_t nmat() const = 0;
264 : : virtual void IcBoxElems( const tk::Fields&,
265 : : std::size_t,
266 : : std::vector< std::unordered_set< std::size_t > >& ) const = 0;
267 : : virtual void initialize(
268 : : const tk::Fields&,
269 : : const std::vector< std::size_t >&,
270 : : const tk::UnsMesh::Coords&,
271 : : const std::vector< std::unordered_set< std::size_t > >&,
272 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&,
273 : : tk::Fields&,
274 : : tk::real,
275 : : const std::size_t nielem ) const = 0;
276 : : virtual void updatePrimitives( const tk::Fields&,
277 : : tk::Fields&,
278 : : std::size_t ) const = 0;
279 : : virtual void cleanTraceMaterial( tk::real,
280 : : const tk::Fields&,
281 : : tk::Fields&,
282 : : tk::Fields&,
283 : : std::size_t ) const = 0;
284 : : virtual void reconstruct( const tk::Fields&,
285 : : const inciter::FaceData&,
286 : : const std::map< std::size_t,
287 : : std::vector< std::size_t > >&,
288 : : const std::vector< std::size_t >&,
289 : : const tk::UnsMesh::Coords&,
290 : : tk::Fields&,
291 : : tk::Fields& ) const = 0;
292 : : virtual void limit( const tk::Fields&,
293 : : const inciter::FaceData&,
294 : : const std::map< std::size_t,
295 : : std::vector< std::size_t > >&,
296 : : const std::vector< std::size_t >&,
297 : : const tk::UnsMesh::Coords&,
298 : : const std::vector< int >&,
299 : : tk::Fields&,
300 : : tk::Fields& ) const = 0;
301 : : virtual void rhs( tk::real,
302 : : const tk::Fields&,
303 : : const tk::Fields&,
304 : : const inciter::FaceData&,
305 : : const std::vector< std::size_t >&,
306 : : const tk::UnsMesh::Coords&,
307 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&,
308 : : const tk::Fields&,
309 : : const tk::Fields&,
310 : : tk::Fields&,
311 : : std::vector< int >& ) const = 0;
312 : : virtual tk::real dt( const inciter::FaceData&,
313 : : const tk::Fields&,
314 : : const tk::Fields&,
315 : : const tk::Fields&,
316 : : const tk::Fields&,
317 : : const std::size_t,
318 : : const std::vector< int >&,
319 : : std::vector< tk::real >& ) const = 0;
320 : : virtual std::map< std::string, tk::GetVarFn > OutVarFn() const = 0;
321 : : virtual std::vector< std::string > analyticFieldNames() const = 0;
322 : : virtual std::vector< std::string > surfNames() const = 0;
323 : : virtual std::vector< std::string > histNames() const = 0;
324 : : virtual std::vector< std::string > names() const = 0;
325 : : virtual std::vector< std::vector< tk::real > > surfOutput(
326 : : const inciter::FaceData&,
327 : : const tk::Fields&,
328 : : const tk::Fields& ) const = 0;
329 : : virtual std::vector< std::vector< tk::real > > histOutput(
330 : : const std::vector< HistData >&,
331 : : const std::vector< std::size_t >&,
332 : : const tk::UnsMesh::Coords&,
333 : : const tk::Fields&,
334 : : const tk::Fields& ) const = 0;
335 : : virtual tk::InitializeFn::result_type analyticSolution(
336 : : tk::real xi, tk::real yi, tk::real zi, tk::real t ) const = 0;
337 : : virtual tk::InitializeFn::result_type solution(
338 : : tk::real xi, tk::real yi, tk::real zi, tk::real t ) const = 0;
339 : : virtual tk::real sp_totalenergy(
340 : : std::size_t, const tk::Fields& ) const = 0;
341 : : virtual void soundspeed(
342 : : std::size_t,
343 : : const tk::Fields&,
344 : : const tk::Fields&,
345 : : std::vector< tk::real >& ) const = 0;
346 : : };
347 : :
348 : : //! \brief Model models the Concept above by deriving from it and overriding
349 : : //! the virtual functions required by Concept
350 : : template< typename T >
351 : : struct Model : Concept {
352 : 95 : explicit Model( T x ) : data( std::move(x) ) {}
353 [ - - ]: 0 : Concept* copy() const override { return new Model( *this ); }
354 : 299 : std::size_t nprim() const override
355 : 299 : { return data.nprim(); }
356 : 0 : std::size_t nmat() const override
357 : 0 : { return data.nmat(); }
358 : 299 : void IcBoxElems( const tk::Fields& geoElem,
359 : : std::size_t nielem,
360 : : std::vector< std::unordered_set< std::size_t > >& inbox )
361 : 299 : const override { data.IcBoxElems( geoElem, nielem, inbox ); }
362 : 299 : void initialize(
363 : : const tk::Fields& L,
364 : : const std::vector< std::size_t >& inpoel,
365 : : const tk::UnsMesh::Coords& coord,
366 : : const std::vector< std::unordered_set< std::size_t > >& inbox,
367 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&
368 : : elemblkid,
369 : : tk::Fields& unk,
370 : : tk::real t,
371 : : const std::size_t nielem )
372 : 299 : const override { data.initialize( L, inpoel, coord, inbox, elemblkid, unk,
373 : 299 : t, nielem ); }
374 : 1667 : void updatePrimitives( const tk::Fields& unk,
375 : : tk::Fields& prim,
376 : : std::size_t nielem )
377 : 1667 : const override { data.updatePrimitives( unk, prim, nielem ); }
378 : 1368 : void cleanTraceMaterial( tk::real t,
379 : : const tk::Fields& geoElem,
380 : : tk::Fields& unk,
381 : : tk::Fields& prim,
382 : : std::size_t nielem )
383 : 1368 : const override { data.cleanTraceMaterial( t, geoElem, unk, prim, nielem ); }
384 : 1368 : void reconstruct( const tk::Fields& geoElem,
385 : : const inciter::FaceData& fd,
386 : : const std::map< std::size_t,
387 : : std::vector< std::size_t > >& esup,
388 : : const std::vector< std::size_t >& inpoel,
389 : : const tk::UnsMesh::Coords& coord,
390 : : tk::Fields& U,
391 : : tk::Fields& P ) const override
392 : : {
393 : 1368 : data.reconstruct( geoElem, fd, esup, inpoel, coord, U, P );
394 : 1368 : }
395 : 1368 : void limit( const tk::Fields& geoFace,
396 : : const inciter::FaceData& fd,
397 : : const std::map< std::size_t, std::vector< std::size_t > >&
398 : : esup,
399 : : const std::vector< std::size_t >& inpoel,
400 : : const tk::UnsMesh::Coords& coord,
401 : : const std::vector< int >& srcFlag,
402 : : tk::Fields& U,
403 : : tk::Fields& P ) const override
404 : : {
405 : 1368 : data.limit( geoFace, fd, esup, inpoel, coord, srcFlag, U, P );
406 : 1368 : }
407 : 1368 : void rhs(
408 : : tk::real t,
409 : : const tk::Fields& geoFace,
410 : : const tk::Fields& geoElem,
411 : : const inciter::FaceData& fd,
412 : : const std::vector< std::size_t >& inpoel,
413 : : const tk::UnsMesh::Coords& coord,
414 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&
415 : : elemblkid,
416 : : const tk::Fields& U,
417 : : const tk::Fields& P,
418 : : tk::Fields& R,
419 : : std::vector< int >& srcFlag ) const override
420 : : {
421 : 1368 : data.rhs( t, geoFace, geoElem, fd, inpoel, coord, elemblkid, U, P, R,
422 : : srcFlag );
423 : 1368 : }
424 : 100 : tk::real dt( const inciter::FaceData& fd,
425 : : const tk::Fields& geoFace,
426 : : const tk::Fields& geoElem,
427 : : const tk::Fields& U,
428 : : const tk::Fields& P,
429 : : const std::size_t nielem,
430 : : const std::vector< int >& srcFlag,
431 : : std::vector< tk::real >& local_dte ) const override
432 : : { return data.dt( fd, geoFace, geoElem, U, P, nielem, srcFlag,
433 : 100 : local_dte ); }
434 : 118 : std::map< std::string, tk::GetVarFn > OutVarFn() const override
435 : 118 : { return data.OutVarFn(); }
436 : 0 : std::vector< std::string > analyticFieldNames() const override
437 : 0 : { return data.analyticFieldNames(); }
438 : 59 : std::vector< std::string > surfNames() const override
439 : 59 : { return data.surfNames(); }
440 : 0 : std::vector< std::string > histNames() const override
441 : 0 : { return data.histNames(); }
442 : 22 : std::vector< std::string > names() const override
443 : 22 : { return data.names(); }
444 : 59 : std::vector< std::vector< tk::real > > surfOutput(
445 : : const inciter::FaceData& fd,
446 : : const tk::Fields& U,
447 : : const tk::Fields& P ) const override
448 : 59 : { return data.surfOutput( fd, U, P ); }
449 : 0 : std::vector< std::vector< tk::real > > histOutput(
450 : : const std::vector< HistData >& h,
451 : : const std::vector< std::size_t >& inpoel,
452 : : const tk::UnsMesh::Coords& coord,
453 : : const tk::Fields& U,
454 : : const tk::Fields& P ) const override
455 : 0 : { return data.histOutput( h, inpoel, coord, U, P ); }
456 : : tk::InitializeFn::result_type
457 : 0 : analyticSolution( tk::real xi, tk::real yi, tk::real zi, tk::real t )
458 : 0 : const override { return data.analyticSolution( xi, yi, zi, t ); }
459 : : tk::InitializeFn::result_type
460 : 0 : solution( tk::real xi, tk::real yi, tk::real zi, tk::real t )
461 : 0 : const override { return data.solution( xi, yi, zi, t ); }
462 : 0 : tk::real sp_totalenergy( std::size_t e, const tk::Fields& unk )
463 : 0 : const override { return data.sp_totalenergy( e, unk ); }
464 : 59 : void soundspeed(
465 : : std::size_t nielem,
466 : : const tk::Fields& U,
467 : : const tk::Fields& P,
468 : : std::vector< tk::real >& ss )
469 : 59 : const override { return data.soundspeed( nielem, U, P, ss ); }
470 : : T data;
471 : : };
472 : :
473 : : std::unique_ptr< Concept > self; //!< Base pointer used polymorphically
474 : : };
475 : :
476 : : } // inciter::
477 : :
478 : : #endif // FVPDE_h
|