BELFEM
0.9.3
Berkeley Lab Finite Element Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
user_table.hpp
Go to the documentation of this file.
1
/*
2
* BELFEM -- The Berkeley Lab Finite Element Framework
3
* Copyright (c) 2026, The Regents of the University of California,
4
* through Lawrence Berkeley National Laboratory (subject to receipt of any required
5
* approvals from the U.S. Dept. of Energy). All rights reserved.
6
*
7
* Developers: Christian Messe, Gregory Giard
8
*
9
* See the top-level LICENSE file for the complete license and disclaimer.
10
*/
11
12
/*
13
* Two-column lookup table, shared by the material and the source plugin.
14
*
15
* Both plugins used to carry one copy of this loader per data file -- ten in
16
* matlib.cpp and one in current.cpp, sixty-eight lines each, differing only in
17
* the file name. They also located their data with the __FILE__ trick, which
18
* baked the SOURCE directory into the binary and broke the moment the sources
19
* moved into ./src and the data into ./data. Both problems are fixed here:
20
* one implementation, and the data directory comes from CMake as
21
* BELFEM_USER_DATA_DIR, so the library does not depend on where it is launched
22
* from either.
23
*/
24
25
#ifndef TAPE_QUENCH_USER_TABLE_HPP
26
#define TAPE_QUENCH_USER_TABLE_HPP
27
28
#include <algorithm>
29
#include <fstream>
30
#include <sstream>
31
#include <string>
32
#include <vector>
33
34
#include <belfem_user_api>
35
36
#ifndef BELFEM_USER_DATA_DIR
37
#error "BELFEM_USER_DATA_DIR is not set -- see the target_compile_definitions in CMakeLists.txt"
38
#endif
39
40
namespace
usermat
41
{
42
//------------------------------------------------------------------------------
43
59
class
Table
60
{
61
const
std::string mFile ;
62
std::vector< belfem::real > mX ;
63
std::vector< belfem::real > mY ;
64
bool
mLoaded = false ;
65
66
//------------------------------------------------------------------------------
67
public
:
68
//------------------------------------------------------------------------------
69
70
explicit
Table
(
const
std::string & File ) : mFile( File )
71
{
72
}
73
74
//------------------------------------------------------------------------------
75
76
belfem::real
77
operator()
(
const
belfem::real
x )
78
{
79
if
( ! mLoaded )
80
{
81
this->load() ;
82
}
83
84
if
( x <= mX.front() )
return
mY.front() ;
85
if
( x >= mX.back() )
return
mY.back() ;
86
87
// index of the first sample strictly greater than x. The two
88
// clamps above guarantee 1 <= k <= mX.size() - 1, so neither
89
// k - 1 nor k can run off the ends.
90
const
std::size_t k = std::distance(
91
mX.begin(), std::upper_bound( mX.begin(), mX.end(), x ) ) ;
92
93
const
belfem::real
t = ( x - mX[ k-1 ] ) / ( mX[ k ] - mX[ k-1 ] ) ;
94
95
return
mY[ k-1 ] + t * ( mY[ k ] - mY[ k-1 ] ) ;
96
}
97
98
//------------------------------------------------------------------------------
99
private
:
100
//------------------------------------------------------------------------------
101
102
void
103
load()
104
{
105
const
std::string tPath = std::string( BELFEM_USER_DATA_DIR ) + mFile ;
106
107
std::ifstream tFile( tPath ) ;
108
109
BELFEM_ERROR
( tFile.is_open(),
110
"Cannot open user data file %s"
, tPath.c_str() ) ;
111
112
std::string tLine ;
113
belfem::real
x ;
114
belfem::real
y ;
115
116
while
( std::getline( tFile, tLine ) )
117
{
118
std::istringstream tStream( tLine ) ;
119
120
if
( tStream >> x >> y )
121
{
122
mX.push_back( x ) ;
123
mY.push_back( y ) ;
124
}
125
}
126
127
tFile.close() ;
128
129
BELFEM_ERROR
( mX.size() > 1,
130
"User data file %s contains fewer than two samples"
, tPath.c_str() ) ;
131
132
mLoaded = true ;
133
}
134
};
135
136
//------------------------------------------------------------------------------
137
}
138
#endif
//TAPE_QUENCH_USER_TABLE_HPP
BELFEM_ERROR
#define BELFEM_ERROR(aCheck,...)
Definition
assert.hpp:264
usermat::Table::operator()
belfem::real operator()(const belfem::real x)
Definition
user_table.hpp:77
usermat::Table::Table
Table(const std::string &File)
Definition
user_table.hpp:70
belfem::real
double real
Definition
typedefs.hpp:36
usermat
Definition
user_table.hpp:41
examples
tape_quench_usermat
src
user_table.hpp
Generated by
1.18.0