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 : 95 : class FVPDE {
52 : :
53 : : private:
54 : : using ncomp_t = tk::ncomp_t;
55 : :
56 : : public:
57 : : //! Default constructor taking no arguments for Charm++
58 : : 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 : : 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 : : 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 : : 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 : : 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 : : 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 : : 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 : : 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 std::vector< int >& srcFlag,
150 : : tk::Fields& U,
151 : : tk::Fields& P ) const
152 : : {
153 : 1368 : self->limit( geoFace, fd, esup, inpoel, srcFlag, U, P );
154 : 1368 : }
155 : :
156 : : //! Public interface to computing the P1 right-hand side vector
157 : : void rhs( tk::real t,
158 : : const tk::Fields& geoFace,
159 : : const tk::Fields& geoElem,
160 : : const inciter::FaceData& fd,
161 : : const std::vector< std::size_t >& inpoel,
162 : : const tk::UnsMesh::Coords& coord,
163 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&
164 : : elemblkid,
165 : : const tk::Fields& U,
166 : : const tk::Fields& P,
167 : : tk::Fields& R,
168 : : std::vector< int >& srcFlag ) const
169 : : {
170 : 1368 : self->rhs( t, geoFace, geoElem, fd, inpoel, coord, elemblkid, U, P, R,
171 : 1368 : srcFlag );
172 : : }
173 : :
174 : : //! Public interface for computing the minimum time step size
175 : : tk::real dt( const inciter::FaceData& fd,
176 : : const tk::Fields& geoFace,
177 : : const tk::Fields& geoElem,
178 : : const tk::Fields& U,
179 : : const tk::Fields& P,
180 : : const std::size_t nielem,
181 : : const std::vector< int >& srcFlag,
182 : : std::vector< tk::real >& local_dte ) const
183 : 100 : { return self->dt( fd, geoFace, geoElem, U, P, nielem, srcFlag, local_dte );
184 : : }
185 : :
186 : : //! Public interface to returning maps of output var functions
187 : : std::map< std::string, tk::GetVarFn > OutVarFn() const
188 [ + - ][ + - ]: 118 : { return self->OutVarFn(); }
189 : :
190 : : //! Public interface to returning analytic field output labels
191 : : std::vector< std::string > analyticFieldNames() const
192 : 0 : { return self->analyticFieldNames(); }
193 : :
194 : : //! Public interface to returning surface output labels
195 [ + - ]: 59 : std::vector< std::string > surfNames() const { return self->surfNames(); }
196 : :
197 : : //! Public interface to returning time history field output labels
198 [ - - ]: 0 : std::vector< std::string > histNames() const { return self->histNames(); }
199 : :
200 : : //! Public interface to returning variable names
201 : : std::vector< std::string > names() const { return self->names(); }
202 : :
203 : : //! Public interface to returning surface field output
204 : : std::vector< std::vector< tk::real > >
205 : : surfOutput( const inciter::FaceData& fd,
206 : : const tk::Fields& U,
207 : : const tk::Fields& P ) const
208 [ + - ]: 59 : { return self->surfOutput( fd, U, P ); }
209 : :
210 : : //! Public interface to return point history output
211 : : std::vector< std::vector< tk::real > >
212 : : histOutput( const std::vector< HistData >& h,
213 : : const std::vector< std::size_t >& inpoel,
214 : : const tk::UnsMesh::Coords& coord,
215 : : const tk::Fields& U,
216 : : const tk::Fields& P ) const
217 [ - - ]: 0 : { return self->histOutput( h, inpoel, coord, U, P ); }
218 : :
219 : : //! Public interface to returning analytic solution
220 : : tk::InitializeFn::result_type
221 : : analyticSolution( tk::real xi, tk::real yi, tk::real zi, tk::real t ) const
222 : 0 : { return self->analyticSolution( xi, yi, zi, t ); }
223 : :
224 : : //! Public interface to returning the analytic solution for conserved vars
225 : : tk::InitializeFn::result_type
226 : : solution( tk::real xi, tk::real yi, tk::real zi, tk::real t ) const
227 : : { return self->solution( xi, yi, zi, t ); }
228 : :
229 : : //! Public interface to returning the specific total energy
230 : : tk::real
231 : : sp_totalenergy( std::size_t e, const tk::Fields& unk ) const
232 : : { return self->sp_totalenergy( e, unk ); }
233 : :
234 : : //! Public interface to returning the relevant sound speed in each cell
235 : : void
236 : : soundspeed(
237 : : std::size_t nielem,
238 : : const tk::Fields& U,
239 : : const tk::Fields& P,
240 : : std::vector< tk::real >& ss ) const
241 [ + - ]: 59 : { return self->soundspeed( nielem, U, P, ss ); }
242 : :
243 : : //! Copy assignment
244 : : FVPDE& operator=( const FVPDE& x )
245 : : { FVPDE tmp(x); *this = std::move(tmp); return *this; }
246 : : //! Copy constructor
247 : : FVPDE( const FVPDE& x ) : self( x.self->copy() ) {}
248 : : //! Move assignment
249 : : FVPDE& operator=( FVPDE&& ) noexcept = default;
250 : : //! Move constructor
251 [ - - ]: 0 : FVPDE( FVPDE&& ) noexcept = default;
252 : :
253 : : private:
254 : : //! \brief Concept is a pure virtual base class specifying the requirements
255 : : //! of polymorphic objects deriving from it
256 : : struct Concept {
257 : : Concept() = default;
258 : 0 : Concept( const Concept& ) = default;
259 : : virtual ~Concept() = default;
260 : : virtual Concept* copy() const = 0;
261 : : virtual std::size_t nprim() const = 0;
262 : : virtual std::size_t nmat() const = 0;
263 : : virtual void IcBoxElems( const tk::Fields&,
264 : : std::size_t,
265 : : std::vector< std::unordered_set< std::size_t > >& ) const = 0;
266 : : virtual void initialize(
267 : : const tk::Fields&,
268 : : const std::vector< std::size_t >&,
269 : : const tk::UnsMesh::Coords&,
270 : : const std::vector< std::unordered_set< std::size_t > >&,
271 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&,
272 : : tk::Fields&,
273 : : tk::real,
274 : : const std::size_t nielem ) const = 0;
275 : : virtual void updatePrimitives( const tk::Fields&,
276 : : tk::Fields&,
277 : : std::size_t ) const = 0;
278 : : virtual void cleanTraceMaterial( tk::real,
279 : : const tk::Fields&,
280 : : tk::Fields&,
281 : : tk::Fields&,
282 : : std::size_t ) const = 0;
283 : : virtual void reconstruct( const tk::Fields&,
284 : : const inciter::FaceData&,
285 : : const std::map< std::size_t,
286 : : std::vector< std::size_t > >&,
287 : : const std::vector< std::size_t >&,
288 : : const tk::UnsMesh::Coords&,
289 : : tk::Fields&,
290 : : tk::Fields& ) const = 0;
291 : : virtual void limit( const tk::Fields&,
292 : : const inciter::FaceData&,
293 : : const std::map< std::size_t,
294 : : std::vector< std::size_t > >&,
295 : : const std::vector< std::size_t >&,
296 : : const std::vector< int >&,
297 : : tk::Fields&,
298 : : tk::Fields& ) const = 0;
299 : : virtual void rhs( tk::real,
300 : : const tk::Fields&,
301 : : const tk::Fields&,
302 : : const inciter::FaceData&,
303 : : const std::vector< std::size_t >&,
304 : : const tk::UnsMesh::Coords&,
305 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&,
306 : : const tk::Fields&,
307 : : const tk::Fields&,
308 : : tk::Fields&,
309 : : std::vector< int >& ) const = 0;
310 : : virtual tk::real dt( const inciter::FaceData&,
311 : : const tk::Fields&,
312 : : const tk::Fields&,
313 : : const tk::Fields&,
314 : : const tk::Fields&,
315 : : const std::size_t,
316 : : const std::vector< int >&,
317 : : std::vector< tk::real >& ) const = 0;
318 : : virtual std::map< std::string, tk::GetVarFn > OutVarFn() const = 0;
319 : : virtual std::vector< std::string > analyticFieldNames() const = 0;
320 : : virtual std::vector< std::string > surfNames() const = 0;
321 : : virtual std::vector< std::string > histNames() const = 0;
322 : : virtual std::vector< std::string > names() const = 0;
323 : : virtual std::vector< std::vector< tk::real > > surfOutput(
324 : : const inciter::FaceData&,
325 : : const tk::Fields&,
326 : : const tk::Fields& ) const = 0;
327 : : virtual std::vector< std::vector< tk::real > > histOutput(
328 : : const std::vector< HistData >&,
329 : : const std::vector< std::size_t >&,
330 : : const tk::UnsMesh::Coords&,
331 : : const tk::Fields&,
332 : : const tk::Fields& ) const = 0;
333 : : virtual tk::InitializeFn::result_type analyticSolution(
334 : : tk::real xi, tk::real yi, tk::real zi, tk::real t ) const = 0;
335 : : virtual tk::InitializeFn::result_type solution(
336 : : tk::real xi, tk::real yi, tk::real zi, tk::real t ) const = 0;
337 : : virtual tk::real sp_totalenergy(
338 : : std::size_t, const tk::Fields& ) const = 0;
339 : : virtual void soundspeed(
340 : : std::size_t,
341 : : const tk::Fields&,
342 : : const tk::Fields&,
343 : : std::vector< tk::real >& ) const = 0;
344 : : };
345 : :
346 : : //! \brief Model models the Concept above by deriving from it and overriding
347 : : //! the virtual functions required by Concept
348 : : template< typename T >
349 [ - - ][ - - ]: 0 : struct Model : Concept {
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
350 : 95 : explicit Model( T x ) : data( std::move(x) ) {}
351 : 0 : Concept* copy() const override { return new Model( *this ); }
352 : 299 : std::size_t nprim() const override
353 : 299 : { return data.nprim(); }
354 : 0 : std::size_t nmat() const override
355 : 0 : { return data.nmat(); }
356 : 299 : void IcBoxElems( const tk::Fields& geoElem,
357 : : std::size_t nielem,
358 : : std::vector< std::unordered_set< std::size_t > >& inbox )
359 : 299 : const override { data.IcBoxElems( geoElem, nielem, inbox ); }
360 : 299 : void initialize(
361 : : const tk::Fields& L,
362 : : const std::vector< std::size_t >& inpoel,
363 : : const tk::UnsMesh::Coords& coord,
364 : : const std::vector< std::unordered_set< std::size_t > >& inbox,
365 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&
366 : : elemblkid,
367 : : tk::Fields& unk,
368 : : tk::real t,
369 : : const std::size_t nielem )
370 : 299 : const override { data.initialize( L, inpoel, coord, inbox, elemblkid, unk,
371 : 299 : t, nielem ); }
372 : 1667 : void updatePrimitives( const tk::Fields& unk,
373 : : tk::Fields& prim,
374 : : std::size_t nielem )
375 : 1667 : const override { data.updatePrimitives( unk, prim, nielem ); }
376 : 1368 : void cleanTraceMaterial( tk::real t,
377 : : const tk::Fields& geoElem,
378 : : tk::Fields& unk,
379 : : tk::Fields& prim,
380 : : std::size_t nielem )
381 : 1368 : const override { data.cleanTraceMaterial( t, geoElem, unk, prim, nielem ); }
382 : 1368 : void reconstruct( const tk::Fields& geoElem,
383 : : const inciter::FaceData& fd,
384 : : const std::map< std::size_t,
385 : : std::vector< std::size_t > >& esup,
386 : : const std::vector< std::size_t >& inpoel,
387 : : const tk::UnsMesh::Coords& coord,
388 : : tk::Fields& U,
389 : : tk::Fields& P ) const override
390 : : {
391 : 1368 : data.reconstruct( geoElem, fd, esup, inpoel, coord, U, P );
392 : 1368 : }
393 : 1368 : void limit( const tk::Fields& geoFace,
394 : : const inciter::FaceData& fd,
395 : : const std::map< std::size_t, std::vector< std::size_t > >&
396 : : esup,
397 : : const std::vector< std::size_t >& inpoel,
398 : : const std::vector< int >& srcFlag,
399 : : tk::Fields& U,
400 : : tk::Fields& P ) const override
401 : : {
402 : 1368 : data.limit( geoFace, fd, esup, inpoel, srcFlag, U, P );
403 : 1368 : }
404 : 1368 : void rhs(
405 : : tk::real t,
406 : : const tk::Fields& geoFace,
407 : : const tk::Fields& geoElem,
408 : : const inciter::FaceData& fd,
409 : : const std::vector< std::size_t >& inpoel,
410 : : const tk::UnsMesh::Coords& coord,
411 : : const std::unordered_map< std::size_t, std::set< std::size_t > >&
412 : : elemblkid,
413 : : const tk::Fields& U,
414 : : const tk::Fields& P,
415 : : tk::Fields& R,
416 : : std::vector< int >& srcFlag ) const override
417 : : {
418 : 1368 : data.rhs( t, geoFace, geoElem, fd, inpoel, coord, elemblkid, U, P, R,
419 : : srcFlag );
420 : 1368 : }
421 : 100 : tk::real dt( const inciter::FaceData& fd,
422 : : const tk::Fields& geoFace,
423 : : const tk::Fields& geoElem,
424 : : const tk::Fields& U,
425 : : const tk::Fields& P,
426 : : const std::size_t nielem,
427 : : const std::vector< int >& srcFlag,
428 : : std::vector< tk::real >& local_dte ) const override
429 : : { return data.dt( fd, geoFace, geoElem, U, P, nielem, srcFlag,
430 : 100 : local_dte ); }
431 : 118 : std::map< std::string, tk::GetVarFn > OutVarFn() const override
432 : 118 : { return data.OutVarFn(); }
433 : 0 : std::vector< std::string > analyticFieldNames() const override
434 : 0 : { return data.analyticFieldNames(); }
435 : 59 : std::vector< std::string > surfNames() const override
436 : 59 : { return data.surfNames(); }
437 : 0 : std::vector< std::string > histNames() const override
438 : 0 : { return data.histNames(); }
439 : 22 : std::vector< std::string > names() const override
440 : 22 : { return data.names(); }
441 : 59 : std::vector< std::vector< tk::real > > surfOutput(
442 : : const inciter::FaceData& fd,
443 : : const tk::Fields& U,
444 : : const tk::Fields& P ) const override
445 : 59 : { return data.surfOutput( fd, U, P ); }
446 : 0 : std::vector< std::vector< tk::real > > histOutput(
447 : : const std::vector< HistData >& h,
448 : : const std::vector< std::size_t >& inpoel,
449 : : const tk::UnsMesh::Coords& coord,
450 : : const tk::Fields& U,
451 : : const tk::Fields& P ) const override
452 : 0 : { return data.histOutput( h, inpoel, coord, U, P ); }
453 : : tk::InitializeFn::result_type
454 : 0 : analyticSolution( tk::real xi, tk::real yi, tk::real zi, tk::real t )
455 : 0 : const override { return data.analyticSolution( xi, yi, zi, t ); }
456 : : tk::InitializeFn::result_type
457 : 0 : solution( tk::real xi, tk::real yi, tk::real zi, tk::real t )
458 : 0 : const override { return data.solution( xi, yi, zi, t ); }
459 : 0 : tk::real sp_totalenergy( std::size_t e, const tk::Fields& unk )
460 : 0 : const override { return data.sp_totalenergy( e, unk ); }
461 : 59 : void soundspeed(
462 : : std::size_t nielem,
463 : : const tk::Fields& U,
464 : : const tk::Fields& P,
465 : : std::vector< tk::real >& ss )
466 : 59 : const override { return data.soundspeed( nielem, U, P, ss ); }
467 : : T data;
468 : : };
469 : :
470 : : std::unique_ptr< Concept > self; //!< Base pointer used polymorphically
471 : : };
472 : :
473 : : } // inciter::
474 : :
475 : : #endif // FVPDE_h
|