31#ifndef __TASMANIAN_IOHELPERS_HPP
32#define __TASMANIAN_IOHELPERS_HPP
34#include "tsgEnumerates.hpp"
82struct mode_ascii_type{};
88struct mode_binary_type{};
116inline std::map<std::string_view, TypeOneDRule> getStringRuleMap(){
117 return std::initializer_list<std::pair<std::string_view const, TypeOneDRule>>{
167inline TypeOneDRule getRuleString(std::string_view
const &name){
168 auto const &name_map = getStringRuleMap();
169 auto rule = name_map.find(name);
170 return (rule == name_map.end()) ?
rule_none : rule->second;
177inline std::string_view getRuleString(
TypeOneDRule rule){
178 auto smap = getStringRuleMap();
179 return std::find_if(smap.begin(), smap.end(),
180 [&](std::pair<std::string_view const, TypeOneDRule> r)->bool{ return (r.second == rule); })->first;
187inline std::vector<TypeOneDRule> getIntRuleMap(){
208 auto rmap = getIntRuleMap();
209 return ((
size_t) r < rmap.size()) ? rmap[(size_t) r] :
rule_none;
217 auto rmap = getIntRuleMap();
218 return (
int) std::distance(rmap.begin(), std::find_if(rmap.begin(), rmap.end(),
226inline std::map<std::string_view, TypeDepth> getStringToDepthMap(){
227 return std::initializer_list<std::pair<std::string_view const, TypeDepth>>{
245inline TypeDepth getDepthTypeString(std::string_view
const &name){
246 auto const &name_map = getStringToDepthMap();
247 auto depth = name_map.find(name);
248 return (depth == name_map.end()) ?
type_none : depth->second;
259 return ((
size_t) t < imap.size()) ? imap[(size_t) t] :
type_none;
266inline std::map<std::string_view, TypeRefinement> getStringToRefinementMap(){
267 return std::initializer_list<std::pair<std::string_view const, TypeRefinement>>{
278inline TypeRefinement getTypeRefinementString(std::string_view
const &name){
279 auto const &name_map = getStringToRefinementMap();
280 auto const ref = name_map.find(name);
281 return (ref == name_map.end()) ?
refine_none : ref->second;
291 return ((
size_t) refinement < imap.size()) ? imap[(size_t) refinement] :
refine_none;
298template<
bool iomode, IOPad pad>
299void writeFlag(
bool flag, std::ostream &os){
301 os << ((flag) ?
"1" :
"0");
302 if ((pad == pad_rspace) || ((pad == pad_auto) && flag)) os <<
" ";
303 if ((pad == pad_line) || ((pad == pad_auto) && !flag)) os << std::endl;
305 char cflag = ((flag) ?
'y' :
'n');
306 os.write(&cflag,
sizeof(
char));
314template<
typename iomode>
315bool readFlag(std::istream &os){
316 if (std::is_same<iomode, mode_ascii_type>::value){
322 os.read(&cflag,
sizeof(
char));
323 return (cflag ==
'y');
331template<
bool iomode, IOPad pad,
typename VecType>
332void writeVector(
const std::vector<VecType> &x, std::ostream &os){
334 if (pad == pad_lspace)
335 for(
auto i : x) os <<
" " << i;
336 if (pad == pad_rspace)
337 for(
auto i : x) os << i <<
" ";
338 if ((pad == pad_none) || (pad == pad_line)){
340 for(
size_t i = 1; i < x.size(); i++) os <<
" " << x[i];
341 if (pad == pad_line) os << std::endl;
344 os.write((
char*) x.data(), x.size() *
sizeof(VecType));
352template<
typename iomode,
typename VecType>
353void readVector(std::istream &is, std::vector<VecType> &x){
354 if (std::is_same<iomode, mode_ascii_type>::value){
355 for(
auto &i : x) is >> i;
357 is.read((
char*) x.data(), x.size() *
sizeof(VecType));
365template<
typename iomode,
typename VecType,
typename SizeType>
366std::vector<VecType> readVector(std::istream &is, SizeType num_entries){
367 std::vector<VecType> x((
size_t) num_entries);
368 readVector<iomode, VecType>(is, x);
376template<
bool iomode, IOPad pad,
typename... Vals>
377void writeNumbers(std::ostream &os, Vals... vals){
378 std::vector<
typename std::tuple_element<0, std::tuple<Vals...>>::type> values = {vals...};
379 writeVector<iomode, pad>(values, os);
386template<
typename iomode,
typename Val>
387Val readNumber(std::istream &is){
389 if (std::is_same<iomode, mode_ascii_type>::value){
392 is.read((
char*) &v,
sizeof(Val));
404 os << getRuleString(rule) << std::endl;
406 int r = getRuleInt(rule);
407 os.write((
char*) &r,
sizeof(
int));
415template<
typename iomode>
417 if (std::is_same<iomode, mode_ascii_type>::value){
420 return getRuleString(T);
422 return getRuleInt(readNumber<mode_binary_type, int>(is));
TypeOneDRule
Used to specify the one dimensional family of rules that induces the sparse grid.
Definition tsgEnumerates.hpp:288
TypeDepth
Used by Global Sequence and Fourier grids, indicates the selection criteria.
Definition tsgEnumerates.hpp:206
TypeRefinement
Refinement strategy for local polynomial and wavelet grids.
Definition tsgEnumerates.hpp:428
@ rule_minlebesgue
A greedy sequence rule with nodes added to minimize the Lebesgue constant.
Definition tsgEnumerates.hpp:324
@ rule_gaussgegenbauer
Non-nested rule optimized for integral of the form .
Definition tsgEnumerates.hpp:346
@ rule_gausshermite
Non-nested rule optimized for integral of the form .
Definition tsgEnumerates.hpp:358
@ rule_gausslegendre
Non-nested rule but optimized for integration.
Definition tsgEnumerates.hpp:332
@ rule_fejer2
Similar to rule_clenshawcurtis but with nodes strictly in the interior.
Definition tsgEnumerates.hpp:296
@ rule_leja
Classic sequence rule, moderate Lebesgue constant growth (empirical result only).
Definition tsgEnumerates.hpp:302
@ rule_rlejashifteddouble
Same as rule_rlejashifted but doubling the number of nodes per level, which reduced the Lebesgue cons...
Definition tsgEnumerates.hpp:318
@ rule_gausslaguerreodd
Same as rule_gausslaguerre but using only odd levels, partially mitigates the non-nested issues.
Definition tsgEnumerates.hpp:356
@ rule_localp
Nested rule with a hierarchy of uniformly distributed nodes and functions with compact support.
Definition tsgEnumerates.hpp:365
@ rule_chebyshevodd
Same as rule_chebyshev but using only odd levels, partially mitigates the non-nested issues.
Definition tsgEnumerates.hpp:300
@ rule_minlebesgueodd
Same as rule_minlebesgue but using only odd levels, quadrature is more stable.
Definition tsgEnumerates.hpp:326
@ rule_gausslegendreodd
Same as rule_gausslegendre but using only odd levels, partially mitigates the non-nested issues.
Definition tsgEnumerates.hpp:334
@ rule_gausschebyshev1
Non-nested rule optimized for integral of the form .
Definition tsgEnumerates.hpp:338
@ rule_gausslaguerre
Non-nested rule optimized for integral of the form .
Definition tsgEnumerates.hpp:354
@ rule_wavelet
Wavelet basis with uniformly distributed nodes (primarily for internal use).
Definition tsgEnumerates.hpp:373
@ rule_mindelta
A greedy sequence rule with nodes added to minimize the norm of the surplus operator.
Definition tsgEnumerates.hpp:328
@ rule_gaussgegenbauerodd
Same as rule_gaussgegenbauer but using only odd levels, partially mitigates the non-nested issues.
Definition tsgEnumerates.hpp:348
@ rule_localpb
Variation of rule_localp focusing nodes on the boundary instead of the interior.
Definition tsgEnumerates.hpp:371
@ rule_customtabulated
User provided rule, nodes and weights must be provided with a separate file.
Definition tsgEnumerates.hpp:362
@ rule_gausshermiteodd
Same as rule_gausshermite but using only odd levels, partially mitigates the non-nested issues.
Definition tsgEnumerates.hpp:360
@ rule_clenshawcurtis
Classic nested rule using Chebyshev nodes with very low Lebesgue constant.
Definition tsgEnumerates.hpp:292
@ rule_gausschebyshev2odd
Same as rule_gausschebyshev2 but using only odd levels, partially mitigates the non-nested issues.
Definition tsgEnumerates.hpp:344
@ rule_maxlebesgueodd
Same as rule_maxlebesgue but using only odd levels, quadrature is more stable.
Definition tsgEnumerates.hpp:322
@ rule_maxlebesgue
A greedy sequence rule with nodes placed at the maximum of the Lebesgue function.
Definition tsgEnumerates.hpp:320
@ rule_clenshawcurtis0
Same as rule_clenshawcurtis but with modified basis that assumes the model is zero at the boundary.
Definition tsgEnumerates.hpp:294
@ rule_none
Null rule, should never be used as input (default rule for an empty grid).
Definition tsgEnumerates.hpp:290
@ rule_chebyshev
Using Chebyshev nodes with very low Lebesgue constant and slow node growth, but non-nested.
Definition tsgEnumerates.hpp:298
@ rule_gausschebyshev1odd
Same as rule_gausschebyshev1 but using only odd levels, partially mitigates the non-nested issues.
Definition tsgEnumerates.hpp:340
@ rule_rlejashifted
Similar sequence to rule_rleja but with nodes strictly in the interior.
Definition tsgEnumerates.hpp:314
@ rule_gausschebyshev2
Non-nested rule optimized for integral of the form .
Definition tsgEnumerates.hpp:342
@ rule_localp0
Variation of rule_localp assuming the model is zero at the domain boundary.
Definition tsgEnumerates.hpp:367
@ rule_fourier
Trigonometric basis with uniformly distributed nodes (primarily for internal use).
Definition tsgEnumerates.hpp:375
@ rule_mindeltaodd
Same as rule_mindelta but using only odd levels, quadrature is more stable.
Definition tsgEnumerates.hpp:330
@ rule_rlejadouble4
Using rule_rleja nodes but doubling the nodes every 4 levels, reduces the Lebesgue constant.
Definition tsgEnumerates.hpp:310
@ rule_gaussjacobiodd
Same as rule_gaussjacobi but using only odd levels, partially mitigates the non-nested issues.
Definition tsgEnumerates.hpp:352
@ rule_lejaodd
Same as rule_leja but using only odd levels, quadrature is more stable.
Definition tsgEnumerates.hpp:304
@ rule_rlejaodd
Same as rule_rleja but using only odd levels, quadrature is more stable.
Definition tsgEnumerates.hpp:312
@ rule_rlejadouble2
Using rule_rleja nodes but doubling the nodes every 2 levels, reduces the Lebesgue constant.
Definition tsgEnumerates.hpp:308
@ rule_rlejashiftedeven
Same as rule_rlejashifted but using only even levels, quadrature is more stable.
Definition tsgEnumerates.hpp:316
@ rule_gausspatterson
Nested rule that is optimized for integration, probably the best integration rule in more than 2 dime...
Definition tsgEnumerates.hpp:336
@ rule_rleja
Classic sequence rule based on complex analysis, moderate Lebesgue constant growth (theoretically pro...
Definition tsgEnumerates.hpp:306
@ rule_semilocalp
Variation of rule_localp using increased support in exchange for higher order basis (better for smoot...
Definition tsgEnumerates.hpp:369
@ rule_gaussjacobi
Non-nested rule optimized for integral of the form .
Definition tsgEnumerates.hpp:350
@ type_curved
Ignoring the polynomial space, use rules with index .
Definition tsgEnumerates.hpp:216
@ type_iptensor
Make a dense tensor grid with interpolation range that includes .
Definition tsgEnumerates.hpp:248
@ type_qptotal
Total degree polynomial space for quadrature/integration.
Definition tsgEnumerates.hpp:227
@ type_qpcurved
Curved polynomial space for quadrature/integration.
Definition tsgEnumerates.hpp:234
@ type_level
Ignoring the polynomial space, use rules with index .
Definition tsgEnumerates.hpp:212
@ type_qphyperbolic
Hyperbolic cross section polynomial space for quadrature/integration.
Definition tsgEnumerates.hpp:241
@ type_none
Null type, should never be used for input, indicates an error of some sort.
Definition tsgEnumerates.hpp:208
@ type_iptotal
Total degree polynomial space for interpolation, i.e., the span of .
Definition tsgEnumerates.hpp:224
@ type_qptensor
Make a dense tensor grid with quadrature/integration range that includes .
Definition tsgEnumerates.hpp:252
@ type_hyperbolic
Ignoring the polynomial space, use rules with index .
Definition tsgEnumerates.hpp:220
@ type_iphyperbolic
Hyperbolic cross section polynomial space for interpolation, i.e., the span of .
Definition tsgEnumerates.hpp:238
@ type_tensor
Make a dense tensor grid with rules indexed by .
Definition tsgEnumerates.hpp:244
@ type_ipcurved
Curved polynomial space for interpolation, i.e., the span of .
Definition tsgEnumerates.hpp:231
@ refine_parents_first
Isotropic refinement adding children only if the parents are already included.
Definition tsgEnumerates.hpp:432
@ refine_classic
Isotropic refinement using only the children and disregarding missing parents.
Definition tsgEnumerates.hpp:430
@ refine_direction_selective
Anisotropic refinement using only the children and disregarding missing parents.
Definition tsgEnumerates.hpp:434
@ refine_fds
Anisotropic refinement adding children only if the parents are already included.
Definition tsgEnumerates.hpp:436
@ refine_none
Null method, should never be used as input.
Definition tsgEnumerates.hpp:440
@ refine_stable
Isotropic refinement that ensures the points maintain lower-complete structures.
Definition tsgEnumerates.hpp:438
constexpr bool mode_binary
Constant allowing for more expressive selection of ascii and binary mode in IO methods.
Definition tsgIOHelpers.hpp:68
constexpr bool mode_ascii
Constant allowing for more expressive selection of ascii and binary mode in IO methods.
Definition tsgIOHelpers.hpp:62
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68