Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pymatlib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rahil Doshi
pymatlib
Commits
e8c0aac8
Commit
e8c0aac8
authored
4 months ago
by
Rahil Doshi
Browse files
Options
Downloads
Patches
Plain Diff
Generalize InterpolationArrayContainer to use x and y arrays
parent
fa71279a
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#77205
passed
4 months ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/pymatlib/core/codegen/interpolation_array_container.py
+45
-45
45 additions, 45 deletions
src/pymatlib/core/codegen/interpolation_array_container.py
with
45 additions
and
45 deletions
src/pymatlib/core/codegen/interpolation_array_container.py
+
45
−
45
View file @
e8c0aac8
...
...
@@ -7,27 +7,27 @@ from pymatlib.core.interpolators import prepare_interpolation_arrays
class
InterpolationArrayContainer
(
CustomGenerator
):
"""
Container for
energy-temperature
interpolation arrays and methods.
"""
Container for
x-y
interpolation arrays and methods.
This class stores
temperature and energy densit
y arrays and generates C++ code
for efficient
bilateral
conversion
between these properties
. It supports both
This class stores
x and
y arrays and generates C++ code
for efficient conversion
to compute y for a given x
. It supports both
binary search interpolation (O(log n)) and double lookup interpolation (O(1))
with automatic method selection based on data characteristics.
Attributes:
name (str): Name for the generated C++ class.
T
_array (np.ndarray): Array of
temperature
values (must be monotonically increasing).
E
_array (np.ndarray): Array of
energy densit
y values corresponding to
T
_array.
x
_array (np.ndarray): Array of
x
values (must be monotonically increasing).
y
_array (np.ndarray): Array of y values corresponding to
x
_array.
method (str): Interpolation method selected (
"
binary_search
"
or
"
double_lookup
"
).
T
_bs (np.ndarray):
Temperature
array prepared for binary search.
E
_bs (np.ndarray):
Energ
y array prepared for binary search.
x
_bs (np.ndarray):
x
array prepared for binary search.
y
_bs (np.ndarray): y array prepared for binary search.
has_double_lookup (bool): Whether double lookup interpolation is available.
If has_double_lookup is True, the following attributes are also available:
T
_eq (np.ndarray): Equidistant
temperature
array for double lookup.
E
_neq (np.ndarray): Non-equidistant
energ
y array for double lookup.
E
_eq (np.ndarray): Equidistant
energ
y array for double lookup.
inv_delta_
E
_eq (float): Inverse of the
energ
y step size for double lookup.
x
_eq (np.ndarray): Equidistant
x
array for double lookup.
y
_neq (np.ndarray): Non-equidistant y array for double lookup.
y
_eq (np.ndarray): Equidistant y array for double lookup.
inv_delta_
y
_eq (float): Inverse of the y step size for double lookup.
idx_map (np.ndarray): Index mapping array for double lookup.
Examples:
...
...
@@ -44,14 +44,14 @@ class InterpolationArrayContainer(CustomGenerator):
>>>
container
=
InterpolationArrayContainer
(
"
MyMaterial
"
,
T
,
E
)
>>>
sfg
.
generate
(
container
)
"""
def
__init__
(
self
,
name
:
str
,
temperature
_array
:
np
.
ndarray
,
energy_densit
y_array
:
np
.
ndarray
):
def
__init__
(
self
,
name
:
str
,
x
_array
:
np
.
ndarray
,
y_array
:
np
.
ndarray
):
"""
Initialize the interpolation container.
Args:
name (str): Name for the generated C++ class.
temperature
_array (np.ndarray): Array of
temperature
values
(K)
.
x
_array (np.ndarray): Array of
x
values.
Must be monotonically increasing.
energy_densit
y_array (np.ndarray): Array of
energy densit
y values
(J/m³)
corresponding to
temperature
_array.
y_array (np.ndarray): Array of y values
corresponding to
x
_array.
Raises:
ValueError: If arrays are empty, have different lengths, or are not monotonic.
TypeError: If name is not a string or arrays are not numpy arrays.
...
...
@@ -65,28 +65,28 @@ class InterpolationArrayContainer(CustomGenerator):
if
not
re
.
match
(
r
'
^[a-zA-Z_][a-zA-Z0-9_]*$
'
,
name
):
raise
ValueError
(
f
"'
{
name
}
'
is not a valid C++ class name
"
)
if
not
isinstance
(
temperature
_array
,
np
.
ndarray
)
or
not
isinstance
(
energy_densit
y_array
,
np
.
ndarray
):
if
not
isinstance
(
x
_array
,
np
.
ndarray
)
or
not
isinstance
(
y_array
,
np
.
ndarray
):
raise
TypeError
(
"
Temperature and energy arrays must be numpy arrays
"
)
self
.
name
=
name
self
.
T
_array
=
temperature
_array
self
.
E
_array
=
energy_densit
y_array
self
.
x
_array
=
x
_array
self
.
y
_array
=
y_array
# Prepare arrays and determine best method
try
:
self
.
data
=
prepare_interpolation_arrays
(
T
_array
=
self
.
T
_array
,
E
_array
=
self
.
E
_array
,
verbose
=
False
)
self
.
data
=
prepare_interpolation_arrays
(
x
_array
=
self
.
x
_array
,
y
_array
=
self
.
y
_array
,
verbose
=
False
)
self
.
method
=
self
.
data
[
"
method
"
]
# Store arrays for binary search (always available)
self
.
T
_bs
=
self
.
data
[
"
T
_bs
"
]
self
.
E
_bs
=
self
.
data
[
"
E
_bs
"
]
self
.
x
_bs
=
self
.
data
[
"
x
_bs
"
]
self
.
y
_bs
=
self
.
data
[
"
y
_bs
"
]
# Store arrays for double lookup if available
if
self
.
method
==
"
double_lookup
"
:
self
.
T
_eq
=
self
.
data
[
"
T
_eq
"
]
self
.
E
_neq
=
self
.
data
[
"
E
_neq
"
]
self
.
E
_eq
=
self
.
data
[
"
E
_eq
"
]
self
.
inv_delta_
E
_eq
=
self
.
data
[
"
inv_delta_
E
_eq
"
]
self
.
x
_eq
=
self
.
data
[
"
x
_eq
"
]
self
.
y
_neq
=
self
.
data
[
"
y
_neq
"
]
self
.
y
_eq
=
self
.
data
[
"
y
_eq
"
]
self
.
inv_delta_
y
_eq
=
self
.
data
[
"
inv_delta_
y
_eq
"
]
self
.
idx_map
=
self
.
data
[
"
idx_map
"
]
self
.
has_double_lookup
=
True
else
:
...
...
@@ -100,11 +100,11 @@ class InterpolationArrayContainer(CustomGenerator):
Args:
name (str): Name for the generated C++ class.
material: Material object with temperature and energy properties.
Must have energy_density_temperature_array and
energy_densit
y_array attributes.
Must have energy_density_temperature_array and y_array attributes.
Returns:
InterpolationArrayContainer: Container with arrays for interpolation.
"""
return
cls
(
name
,
material
.
energy_density_temperature_array
,
material
.
energy_densit
y_array
)
return
cls
(
name
,
material
.
energy_density_temperature_array
,
material
.
y_array
)
def
_generate_binary_search
(
self
,
sfg
:
SfgComposer
):
"""
Generate code for binary search interpolation.
...
...
@@ -113,19 +113,19 @@ class InterpolationArrayContainer(CustomGenerator):
Returns:
list: List of public members for the C++ class.
"""
T
_bs_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
T
_bs
)
E
_bs_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
E
_bs
)
x
_bs_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
x
_bs
)
y
_bs_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
y
_bs
)
E
_target
=
sfg
.
var
(
"
E
_target
"
,
"
double
"
)
y
_target
=
sfg
.
var
(
"
y
_target
"
,
"
double
"
)
return
[
# Binary search arrays
f
"
static constexpr std::array< double,
{
self
.
T
_bs
.
shape
[
0
]
}
>
T
_bs {{
{
T
_bs_arr_values
}
}};
\n
"
f
"
static constexpr std::array< double,
{
self
.
E
_bs
.
shape
[
0
]
}
>
E
_bs {{
{
E
_bs_arr_values
}
}};
\n
"
,
f
"
static constexpr std::array< double,
{
self
.
x
_bs
.
shape
[
0
]
}
>
x
_bs {{
{
x
_bs_arr_values
}
}};
\n
"
f
"
static constexpr std::array< double,
{
self
.
y
_bs
.
shape
[
0
]
}
>
y
_bs {{
{
y
_bs_arr_values
}
}};
\n
"
,
# Binary search method
sfg
.
method
(
"
interpolateBS
"
,
returns
=
PsCustomType
(
"
[[nodiscard]] double
"
),
inline
=
True
,
const
=
True
)(
sfg
.
expr
(
"
return interpolate_binary_search_cpp({}, *this);
"
,
E
_target
)
sfg
.
expr
(
"
return interpolate_binary_search_cpp({}, *this);
"
,
y
_target
)
)
]
...
...
@@ -139,24 +139,24 @@ class InterpolationArrayContainer(CustomGenerator):
if
not
self
.
has_double_lookup
:
return
[]
T
_eq_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
T
_eq
)
E
_neq_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
E
_neq
)
E
_eq_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
E
_eq
)
x
_eq_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
x
_eq
)
y
_neq_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
y
_neq
)
y
_eq_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
y
_eq
)
idx_mapping_arr_values
=
"
,
"
.
join
(
str
(
v
)
for
v
in
self
.
idx_map
)
E
_target
=
sfg
.
var
(
"
E
_target
"
,
"
double
"
)
y
_target
=
sfg
.
var
(
"
y
_target
"
,
"
double
"
)
return
[
# Double lookup arrays
f
"
static constexpr std::array< double,
{
self
.
T
_eq
.
shape
[
0
]
}
>
T
_eq {{
{
T
_eq_arr_values
}
}};
\n
"
f
"
static constexpr std::array< double,
{
self
.
E
_neq
.
shape
[
0
]
}
>
E
_neq {{
{
E
_neq_arr_values
}
}};
\n
"
f
"
static constexpr std::array< double,
{
self
.
E
_eq
.
shape
[
0
]
}
>
E
_eq {{
{
E
_eq_arr_values
}
}};
\n
"
f
"
static constexpr double inv_delta_
E
_eq =
{
self
.
inv_delta_
E
_eq
}
;
\n
"
f
"
static constexpr std::array< double,
{
self
.
x
_eq
.
shape
[
0
]
}
>
x
_eq {{
{
x
_eq_arr_values
}
}};
\n
"
f
"
static constexpr std::array< double,
{
self
.
y
_neq
.
shape
[
0
]
}
>
y
_neq {{
{
y
_neq_arr_values
}
}};
\n
"
f
"
static constexpr std::array< double,
{
self
.
y
_eq
.
shape
[
0
]
}
>
y
_eq {{
{
y
_eq_arr_values
}
}};
\n
"
f
"
static constexpr double inv_delta_
y
_eq =
{
self
.
inv_delta_
y
_eq
}
;
\n
"
f
"
static constexpr std::array< int,
{
self
.
idx_map
.
shape
[
0
]
}
> idx_map {{
{
idx_mapping_arr_values
}
}};
\n
"
,
# Double lookup method
sfg
.
method
(
"
interpolateDL
"
,
returns
=
PsCustomType
(
"
[[nodiscard]] double
"
),
inline
=
True
,
const
=
True
)(
sfg
.
expr
(
"
return interpolate_double_lookup_cpp({}, *this);
"
,
E
_target
)
sfg
.
expr
(
"
return interpolate_double_lookup_cpp({}, *this);
"
,
y
_target
)
)
]
...
...
@@ -178,17 +178,17 @@ class InterpolationArrayContainer(CustomGenerator):
public_members
.
extend
(
self
.
_generate_double_lookup
(
sfg
))
# Add interpolate method that uses recommended approach
E
_target
=
sfg
.
var
(
"
E
_target
"
,
"
double
"
)
y
_target
=
sfg
.
var
(
"
y
_target
"
,
"
double
"
)
if
self
.
has_double_lookup
:
public_members
.
append
(
sfg
.
method
(
"
interpolate
"
,
returns
=
PsCustomType
(
"
[[nodiscard]] double
"
),
inline
=
True
,
const
=
True
)(
sfg
.
expr
(
"
return interpolate_double_lookup_cpp({}, *this);
"
,
E
_target
)
sfg
.
expr
(
"
return interpolate_double_lookup_cpp({}, *this);
"
,
y
_target
)
)
)
else
:
public_members
.
append
(
sfg
.
method
(
"
interpolate
"
,
returns
=
PsCustomType
(
"
[[nodiscard]] double
"
),
inline
=
True
,
const
=
True
)(
sfg
.
expr
(
"
return interpolate_binary_search_cpp({}, *this);
"
,
E
_target
)
sfg
.
expr
(
"
return interpolate_binary_search_cpp({}, *this);
"
,
y
_target
)
)
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment