1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725 | // *****************************************************************************
/*!
\file src/Base/Data.hpp
\copyright 2012-2015 J. Bakosi,
2016-2018 Los Alamos National Security, LLC.,
2019-2021 Triad National Security, LLC.
All rights reserved. See the LICENSE file for details.
\brief Generic data storage with different memory layouts
\details Generic data storage with different memory layouts. See also the
rationale discussed in the [design](layout.html) document.
*/
// *****************************************************************************
#ifndef Data_h
#define Data_h
#include <array>
#include <string>
#include <cstdint>
#include <vector>
#include <set>
#include <algorithm>
#include "Types.hpp"
#include "Exception.hpp"
#include "NoWarning/pup_stl.hpp"
namespace tk {
//! Tags for selecting data layout policies
const uint8_t UnkEqComp = 0;
const uint8_t EqCompUnk = 1;
//! Zero-runtime-cost data-layout wrappers with type-based compile-time dispatch
template< uint8_t Layout >
class Data {
private:
//! \brief Inherit type of number of components from keyword 'ncomp'
using ncomp_t = tk::ncomp_t;
public:
//! Default constructor (required for Charm++ migration)
explicit Data() : m_vec(), m_nunk(), m_nprop() {}
//! Constructor
//! \param[in] nu Number of unknowns to allocate memory for
//! \param[in] np Total number of properties, i.e., scalar variables or
//! components, per unknown
explicit Data( ncomp_t nu, ncomp_t np ) :
m_vec( nu*np ),
m_nunk( nu ),
m_nprop( np ) {}
//! Const data access dispatch
//! \details Public interface to const-ref data access to a single real
//! value. Use it as Data(p,c), where p is the unknown index, and c is
//! the component index specifying the scalar equation within a system of
//! equations. Requirement: component <
//! nprop, unknown < nunk, enforced with an assert in DEBUG mode, see also
//! the constructor.
//! \param[in] unknown Unknown index
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \return Const reference to data of type tk::real
const tk::real&
operator()( ncomp_t unknown, ncomp_t component ) const
{ return access( unknown, component, int2type< Layout >() ); }
//! Non-const data access dispatch
//! \details Public interface to non-const-ref data access to a single real
//! value. Use it as Data(p,c), where p is the unknown index, and c is
//! the component index specifying the scalar equation within a system of
//! equations. Requirement: component <
//! nprop, unknown < nunk, enforced with an assert in DEBUG mode, see also
//! the constructor.
//! \param[in] unknown Unknown index
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \return Non-const reference to data of type tk::real
//! \see "Avoid Duplication in const and Non-const Member Function," and
//! "Use const whenever possible," Scott Meyers, Effective C++, 3d ed.
tk::real&
operator()( ncomp_t unknown, ncomp_t component ) {
return const_cast< tk::real& >(
static_cast< const Data& >( *this ).
operator()( unknown, component ) );
}
//! Const ptr to physical variable access dispatch
//! \details Public interface to the first half of a physical variable
//! access. cptr() and var() are two member functions intended to be used
//! together in case when component would be expensive to
//! compute for data access via the function call operator, i.e., cptr()
//! can be used to pre-compute part of the address, which returns a
//! pointer and var() can be used to finish the data access using the
//! pointer returned by cptr(). In other words, cptr() returns part of the
//! address known based on component and intended to be used in
//! a setup phase. Then var() takes this partial address and finishes the
//! address calculation given the unknown id. Thus the following two data
//! accesses are equivalent (modulo constness):
//! * real& value = operator()( unk, comp ); and
//! * const real* p = cptr( comp ); and
//! const real& value = var( p, unk ); or real& value = var( p, unk );
//! Requirement: component < nprop, enforced with an assert in
//! DEBUG mode, see also the constructor.
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \return Pointer to data of type tk::real for use with var()
//! \see Example client code in Statistics::setupOrdinary() and
//! Statistics::accumulateOrd() in Statistics/Statistics.C.
const tk::real*
cptr( ncomp_t component ) const
{ return cptr( component, int2type< Layout >() ); }
//! Const-ref data-access dispatch
//! \details Public interface to the second half of a physical variable
//! access. cptr() and var() are two member functions intended to be used
//! together in case when component would be expensive to
//! compute for data access via the function call operator, i.e., cptr()
//! can be used to pre-compute part of the address, which returns a
//! pointer and var() can be used to finish the data access using the
//! pointer returned by cptr(). In other words, cptr() returns part of the
//! address known based on component and intended to be used in
//! a setup phase. Then var() takes this partial address and finishes the
//! address calculation given the unknown id. Thus the following two data
//! accesses are equivalent (modulo constness):
//! * real& value = operator()( unk, comp ); and
//! * const real* p = cptr( comp ); and
//! const real& value = var( p, unk ); or real& value = var( p, unk );
//! Requirement: unknown < nunk, enforced with an assert in DEBUG mode,
//! see also the constructor.
//! \param[in] pt Pointer to data of type tk::real as returned from cptr()
//! \param[in] unknown Unknown index
//! \return Const reference to data of type tk::real
//! \see Example client code in Statistics::setupOrdinary() and
//! Statistics::accumulateOrd() in Statistics/Statistics.C.
const tk::real&
var( const tk::real* pt, ncomp_t unknown ) const
{ return var( pt, unknown, int2type< Layout >() ); }
//! Non-const-ref data-access dispatch
//! \details Public interface to the second half of a physical variable
//! access. cptr() and var() are two member functions intended to be used
//! together in case when component would be expensive to
//! compute for data access via the function call operator, i.e., cptr()
//! can be used to pre-compute part of the address, which returns a
//! pointer and var() can be used to finish the data access using the
//! pointer returned by cptr(). In other words, cptr() returns part of the
//! address known based on component and intended to be used in
//! a setup phase. Then var() takes this partial address and finishes the
//! address calculation given the unknown id. Thus the following two data
//! accesses are equivalent (modulo constness):
//! * real& value = operator()( unk, comp ); and
//! * const real* p = cptr( comp ); and
//! const real& value = var( p, unk ); or real& value = var( p, unk );
//! Requirement: unknown < nunk, enforced with an assert in DEBUG mode,
//! see also the constructor.
//! \param[in] pt Pointer to data of type tk::real as returned from cptr()
//! \param[in] unknown Unknown index
//! \return Non-const reference to data of type tk::real
//! \see Example client code in Statistics::setupOrdinary() and
//! Statistics::accumulateOrd() in Statistics/Statistics.C.
//! \see "Avoid Duplication in const and Non-const Member Function," and
//! "Use const whenever possible," Scott Meyers, Effective C++, 3d ed.
tk::real&
var( const tk::real* pt, ncomp_t unknown ) {
return const_cast< tk::real& >(
static_cast< const Data& >( *this ).var( pt, unknown ) );
}
//! Access to number of unknowns
//! \return Number of unknowns
ncomp_t nunk() const noexcept { return m_nunk; }
//! Access to number of properties
//! \details This is the total number of scalar components per unknown
//! \return Number of propertes/unknown
ncomp_t nprop() const noexcept { return m_nprop; }
//! Extract flat vector of all unknowns
//! \return Flat vector of reals
std::vector< tk::real >
flat() const {
std::vector< tk::real > w( m_nunk * m_nprop );
for (std::size_t j=0; j<m_nprop; ++j)
for (std::size_t i=0; i<m_nunk; ++i)
w[i*m_nprop+j] = operator()( i, j );
return w;
}
//! Assign from flat vector
//! \param[in] rhs Flat vector to assign from
//! \note This is the opposite of flat().
void operator=( const std::vector< tk::real >& rhs ) {
Assert( rhs.size() == m_nunk * m_nprop, "Size mismatch" );
for (std::size_t j=0; j<m_nprop; ++j)
for (std::size_t i=0; i<m_nunk; ++i)
operator()( i, j ) = rhs[i*m_nprop+j];
}
//! Assign from array(3) of vectors
//! \param[in] rhs Array(3) of vectors to assign from
void operator=( const std::array< std::vector< tk::real >, 3 >& rhs ) {
Assert( m_nprop == 3, "Size mismatch" );
Assert( rhs[0].size() == m_nunk, "Size mismatch" );
Assert( rhs[1].size() == m_nunk, "Size mismatch" );
Assert( rhs[2].size() == m_nunk, "Size mismatch" );
for (std::size_t j=0; j<3; ++j)
for (std::size_t i=0; i<m_nunk; ++i)
operator()( i, j ) = rhs[j][i];
}
//! Extract vector of unknowns given component
//! \details Requirement: component < nprop, enforced with an
//! assert in DEBUG mode, see also the constructor.
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \return A vector of unknowns given by component (length:
//! nunk(), i.e., the first constructor argument)
std::vector< tk::real >
extract_comp( ncomp_t component ) const {
std::vector< tk::real > w( m_nunk );
for (ncomp_t i=0; i<m_nunk; ++i)
w[i] = operator()( i, component );
return w;
}
//! Extract (a copy of) all components for an unknown
//! \details Requirement: unknown < nunk, enforced with an assert in DEBUG
//! mode, see also the constructor.
//! \param[in] unknown Index of unknown
//! \return A vector of components for a single unknown (length: nprop,
//! i.e., the second constructor argument)
std::vector< tk::real >
extract( ncomp_t unknown ) const {
std::vector< tk::real > w( m_nprop );
for (ncomp_t i=0; i<m_nprop; ++i) w[i] = operator()( unknown, i );
return w;
}
//! Extract all components for unknown
//! \details Requirement: unknown < nunk, enforced with an assert in DEBUG
//! mode, see also the constructor.
//! \param[in] unknown Index of unknown
//! \return A vector of components for a single unknown (length: nprop,
//! i.e., the second constructor argument)
//! \note This is simply an alias for extract( unknown )
std::vector< tk::real >
operator[]( ncomp_t unknown ) const { return extract( unknown ); }
//! Extract (a copy of) four values of unknowns
//! \details Requirement: component < nprop, [A,B,C,D] < nunk,
//! enforced with an assert in DEBUG mode, see also the constructor.
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \param[in] A Index of 1st unknown
//! \param[in] B Index of 2nd unknown
//! \param[in] C Index of 3rd unknown
//! \param[in] D Index of 4th unknown
//! \return Array of the four values of component
std::array< tk::real, 4 >
extract( ncomp_t component,
ncomp_t A, ncomp_t B, ncomp_t C, ncomp_t D ) const
{
auto p = cptr( component );
return {{ var(p,A), var(p,B), var(p,C), var(p,D) }};
}
//! Extract (a copy of) four values of unknowns
//! \details Requirement: component < nprop, for all N[i] < nunk,
//! enforced with an assert in DEBUG mode, see also the constructor.
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \param[in] N Indices of the 4 unknowns
//! \return Array of the four values of component
std::array< tk::real, 4 >
extract( ncomp_t component,
const std::array< ncomp_t, 4 >& N ) const
{
return extract( component, N[0], N[1], N[2], N[3] );
}
//! Extract (a copy of) three values of unknowns
//! \details Requirement: component < nprop, [A,B,C] < nunk,
//! enforced with an assert in DEBUG mode, see also the constructor.
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \param[in] A Index of 1st unknown
//! \param[in] B Index of 2nd unknown
//! \param[in] C Index of 3rd unknown
//! \return Array of the four values of component
std::array< tk::real, 3 >
extract( ncomp_t component,
ncomp_t A, ncomp_t B, ncomp_t C ) const
{
auto p = cptr( component );
return {{ var(p,A), var(p,B), var(p,C) }};
}
//! Extract (a copy of) three values of unknowns
//! \details Requirement: component < nprop, for all N[i] < nunk,
//! enforced with an assert in DEBUG mode, see also the constructor.
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \param[in] N Indices of the 3 unknowns
//! \return Array of the three values of component
std::array< tk::real, 3 >
extract( ncomp_t component,
const std::array< ncomp_t, 3 >& N ) const
{
return extract( component, N[0], N[1], N[2] );
}
//! Const-ref accessor to underlying raw data as a std::vector
//! \return Constant reference to underlying raw data
const std::vector< tk::real >& vec() const { return m_vec; }
//! Non-const-ref accessor to underlying raw data as a std::vector
//! \return Non-constant reference to underlying raw data
std::vector< tk::real >& vec() { return m_vec; }
//! Compound operator-=
//! \param[in] rhs Data object to subtract
//! \return Reference to ourselves after subtraction
Data< Layout >& operator-= ( const Data< Layout >& rhs ) {
Assert( rhs.nunk() == m_nunk, "Incorrect number of unknowns" );
Assert( rhs.nprop() == m_nprop, "Incorrect number of properties" );
std::transform( rhs.vec().cbegin(), rhs.vec().cend(),
m_vec.cbegin(), m_vec.begin(),
[]( tk::real s, tk::real d ){ return d-s; } );
return *this;
}
//! Operator -
//! \param[in] rhs Data object to subtract
//! \return Copy of Data object after rhs has been subtracted
//! \details Implemented in terms of compound operator-=
Data< Layout > operator- ( const Data< Layout >& rhs )
const { return Data< Layout >( *this ) -= rhs; }
//! Compound operator+=
//! \param[in] rhs Data object to add
//! \return Reference to ourselves after addition
Data< Layout >& operator+= ( const Data< Layout >& rhs ) {
Assert( rhs.nunk() == m_nunk, "Incorrect number of unknowns" );
Assert( rhs.nprop() == m_nprop, "Incorrect number of properties" );
std::transform( rhs.vec().cbegin(), rhs.vec().cend(),
m_vec.cbegin(), m_vec.begin(),
[]( tk::real s, tk::real d ){ return d+s; } );
return *this;
}
//! Operator +
//! \param[in] rhs Data object to add
//! \return Copy of Data object after rhs has been multiplied with
//! \details Implemented in terms of compound operator+=
Data< Layout > operator+ ( const Data< Layout >& rhs )
const { return Data< Layout >( *this ) += rhs; }
//! Compound operator*= multiplying by another Data object item by item
//! \param[in] rhs Data object to multiply with
//! \return Reference to ourselves after multiplication
Data< Layout >& operator*= ( const Data< Layout >& rhs ) {
Assert( rhs.nunk() == m_nunk, "Incorrect number of unknowns" );
Assert( rhs.nprop() == m_nprop, "Incorrect number of properties" );
std::transform( rhs.vec().cbegin(), rhs.vec().cend(),
m_vec.cbegin(), m_vec.begin(),
[]( tk::real s, tk::real d ){ return d*s; } );
return *this;
}
//! Operator * multiplying by another Data object item by item
//! \param[in] rhs Data object to multiply with
//! \return Copy of Data object after rhs has been multiplied with
//! \details Implemented in terms of compound operator*=
Data< Layout > operator* ( const Data< Layout >& rhs )
const { return Data< Layout >( *this ) *= rhs; }
//! Compound operator*= multiplying all items by a scalar
//! \param[in] rhs Scalar to multiply with
//! \return Reference to ourselves after multiplication
Data< Layout >& operator*= ( tk::real rhs ) {
// cppcheck-suppress useStlAlgorithm
for (auto& v : m_vec) v *= rhs;
return *this;
}
//! Operator * multiplying all items by a scalar
//! \param[in] rhs Scalar to multiply with
//! \return Copy of Data object after rhs has been multiplied with
//! \details Implemented in terms of compound operator*=
Data< Layout > operator* ( tk::real rhs )
const { return Data< Layout >( *this ) *= rhs; }
//! Compound operator/=
//! \param[in] rhs Data object to divide by
//! \return Reference to ourselves after division
Data< Layout >& operator/= ( const Data< Layout >& rhs ) {
Assert( rhs.nunk() == m_nunk, "Incorrect number of unknowns" );
Assert( rhs.nprop() == m_nprop, "Incorrect number of properties" );
std::transform( rhs.vec().cbegin(), rhs.vec().cend(),
m_vec.cbegin(), m_vec.begin(),
[]( tk::real s, tk::real d ){ return d/s; } );
return *this;
}
//! Operator /
//! \param[in] rhs Data object to divide by
//! \return Copy of Data object after rhs has been divided by
//! \details Implemented in terms of compound operator/=
Data< Layout > operator/ ( const Data< Layout >& rhs )
const { return Data< Layout >( *this ) /= rhs; }
//! Compound operator/= dividing all items by a scalar
//! \param[in] rhs Scalar to divide with
//! \return Reference to ourselves after division
Data< Layout >& operator/= ( tk::real rhs ) {
// cppcheck-suppress useStlAlgorithm
for (auto& v : m_vec) v /= rhs;
return *this;
}
//! Operator / dividing all items by a scalar
//! \param[in] rhs Scalar to divide with
//! \return Copy of Data object after rhs has been divided by
//! \details Implemented in terms of compound operator/=
Data< Layout > operator/ ( tk::real rhs )
const { return Data< Layout >( *this ) /= rhs; }
//! Add new unknown at the end of the container
//! \param[in] prop Vector of properties to initialize the new unknown with
void push_back( const std::vector< tk::real >& prop )
{ return push_back( prop, int2type< Layout >() ); }
//! Resize data store to contain 'count' elements
//! \param[in] count Resize store to contain count * nprop elements
//! \param[in] value Value to initialize new data with (default: 0.0)
//! \note This works for both shrinking and enlarging, as this simply
//! translates to std::vector::resize(). Note that count changes, nprop
//! does not, see the private overload resize().
void resize( std::size_t count, tk::real value = 0.0 )
{ resize( count, value, int2type< Layout >() ); }
//! Remove a number of unknowns
//! \param[in] unknown Set of indices of unknowns to remove
void rm( const std::set< ncomp_t >& unknown ) {
auto remove = [ &unknown ]( std::size_t i ) -> bool {
if (unknown.find(i) != end(unknown)) return true;
return false;
};
std::size_t last = 0;
for(std::size_t i=0; i<m_nunk; ++i, ++last) {
while( remove(i) ) ++i;
if (i >= m_nunk) break;
for (ncomp_t p = 0; p<m_nprop; ++p)
m_vec[ last*m_nprop+p ] = m_vec[ i*m_nprop+p ];
}
m_vec.resize( last*m_nprop );
m_nunk -= unknown.size();
}
//! Fill vector of unknowns with the same value
//! \details Requirement: component < nprop, enforced with an
//! assert in DEBUG mode, see also the constructor.
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \param[in] value Value to fill vector of unknowns with
inline void fill( ncomp_t component, tk::real value ) {
auto p = cptr( component );
for (ncomp_t i=0; i<m_nunk; ++i) var(p,i) = value;
}
//! Fill full data storage with value
//! \param[in] value Value to fill data with
void fill( tk::real value )
{ std::fill( begin(m_vec), end(m_vec), value ); }
//! Check if vector of unknowns is empty
bool empty() const noexcept { return m_vec.empty(); }
//! Layout name dispatch
//! \return The name of the data layout used
static std::string layout() { return layout( int2type< Layout >() ); }
/** @name Pack/Unpack: Serialize Data object for Charm++ */
///@{
//! \brief Pack/Unpack serialize member function
//! \param[in,out] p Charm++'s PUP::er serializer object reference
void pup( PUP::er &p ) {<--- Parameter 'p' can be declared with const
p | m_vec;
p | m_nunk;
p | m_nprop;
}
//! \brief Pack/Unpack serialize operator|
//! \param[in,out] p Charm++'s PUP::er serializer object reference
//! \param[in,out] d DataLyaout object reference
friend void operator|( PUP::er& p, Data& d ) { d.pup(p); }
//@}
private:
//! Transform a compile-time uint8_t into a type, used for dispatch
//! \see A. Alexandrescu, Modern C++ Design: Generic Programming and Design
//! Patterns Applied, Addison-Wesley Professional, 2001.
template< uint8_t m > struct int2type { enum { value = m }; };
//! Overloads for the various const data accesses
//! \details Requirement: component < nprop, unknown < nunk,
//! enforced with an assert in DEBUG mode, see also the constructor.
//! \param[in] unknown Unknown index
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \return Const reference to data of type tk::real
//! \see A. Alexandrescu, Modern C++ Design: Generic Programming and Design
//! Patterns Applied, Addison-Wesley Professional, 2001.
const tk::real&
access( ncomp_t unknown, ncomp_t component, int2type< UnkEqComp > ) const
{
Assert( component < m_nprop, "Out-of-bounds access: "
"component < number of properties" );
Assert( unknown < m_nunk, "Out-of-bounds access: unknown < number of "
"unknowns" );
return m_vec[ unknown*m_nprop + component ];
}
const tk::real&
access( ncomp_t unknown, ncomp_t component, int2type< EqCompUnk > ) const
{
Assert( component < m_nprop, "Out-of-bounds access: "
"component < number of properties" );
Assert( unknown < m_nunk, "Out-of-bounds access: unknown < number of "
"unknowns" );
return m_vec[ (component)*m_nunk + unknown ];
}
// Overloads for the various const ptr to physical variable accesses
//! \details Requirement: component < nprop, unknown < nunk,
//! enforced with an assert in DEBUG mode, see also the constructor.
//! \param[in] component Component index, i.e., position of a scalar within
//! a system
//! \return Pointer to data of type tk::real for use with var()
//! \see A. Alexandrescu, Modern C++ Design: Generic Programming and Design
//! Patterns Applied, Addison-Wesley Professional, 2001.
const tk::real*
cptr( ncomp_t component, int2type< UnkEqComp > ) const {
Assert( component < m_nprop, "Out-of-bounds access: "
"component < number of properties" );
return m_vec.data() + component;
}
const tk::real*
cptr( ncomp_t component, int2type< EqCompUnk > ) const {
Assert( component < m_nprop, "Out-of-bounds access: "
"component < number of properties" );
return m_vec.data() + (component)*m_nunk;
}
// Overloads for the various const physical variable accesses
//! Requirement: unknown < nunk, enforced with an assert in DEBUG mode,
//! see also the constructor.
//! \param[in] pt Pointer to data of type tk::real as returned from cptr()
//! \param[in] unknown Unknown index
//! \return Const reference to data of type tk::real
//! \see A. Alexandrescu, Modern C++ Design: Generic Programming and Design
//! Patterns Applied, Addison-Wesley Professional, 2001.
inline const tk::real&
var( const tk::real* const pt, ncomp_t unknown, int2type< UnkEqComp > )
const {
Assert( unknown < m_nunk, "Out-of-bounds access: unknown < number of "
"unknowns" );
return *(pt + unknown*m_nprop);
}
inline const tk::real&
var( const tk::real* const pt, ncomp_t unknown, int2type< EqCompUnk > )
const {
Assert( unknown < m_nunk, "Out-of-bounds access: unknown < number of "
"unknowns" );
return *(pt + unknown);
}
//! Add new unknown
//! \param[in] prop Vector of properties to initialize the new unknown with
//! \note Only the UnkEqComp overload is provided as this operation would be
//! too inefficient with the EqCompUnk data layout.
void push_back( const std::vector< tk::real >& prop, int2type< UnkEqComp > )
{
Assert( prop.size() == m_nprop, "Incorrect number of properties" );
m_vec.resize( (m_nunk+1) * m_nprop );
ncomp_t u = m_nunk;
++m_nunk;
for (ncomp_t i=0; i<m_nprop; ++i) operator()( u, i ) = prop[i];
}
void push_back( const std::vector< tk::real >&, int2type< EqCompUnk > )
{ Throw( "Not implented. It would be inefficient" ); }
//! Resize data store to contain 'count' elements
//! \param[in] count Resize store to contain 'count' elements
//! \param[in] value Value to initialize new data with
//! \note Only the UnkEqComp overload is provided as this operation would be
//! too inefficient with the EqCompUnk data layout.
//! \note This works for both shrinking and enlarging, as this simply
//! translates to std::vector::resize().
void resize( std::size_t count, tk::real value, int2type< UnkEqComp > ) {
m_vec.resize( count * m_nprop, value );
m_nunk = count;
}
void resize( std::size_t, tk::real, int2type< EqCompUnk > ) {
Throw( "Not implemented. It would be inefficient" );
}
// Overloads for the name-queries of data lauouts
//! \return The name of the data layout used
//! \see A. Alexandrescu, Modern C++ Design: Generic Programming and Design
//! Patterns Applied, Addison-Wesley Professional, 2001.
static std::string layout( int2type< UnkEqComp > )
{ return "unknown-major"; }
static std::string layout( int2type< EqCompUnk > )
{ return "equation-major"; }
std::vector< tk::real > m_vec; //!< Data pointer
ncomp_t m_nunk; //!< Number of unknowns
ncomp_t m_nprop; //!< Number of properties/unknown
};
//! Operator * multiplying all items by a scalar from the left
//! \param[in] lhs Scalar to multiply with
//! \param[in] rhs Date object to multiply
//! \return New Data object with all items multipled with lhs
template< uint8_t Layout >
Data< Layout > operator* ( tk::real lhs, const Data< Layout >& rhs ) {
return Data< Layout >( rhs ) *= lhs;
}
//! Operator min between two Data objects
//! \param[in] a 1st Data object
//! \param[in] b 2nd Data object
//! \return New Data object containing the minimum of all values for each
//! value in _a_ and _b_
//! \note The Data objects _a_ and _b_ must have the same number of
//! unknowns and properties.
//! \note As opposed to std::min, this function creates and returns a new object
//! instead of returning a reference to one of the operands.
template< uint8_t Layout >
Data< Layout > min( const Data< Layout >& a, const Data< Layout >& b ) {
Assert( a.nunk() == b.nunk(), "Number of unknowns unequal" );
Assert( a.nprop() == b.nprop(), "Number of properties unequal" );
Data< Layout > r( a.nunk(), a.nprop() );
std::transform( a.vec().cbegin(), a.vec().cend(),
b.vec().cbegin(), r.vec().begin(),
[]( tk::real s, tk::real d ){ return std::min(s,d); } );
return r;
}
//! Operator max between two Data objects
//! \param[in] a 1st Data object
//! \param[in] b 2nd Data object
//! \return New Data object containing the maximum of all values for each
//! value in _a_ and _b_
//! \note The Data objects _a_ and _b_ must have the same number of
//! unknowns and properties.
//! \note As opposed to std::max, this function creates and returns a new object
//! instead of returning a reference to one of the operands.
template< uint8_t Layout >
Data< Layout > max( const Data< Layout >& a, const Data< Layout >& b ) {
Assert( a.nunk() == b.nunk(), "Number of unknowns unequal" );
Assert( a.nprop() == b.nprop(), "Number of properties unequal" );
Data< Layout > r( a.nunk(), a.nprop() );
std::transform( a.vec().cbegin(), a.vec().cend(),
b.vec().cbegin(), r.vec().begin(),
[]( tk::real s, tk::real d ){ return std::max(s,d); } );
return r;
}
//! Operator == between two Data objects
//! \param[in] lhs Data object to compare
//! \param[in] rhs Data object to compare
//! \return True if all entries are equal up to epsilon
template< uint8_t Layout >
bool operator== ( const Data< Layout >& lhs, const Data< Layout >& rhs ) {
Assert( rhs.nunk() == lhs.nunk(), "Incorrect number of unknowns" );
Assert( rhs.nprop() == lhs.nprop(), "Incorrect number of properties" );
auto l = lhs.vec().cbegin();
auto r = rhs.vec().cbegin();
while (l != lhs.vec().cend()) {
if (std::abs(*l - *r) > std::numeric_limits< tk::real >::epsilon())
return false;
++l; ++r;
}
return true;
}
//! Operator != between two Data objects
//! \param[in] lhs Data object to compare
//! \param[in] rhs Data object to compare
//! \return True if all entries are unequal up to epsilon
template< uint8_t Layout >
bool operator!= ( const Data< Layout >& lhs, const Data< Layout >& rhs )
{ return !(lhs == rhs); }
//! Compute the maximum difference between the elements of two Data objects
//! \param[in] lhs 1st Data object
//! \param[in] rhs 2nd Data object
//! \return The index, i.e., the raw position, of and the largest absolute value
//! of the difference between all corresponding elements of _lhs_ and _rhs_.
//! \details The position returned is the position in the underlying raw data
//! structure, independent of components, etc. If lhs == rhs with
//! precision std::numeric_limits< tk::real >::epsilon(), a pair of (0,0.0)
//! is returned.
//! \note The Data objects _lhs_ and _rhs_ must have the same number of
//! unknowns and properties.
template< uint8_t Layout >
std::pair< std::size_t, tk::real >
maxdiff( const Data< Layout >& lhs, const Data< Layout >& rhs ) {
Assert( lhs.nunk() == rhs.nunk(), "Number of unknowns unequal" );
Assert( lhs.nprop() == rhs.nprop(), "Number of properties unequal" );
auto l = lhs.vec().cbegin();
auto r = rhs.vec().cbegin();
std::pair< std::size_t, tk::real > m( 0, std::abs(*l - *r) );
++l; ++r;
while (l != lhs.vec().cend()) {
const auto d = std::abs(*l - *r);
if (d > m.second) m = { std::distance(lhs.vec().cbegin(),l), d };
++l; ++r;
}
return m;
}
} // tk::
#endif // Data_h
|