NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
LabeledAxis.h
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#pragma once
26
27#include <map>
28
29#include "neml2/misc/string_utils.h"
30#include "neml2/base/LabeledAxisAccessor.h"
31
32namespace neml2
33{
34class LabeledAxis;
35
47{
48public:
50 LabeledAxis() = default;
51
54
56 bool is_setup() const { return _setup; }
57
59 LabeledAxisAccessor qualify(const LabeledAxisAccessor & accessor) const;
60
63 bool has_state() const { return _has_state; }
64 bool has_old_state() const { return _has_old_state; }
65 bool has_forces() const { return _has_forces; }
66 bool has_old_forces() const { return _has_old_forces; }
67 bool has_residual() const { return _has_residual; }
68 bool has_parameters() const { return _has_parameters; }
70
72 LabeledAxis & add_subaxis(const std::string & name);
73
75 void add_variable(const LabeledAxisAccessor & name, Size sz);
76
78 template <typename T>
80
82 void setup_layout();
83
85 Size size() const;
86
88 Size size(const LabeledAxisAccessor & name) const;
89
91 indexing::Slice slice(const LabeledAxisAccessor & name) const;
92
96 std::size_t nvariable() const;
98 bool has_variable(const LabeledAxisAccessor & name) const;
100 std::size_t variable_id(const LabeledAxisAccessor & name) const;
102 const std::vector<LabeledAxisAccessor> & variable_names() const;
104 const std::vector<std::pair<Size, Size>> & variable_slices() const;
106 const std::pair<Size, Size> & variable_slice(const LabeledAxisAccessor & name) const;
108 const std::vector<Size> & variable_sizes() const;
112
116 std::size_t nsubaxis() const;
118 bool has_subaxis(const LabeledAxisAccessor & name) const;
120 std::size_t subaxis_id(const std::string & name) const;
122 const std::vector<const LabeledAxis *> & subaxes() const;
124 const LabeledAxis & subaxis(const LabeledAxisAccessor & name) const;
128 const std::vector<std::string> & subaxis_names() const;
130 const std::vector<std::pair<Size, Size>> & subaxis_slices() const;
132 std::pair<Size, Size> subaxis_slice(const LabeledAxisAccessor & name) const;
134 const std::vector<Size> & subaxis_sizes() const;
138
140 bool equals(const LabeledAxis & other) const;
141
142 friend std::ostream & operator<<(std::ostream & os, const LabeledAxis & axis);
143
144private:
146 void cache_reserved_subaxis(const std::string & axis_name);
147
149 void ensure_setup_dbg() const;
150
152 bool _setup = false;
153
155 const LabeledAxisAccessor _prefix = {};
156
158 Size _size = 0;
159
161 std::map<std::string, Size> _variables;
162
164 std::map<std::string, std::shared_ptr<LabeledAxis>> _subaxes;
165
174 std::map<LabeledAxisAccessor, std::size_t> _variable_to_id_map;
176 std::vector<LabeledAxisAccessor> _id_to_variable_map;
178 std::vector<Size> _id_to_variable_size_map;
180 std::vector<std::pair<Size, Size>> _id_to_variable_slice_map;
182
191 std::vector<const LabeledAxis *> _sorted_subaxes;
193 std::map<std::string, std::size_t> _subaxis_to_id_map;
195 std::vector<std::string> _id_to_subaxis_map;
197 std::vector<Size> _id_to_subaxis_size_map;
199 std::vector<std::pair<Size, Size>> _id_to_subaxis_slice_map;
201
204 bool _has_state = false;
205 bool _has_old_state = false;
206 bool _has_forces = false;
207 bool _has_old_forces = false;
208 bool _has_residual = false;
209 bool _has_parameters = false;
211};
212
213std::ostream & operator<<(std::ostream & os, const LabeledAxis & axis);
214
215bool operator==(const LabeledAxis & a, const LabeledAxis & b);
216
217bool operator!=(const LabeledAxis & a, const LabeledAxis & b);
218} // namespace neml2
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:56
A labeled axis used to associate layout of a tensor with human-interpretable names.
Definition LabeledAxis.h:47
bool equals(const LabeledAxis &other) const
Check to see if two axes are equivalent.
Definition LabeledAxis.cxx:426
void add_variable(const LabeledAxisAccessor &name, Size sz)
Add a variable with known storage size.
Definition LabeledAxis.cxx:58
std::size_t nsubaxis() const
Definition LabeledAxis.cxx:313
LabeledAxis()=default
Empty constructor.
bool is_setup() const
Whether the axis has been set up.
Definition LabeledAxis.h:56
bool has_variable(const LabeledAxisAccessor &name) const
Check the existence of a variable by its name.
Definition LabeledAxis.cxx:231
const std::vector< std::pair< Size, Size > > & variable_slices() const
Get the variable slicing indices (in assembly order)
Definition LabeledAxis.cxx:270
const std::vector< Size > & variable_sizes() const
Get the variable storage sizes (in assembly order)
Definition LabeledAxis.cxx:284
bool has_forces() const
Definition LabeledAxis.h:65
friend std::ostream & operator<<(std::ostream &os, const LabeledAxis &axis)
Definition LabeledAxis.cxx:473
std::size_t nvariable() const
Definition LabeledAxis.cxx:217
const std::pair< Size, Size > & variable_slice(const LabeledAxisAccessor &name) const
Get the slicing indices of a variable by name.
Definition LabeledAxis.cxx:277
bool has_old_forces() const
Definition LabeledAxis.h:66
bool has_residual() const
Definition LabeledAxis.h:67
const std::vector< std::pair< Size, Size > > & subaxis_slices() const
Get the sub-axis slicing indices (in assembly order)
Definition LabeledAxis.cxx:381
LabeledAxis & add_subaxis(const std::string &name)
Add a sub-axis.
Definition LabeledAxis.cxx:45
bool has_subaxis(const LabeledAxisAccessor &name) const
Check the existence of a subaxis by its name.
Definition LabeledAxis.cxx:319
std::pair< Size, Size > subaxis_slice(const LabeledAxisAccessor &name) const
Get the slicing indices of a sub-axis by name.
Definition LabeledAxis.cxx:388
indexing::Slice slice(const LabeledAxisAccessor &name) const
Get the slicing indices of a variable or a local sub-axis.
Definition LabeledAxis.cxx:194
void setup_layout()
Setup the layout of all items recursively.
Definition LabeledAxis.cxx:87
Size subaxis_size(const LabeledAxisAccessor &name) const
Get the storage size of a sub-axis by name.
Definition LabeledAxis.cxx:413
const std::vector< LabeledAxisAccessor > & variable_names() const
Get the variable names.
Definition LabeledAxis.cxx:263
std::size_t variable_id(const LabeledAxisAccessor &name) const
Get the assembly ID of a variable.
Definition LabeledAxis.cxx:249
Size variable_size(const LabeledAxisAccessor &name) const
Get the storage size of a variable by name.
Definition LabeledAxis.cxx:291
Size size() const
Get the storage size of the entire axis.
Definition LabeledAxis.cxx:147
const std::vector< Size > & subaxis_sizes() const
Get the sub-axis storage sizes (in assembly order)
Definition LabeledAxis.cxx:406
bool has_parameters() const
Definition LabeledAxis.h:68
bool has_old_state() const
Definition LabeledAxis.h:64
std::size_t subaxis_id(const std::string &name) const
Get the assembly ID of a sub-axis.
Definition LabeledAxis.cxx:332
LabeledAxisAccessor qualify(const LabeledAxisAccessor &accessor) const
Return the fully qualified name of an item (i.e. this axis is a sub-axis)
Definition LabeledAxis.cxx:39
const std::vector< std::string > & subaxis_names() const
Get the sub-axis names.
Definition LabeledAxis.cxx:374
bool has_state() const
Definition LabeledAxis.h:63
const LabeledAxis & subaxis(const LabeledAxisAccessor &name) const
Get a sub-axis by name.
Definition LabeledAxis.cxx:352
const std::vector< const LabeledAxis * > & subaxes() const
Get the sub-axes (in assembly order)
Definition LabeledAxis.cxx:345
Definition DiagnosticsInterface.cxx:30
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
bool operator==(const LabeledAxis &a, const LabeledAxis &b)
Definition LabeledAxis.cxx:503
bool operator!=(const LabeledAxis &a, const LabeledAxis &b)
Definition LabeledAxis.cxx:509
int64_t Size
Definition types.h:69
std::ostream & operator<<(std::ostream &os, const EnumSelection &es)
Definition EnumSelection.cxx:32