NEML2 1.4.0
Loading...
Searching...
No Matches
ComposedModel.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 "neml2/models/Model.h"
28#include "neml2/base/DependencyResolver.h"
29
30#include <future>
31
32namespace neml2
33{
34class ComposedModel : public Model
35{
36public:
38
40
41 virtual void check_AD_limitation() const override;
42
43 virtual std::map<std::string, const VariableBase *>
44 named_nonlinear_parameters(bool recursive = false) const override;
45
46 virtual std::map<std::string, Model *>
47 named_nonlinear_parameter_models(bool recursive = false) const override;
48
49 virtual void setup() override;
50
51 using AssemblyIndices = std::map<indexing::TensorIndex,
52 std::pair<indexing::TensorIndex, Model *>,
54
55protected:
56 virtual void allocate_variables(bool in, bool out) override;
57
65 virtual void setup_submodel_input_views(VariableStore * host) override;
66
67 virtual void setup_output_views() override;
68
69 void set_value(bool, bool, bool) override;
70
71private:
73 AssemblyIndices consolidate(const AssemblyIndices & indices) const;
74
76 void set_value_async(Model * i, bool out, bool dout_din, bool d2out_din2);
77
79 void apply_chain_rule(Model * i);
80
82 void apply_second_order_chain_rule(Model * i);
83
85 void rethrow_exceptions() const;
86
88 const std::vector<VariableName> _additional_outputs;
89
91 const bool _auto_nl_param;
92
95
97 std::map<Model *, AssemblyIndices> _assembly_indices;
98
100 std::map<Model *, std::vector<Tensor>> _dpin_din_views;
101
103 std::map<Model *, LabeledMatrix> _dpout_din;
104
106 std::map<Model *, std::vector<Tensor>> _d2pin_din2_views;
107
109 std::map<Model *, LabeledTensor3D> _d2pout_din2;
110
112 std::map<Model *, std::future<void>> _async_results;
113
115 std::map<std::thread::id, std::exception_ptr> _async_exceptions;
116};
117} // namespace neml2
Definition ComposedModel.h:35
virtual std::map< std::string, const VariableBase * > named_nonlinear_parameters(bool recursive=false) const override
Get all nonlinear parameters.
Definition ComposedModel.cxx:239
virtual void allocate_variables(bool in, bool out) override
Call VariableStore::allocate_variables recursively on all submodels.
Definition ComposedModel.cxx:251
std::map< indexing::TensorIndex, std::pair< indexing::TensorIndex, Model * >, LabeledAxis::AssemblySliceCmp > AssemblyIndices
Definition ComposedModel.h:51
virtual std::map< std::string, Model * > named_nonlinear_parameter_models(bool recursive=false) const override
Get all nonlinear parameters' models.
Definition ComposedModel.cxx:245
virtual void check_AD_limitation() const override
Definition ComposedModel.cxx:230
virtual void setup_submodel_input_views(VariableStore *host) override
Definition ComposedModel.cxx:303
virtual void setup() override
Setup this model.
Definition ComposedModel.cxx:151
static OptionSet expected_options()
Definition ComposedModel.cxx:34
virtual void setup_output_views() override
Definition ComposedModel.cxx:277
void set_value(bool, bool, bool) override
The map between input -> output, and optionally its derivatives.
Definition ComposedModel.cxx:323
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
The base class for all constitutive models.
Definition Model.h:55
friend class ComposedModel
ComposedModel's set_value need to call submodel's set_value.
Definition Model.h:169
const torch::TensorOptions & options() const
Storage tensor options.
Definition Model.h:111
const T * host() const
Get a readonly pointer to the host.
Definition NEML2Object.h:90
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:100
Definition VariableStore.h:40
Definition CrossRef.cxx:30
Definition LabeledAxis.h:61