Line data Source code
1 : // Copyright 2024, UChicago Argonne, LLC
2 : // All Rights Reserved
3 : // Software Name: NEML2 -- the New Engineering material Model Library, version 2
4 : // By: Argonne National Laboratory
5 : // OPEN SOURCE LICENSE (MIT)
6 : //
7 : // Permission is hereby granted, free of charge, to any person obtaining a copy
8 : // of this software and associated documentation files (the "Software"), to deal
9 : // in the Software without restriction, including without limitation the rights
10 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 : // copies of the Software, and to permit persons to whom the Software is
12 : // furnished to do so, subject to the following conditions:
13 : //
14 : // The above copyright notice and this permission notice shall be included in
15 : // all copies or substantial portions of the Software.
16 : //
17 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 : // THE SOFTWARE.
24 :
25 : #include <cmath>
26 :
27 : #include "neml2/base/TensorName.h"
28 : #include "neml2/base/Factory.h"
29 : #include "neml2/base/NEML2Object.h"
30 : #include "neml2/base/Parser.h"
31 :
32 : namespace neml2
33 : {
34 : template <typename T>
35 : const T &
36 2210 : TensorName<T>::resolve(Factory * factory) const
37 : {
38 : // Try to parse as a number
39 2210 : if (_value.defined())
40 11 : return _value;
41 2199 : double val = NAN;
42 2199 : auto success = utils::parse_(val, _raw_str);
43 2199 : if (success)
44 1512 : return _value = resolve_number(val);
45 :
46 : // Try to parse as a tensor object
47 687 : if (_tensor)
48 6 : return *_tensor;
49 :
50 681 : if (!factory)
51 0 : throw ParserException("Failed to resolve tensor name '" + _raw_str + "':" +
52 : "\n Parsing it as a plain numeric literal failed with error message: " +
53 0 : utils::parse_failure_message<double>(_raw_str));
54 :
55 : try
56 : {
57 2109 : _tensor = factory->get_object<T>("Tensors", _raw_str);
58 615 : return *_tensor;
59 : }
60 132 : catch (const FactoryException & err_tensor)
61 : {
62 132 : throw ParserException(
63 66 : "Failed to resolve tensor name '" + _raw_str + "'. Two attempts were made:" +
64 : "\n 1. Parsing it as a plain numeric literal failed with error message: " +
65 66 : utils::parse_failure_message<double>(_raw_str) +
66 66 : "\n 2. Parsing it as a tensor object failed with error message: " + err_tensor.what());
67 : }
68 : }
69 :
70 : template <typename T>
71 : T
72 1512 : TensorName<T>::resolve_number(double val) const
73 : {
74 : if constexpr (std::is_same_v<T, Tensor> || std::is_same_v<T, ATensor>)
75 14 : return Scalar::create(val);
76 : else
77 1498 : return T::full(val);
78 : }
79 :
80 : // Explicit instantiations
81 : template struct TensorName<ATensor>;
82 : #define INSTANTIATE(T) template struct TensorName<T>
83 : FOR_ALL_TENSORBASE(INSTANTIATE);
84 : } // namesace neml2
|