https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LAROMANCE6DInterpolation.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9#if 0 // NEML2 v2->v3 migration: DEFERRED (FEM/discretization/typed-tensor path has no v3 C++
10 // equivalent yet)
11
13
14#ifdef NEML2_ENABLED
15
16#include <fstream>
17#include <initializer_list>
18#include <torch/torch.h>
19
20#include "neml2/tensors/functions/sign.h"
21#include "neml2/tensors/functions/abs.h"
22#include "neml2/tensors/functions/pow.h"
23#include "neml2/tensors/functions/log10.h"
24#include "neml2/tensors/functions/clamp.h"
25#include "neml2/tensors/functions/stack.h"
26
27namespace neml2
28{
30
31OptionSet
33{
34 auto options = Model::expected_options();
35 options.doc() =
36 "Multilinear interpolation over six dimensions (von_mises_stress, temperature, "
37 "equivalent_plastic_strain, cell_dislocation_density, wall_dislocation_density, env_factor)";
38
39 // Model inputs
40 options.add_input("equivalent_plastic_strain", "The equivalent plastic strain");
41 options.add_input("von_mises_stress", "The von Mises stress");
42 options.add_input("cell_dislocation_density", "The cell dislocation density");
43 options.add_input("wall_dislocation_density", "The wall dislocation density");
44 options.add_input("temperature", "The temperature");
45 options.add_input("env_factor", "The environment factor");
46
47 // Model Outputs
48 options.add_output("output_rate", "The output rate");
49
50 // JSON
51 options.add<std::string>("model_file_name", "The name of the model file");
52 options.add<std::string>("model_file_variable_name",
53 "The name of the variable in the model file");
54
55 // jit does not currently work with this
56 options.set<bool>("jit", false);
57 options.suppress("jit");
58
59 return options;
60}
61
63 : Model(options),
64 _vm_stress(declare_input_variable<Scalar>("von_mises_stress")),
65 _temperature(declare_input_variable<Scalar>("temperature")),
66 _ep_strain(declare_input_variable<Scalar>("equivalent_plastic_strain")),
67 _cell_dd(declare_input_variable<Scalar>("cell_dislocation_density")),
68 _wall_dd(declare_input_variable<Scalar>("wall_dislocation_density")),
69 _env_fac(declare_input_variable<Scalar>("env_factor")),
70 _output_rate(declare_output_variable<Scalar>("output_rate"))
71{
72 std::string filename = options.get<std::string>("model_file_name");
73 std::ifstream model_file(filename.c_str());
74 model_file >> _json;
75
76 // storing grid points for indexing.
77 // these should be stored differently so that they are all read in at once. The order of this can
78 // get messed up easily
80 _temperature_grid = json_vector_to_torch("in_temperature");
81 _plastic_strain_grid = json_vector_to_torch("in_plastic_strain");
85
86 // Read in grid axes transform enums
87 _stress_transform_enum = get_transform_enum(json_to_string("in_stress_transform_type"));
88 _temperature_transform_enum = get_transform_enum(json_to_string("in_temperature_transform_type"));
90 get_transform_enum(json_to_string("in_plastic_strain_transform_type"));
91 _cell_transform_enum = get_transform_enum(json_to_string("in_cell_transform_type"));
92 _wall_transform_enum = get_transform_enum(json_to_string("in_wall_transform_type"));
93 _env_transform_enum = get_transform_enum(json_to_string("in_env_transform_type"));
94
95 // Read in grid axes transform values
96 _stress_transform_values = json_to_vector("in_stress_transform_values");
97 _temperature_transform_values = json_to_vector("in_temperature_transform_values");
98 _plastic_strain_transform_values = json_to_vector("in_plastic_strain_transform_values");
99 _cell_transform_values = json_to_vector("in_cell_transform_values");
100 _wall_transform_values = json_to_vector("in_wall_transform_values");
101 _env_transform_values = json_to_vector("in_env_transform_values");
102
103 // Storing values for interpolation
104 _output_rate_name = options.get<std::string>("model_file_variable_name");
106
107 // set up output transforms
108 if (_output_rate_name == "out_ep")
109 {
110 _output_transform_enum = get_transform_enum(json_to_string("out_strain_rate_transform_type"));
111 _output_transform_values = json_to_vector("out_strain_rate_transform_values");
112 }
113 else if (_output_rate_name == "out_cell")
114 {
115 _output_transform_enum = get_transform_enum(json_to_string("out_cell_rate_transform_type"));
116 _output_transform_values = json_to_vector("out_cell_rate_transform_values");
117 }
118 else if (_output_rate_name == "out_wall")
119 {
120 _output_transform_enum = get_transform_enum(json_to_string("out_wall_rate_transform_type"));
121 _output_transform_values = json_to_vector("out_wall_rate_transform_values");
122 }
123 else
124 {
125 throw NEMLException("This ouput variable is not implemented, model_file_variable_name: " +
126 std::string(_output_rate_name));
127 }
128}
129
130void
132{
133 // only using first derivatives of out_ep, not out_cell and out_wall
134 if (_output_rate_name == "out_ep")
135 {
136 std::vector<const VariableBase *> inputs = {&_vm_stress};
137 _output_rate.request_AD(inputs);
138 }
139}
140
141void
142LAROMANCE6DInterpolation::set_value(bool out, bool /*dout_din*/, bool /*d2out_din2*/)
143{
144 if (out)
146}
147
150{
151 if (name == "COMPRESS")
153 else if (name == "DECOMPRESS")
155 else if (name == "LOG10BOUNDED")
157 else if (name == "EXP10BOUNDED")
159 else if (name == "MINMAX")
161
162 throw NEMLException("Unrecognized transform: " + std::string(name));
163}
164
165std::pair<Scalar, Scalar>
167 const Scalar & interp_points) const
168{
169 // idx is for the left grid point.
170 // searchsorted returns the right idx so -1 makes it the left
171 auto left_idx = Scalar(torch::searchsorted(grid, interp_points) - 1, 0);
172
173 // this allows us to extrapolate
174 left_idx = Scalar(torch::clamp(left_idx, 0, grid.sizes()[0] - 2), 0);
175
176 auto left_coord = grid.dynamic_index({left_idx});
177 auto right_coord =
178 grid.dynamic_index({left_idx + torch::tensor(1, default_integer_tensor_options())});
179 auto left_fraction = (right_coord - interp_points) / (right_coord - left_coord);
180
181 return {left_idx, neml2::dynamic_stack({left_fraction, 1 - left_fraction}, -1)};
182}
183
184Scalar
186 const std::vector<std::pair<Scalar, Scalar>> index_and_fraction, const Scalar grid_values) const
187{
188 Scalar result = Scalar::zeros_like(_temperature());
189 for (const auto i : {0, 1})
190 for (const auto j : {0, 1})
191 for (const auto k : {0, 1})
192 for (const auto l : {0, 1})
193 for (const auto m : {0, 1})
194 for (const auto n : {0, 1})
195 {
196 auto vertex_value =
197 grid_values.index({(index_and_fraction[0].first +
198 torch::tensor(i, default_integer_tensor_options())),
199 (index_and_fraction[1].first +
200 torch::tensor(j, default_integer_tensor_options())),
201 (index_and_fraction[2].first +
202 torch::tensor(k, default_integer_tensor_options())),
203 (index_and_fraction[3].first +
204 torch::tensor(l, default_integer_tensor_options())),
205 (index_and_fraction[4].first +
206 torch::tensor(m, default_integer_tensor_options())),
207 (index_and_fraction[5].first +
208 torch::tensor(n, default_integer_tensor_options()))});
209 auto weight = index_and_fraction[0].second.select(-1, i) *
210 index_and_fraction[1].second.select(-1, j) *
211 index_and_fraction[2].second.select(-1, k) *
212 index_and_fraction[3].second.select(-1, l) *
213 index_and_fraction[4].second.select(-1, m) *
214 index_and_fraction[5].second.select(-1, n);
215 result += vertex_value * weight;
216 }
217 return result;
218}
219
221Scalar
223{
224 // These transform constants should be given in the json file.
225 const auto cell_dd_transformed =
227 const auto wall_dd_transformed =
229 const auto vm_stress_transformed =
231 const auto ep_strain_transformed = transform_data(
233 const auto temperature_transformed =
235 const auto env_fac_transformed =
237
238 std::vector<std::pair<Scalar, Scalar>> left_index_weight;
239 left_index_weight.push_back(findLeftIndexAndFraction(_stress_grid, vm_stress_transformed));
240 left_index_weight.push_back(findLeftIndexAndFraction(_temperature_grid, temperature_transformed));
241 left_index_weight.push_back(
242 findLeftIndexAndFraction(_plastic_strain_grid, ep_strain_transformed));
243 left_index_weight.push_back(findLeftIndexAndFraction(_cell_grid, cell_dd_transformed));
244 left_index_weight.push_back(findLeftIndexAndFraction(_wall_grid, wall_dd_transformed));
245 left_index_weight.push_back(findLeftIndexAndFraction(_env_grid, env_fac_transformed));
246 Scalar interpolated_result = compute_interpolation(left_index_weight, _grid_values);
247 Scalar transformed_result =
249 return transformed_result;
250}
251
252Scalar
254 const std::vector<double> & param,
255 TransformEnum transform_type) const
256{
257 switch (transform_type)
258 {
260 return transform_compress(data, param);
261
263 return transform_decompress(data, param);
264
266 return transform_log10_bounded(data, param);
267
269 return transform_exp10_bounded(data, param);
270
272 return transform_min_max(data, param);
273
274 default:
275 return data;
276 }
277}
278
279Scalar
281 const std::vector<double> & param) const
282{
283 double factor = param[0];
284 double compressor = param[1];
285 double original_min = param[2];
286 auto d1 = neml2::sign(data) * neml2::pow(neml2::abs(data * factor), compressor);
287 auto transformed_data = neml2::log10(1.0 + d1 - original_min);
288 return transformed_data;
289}
290
291Scalar
293 const std::vector<double> & param) const
294{
295 double factor = param[0];
296 double compressor = param[1];
297 double original_min = param[2];
298 auto d1 = neml2::pow(10.0, data) - 1.0 + original_min;
299 auto transformed_data = neml2::sign(d1) * neml2::pow(neml2::abs(d1), 1.0 / compressor) / factor;
300 return transformed_data;
301}
302
303Scalar
305 const std::vector<double> & param) const
306
307{
308 double factor = param[0];
309 double lowerbound = param[1];
310 double upperbound = param[2];
311 double logmin = param[3];
312 double logmax = param[4];
313 double range = upperbound - lowerbound;
314 auto transformed_data =
315 range * (neml2::log10(data + factor) - logmin) / (logmax - logmin) + lowerbound;
316 return transformed_data;
317}
318
319Scalar
321 const std::vector<double> & param) const
322{
323 double factor = param[0];
324 double lowerbound = param[1];
325 double upperbound = param[2];
326 double logmin = param[3];
327 double logmax = param[4];
328 double range = upperbound - lowerbound;
329 auto transformed_data =
330 (neml2::pow(10.0, ((data - lowerbound) * (logmax - logmin) / range) + logmin) - factor);
331 return transformed_data;
332}
333
334Scalar
336 const std::vector<double> & param) const
337{
338 double data_min = param[0];
339 double data_max = param[1];
340 double scaled_min = param[2];
341 double scaled_max = param[3];
342 auto transformed_data =
343 ((data - data_min) / (data_max - data_min)) * (scaled_max - scaled_min) + scaled_min;
344 return transformed_data;
345}
346
347std::string
348LAROMANCE6DInterpolation::json_to_string(const std::string & key) const
349{
350 if (!_json.contains(key))
351 throw NEMLException("The key '" + std::string(key) + "' is missing from the JSON data file.");
352
353 std::string name = _json[key].get<std::string>();
354 return name;
355}
356
357std::vector<double>
358LAROMANCE6DInterpolation::json_to_vector(const std::string & key) const
359{
360 if (!_json.contains(key))
361 throw NEMLException("The key '" + std::string(key) + "' is missing from the JSON data file.");
362
363 std::vector<double> data_vec = _json[key].get<std::vector<double>>();
364 return data_vec;
365}
366
367Scalar
369{
370 if (!_json.contains(key))
371 throw NEMLException("The key '" + std::string(key) + "' is missing from the JSON data file.");
372
373 std::vector<double> in_data = _json[key].get<std::vector<double>>();
374 return Scalar::create(in_data).clone();
375}
376
377Scalar
379{
380 using std::vector;
381 if (!_json.contains(key))
382 throw NEMLException("The key '" + std::string(key) + "' is missing from the JSON data file.");
383
384 vector<vector<vector<vector<vector<vector<double>>>>>> out_data =
385 _json[key].get<vector<vector<vector<vector<vector<vector<double>>>>>>>();
386
387 const int64_t sz_l0 = out_data.size();
388 const int64_t sz_l1 = out_data[0].size();
389 const int64_t sz_l2 = out_data[0][0].size();
390 const int64_t sz_l3 = out_data[0][0][0].size();
391 const int64_t sz_l4 = out_data[0][0][0][0].size();
392 const int64_t sz_l5 = out_data[0][0][0][0][0].size();
393
394 auto check_level_size =
395 [](const int64_t current_vec_size, const int64_t sz_level, const std::string & key)
396 {
397 if (current_vec_size != sz_level)
398 throw NEMLException("Incorrect JSON interpolation grid size for '" + key + "'.");
399 };
400
401 std::vector<double> linearize_values;
402 check_level_size(out_data.size(), sz_l0, key);
403 for (auto && level1 : out_data)
404 {
405 check_level_size(level1.size(), sz_l1, key);
406 for (auto && level2 : level1)
407 {
408 check_level_size(level2.size(), sz_l2, key);
409 for (auto && level3 : level2)
410 {
411 check_level_size(level3.size(), sz_l3, key);
412 for (auto && level4 : level3)
413 {
414 check_level_size(level4.size(), sz_l4, key);
415 for (auto && level5 : level4)
416 {
417 check_level_size(level5.size(), sz_l5, key);
418 for (auto && value : level5)
419 linearize_values.push_back(value);
420 }
421 }
422 }
423 }
424 }
425
426 return Scalar::create(linearize_values)
427 .dynamic_reshape({sz_l0, sz_l1, sz_l2, sz_l3, sz_l4, sz_l5})
428 .clone();
429}
430
431} // namespace neml2
432
433#endif
434
435#endif // NEML2 v2->v3 migration: DEFERRED
const std::string name
Definition Setup.h:21
std::vector< double > _temperature_transform_values
Scalar transform_decompress(const Scalar &data, const std::vector< double > &params) const
std::pair< Scalar, Scalar > findLeftIndexAndFraction(const Scalar &grid, const Scalar &interp_points) const
find index of input point
const Variable< Scalar > & _temperature
Temperature.
const Variable< Scalar > & _cell_dd
cell dislocation density
const Variable< Scalar > & _wall_dd
wall dislocation density
Scalar transform_log10_bounded(const Scalar &data, const std::vector< double > &params) const
Scalar transform_data(const Scalar &data, const std::vector< double > &param, TransformEnum transform_type) const
transform data
const Variable< Scalar > & _vm_stress
Model input for interpolation.
Scalar transform_min_max(const Scalar &data, const std::vector< double > &params) const
nlohmann::json _json
JSON object containing interpolation grid and values.
TransformEnum _output_transform_enum
output transform enum
Scalar interpolate_and_transform() const
compute interpolated value and transform results
Scalar _grid_values
grid values being interpolated
std::vector< double > json_to_vector(const std::string &key) const
read in json axes transform constants
Scalar json_vector_to_torch(const std::string &key) const
read 1D vector of grid points from json and store in Torch tensor
std::vector< double > _output_transform_values
output transform values
Scalar compute_interpolation(const std::vector< std::pair< Scalar, Scalar > > index_and_fraction, const Scalar grid_values) const
compute interpolated value
void set_value(bool, bool, bool) override
LAROMANCE6DInterpolation(const OptionSet &options)
Variable< Scalar > & _output_rate
Model output.
Scalar _stress_grid
grid for interpolation
Scalar transform_exp10_bounded(const Scalar &data, const std::vector< double > &params) const
std::string json_to_string(const std::string &key) const
read in json axes transform name
Scalar json_6Dvector_to_torch(const std::string &key) const
read 6D grid date from json and store in Torch tensor
std::vector< double > _stress_transform_values
input transform values
TransformEnum _stress_transform_enum
input transform enums
std::vector< double > _plastic_strain_transform_values
const Variable< Scalar > & _env_fac
environmental factor
const Variable< Scalar > & _ep_strain
The creep strain.
Scalar transform_compress(const Scalar &data, const std::vector< double > &params) const
LAROMANCE transforms for input axes and output axis.
std::string _output_rate_name
output transform rate name
TransformEnum get_transform_enum(const std::string &name) const
register_NEML2_object(LibtorchModel)