https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchObservationHistoryHelper.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
10#ifdef MOOSE_LIBTORCH_ENABLED
11
13
14#include "MooseError.h"
15
16#include <algorithm>
17#include "libmesh/utility.h"
18
20 const unsigned int input_timesteps)
21 : _input_timesteps(input_timesteps)
22{
23 if (_input_timesteps == 0)
24 mooseError("Observation history requires at least one input timestep.");
25}
26
27void
29 const std::vector<std::vector<Real>> & component_trajectories) const
30{
31 if (component_trajectories.empty())
32 return;
33
34 const auto trajectory_size = component_trajectories.front().size();
35 for (const auto & trajectory : component_trajectories)
36 if (trajectory.size() != trajectory_size)
37 mooseError("Observation trajectories must all have the same number of timesteps.");
38}
39
40void
42 const std::vector<Real> & observation, std::vector<std::vector<Real>> & old_observations) const
43{
44 old_observations.assign(_input_timesteps - 1, observation);
45}
46
47void
49 const std::vector<Real> & observation, std::vector<std::vector<Real>> & old_observations) const
50{
51 if (old_observations.empty())
52 return;
53
54 std::rotate(old_observations.rbegin(), old_observations.rbegin() + 1, old_observations.rend());
55 old_observations[0] = observation;
56}
57
58std::vector<Real>
60 const std::vector<Real> & observation_factors) const
61{
62 if (observation_factors.empty())
63 return {};
64
65 std::vector<Real> expanded;
66 expanded.reserve(observation_factors.size() * _input_timesteps);
67
68 for (const auto lag : make_range(_input_timesteps))
69 {
70 libmesh_ignore(lag);
71 expanded.insert(expanded.end(), observation_factors.begin(), observation_factors.end());
72 }
73
74 return expanded;
75}
76
77std::vector<Real>
79 const std::vector<Real> & observation,
80 const std::vector<std::vector<Real>> & old_observations) const
81{
82 const auto expected_history_size = _input_timesteps - 1;
83 if (old_observations.size() != expected_history_size)
84 mooseError("Observation history must contain ",
85 expected_history_size,
86 " stored entries, but got ",
87 old_observations.size(),
88 ".");
89
90 std::vector<Real> stacked;
91 stacked.reserve(observation.size() * _input_timesteps);
92
93 stacked.insert(stacked.end(), observation.begin(), observation.end());
94
95 for (const auto & history_entry : old_observations)
96 {
97 if (history_entry.size() != observation.size())
98 mooseError("Observation history entries must have the same size as the current "
99 "observation.");
100 stacked.insert(stacked.end(), history_entry.begin(), history_entry.end());
101 }
102
103 return stacked;
104}
105
106std::vector<Real>
108 const std::vector<std::vector<Real>> & component_trajectories,
109 const unsigned int time_index) const
110{
111 validateTrajectoryShape(component_trajectories);
112
113 if (component_trajectories.empty())
114 return {};
115
116 const auto trajectory_size = component_trajectories.front().size();
117 if (time_index >= trajectory_size)
118 mooseError("Requested observation time index is out of range.");
119
120 std::vector<Real> stacked;
121 stacked.reserve(component_trajectories.size() * _input_timesteps);
122
123 for (const auto lag : make_range(_input_timesteps))
124 {
125 const auto source_index = time_index > lag ? time_index - lag : 0;
126 for (const auto component_i : make_range(component_trajectories.size()))
127 stacked.push_back(component_trajectories[component_i][source_index]);
128 }
129
130 return stacked;
131}
132
133#endif
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
void initializeHistory(const std::vector< Real > &observation, std::vector< std::vector< Real > > &old_observations) const
Fill the history buffer with copies of the current observation.
std::vector< Real > expandObservationFactors(const std::vector< Real > &observation_factors) const
Repeat per-observation-entry factors across all stacked timesteps.
void advanceHistory(const std::vector< Real > &observation, std::vector< std::vector< Real > > &old_observations) const
Advance the history buffer by inserting the latest observation.
const unsigned int _input_timesteps
Number of timesteps stacked into each flattened observation.
std::vector< Real > stackCurrentObservation(const std::vector< Real > &observation, const std::vector< std::vector< Real > > &old_observations) const
Flatten the current observation together with its stored history.
std::vector< Real > stackTrajectoryObservation(const std::vector< std::vector< Real > > &component_trajectories, unsigned int time_index) const
Flatten one time slice of observation-component trajectories with causal history.
void validateTrajectoryShape(const std::vector< std::vector< Real > > &component_trajectories) const
Check that all observation-component trajectories have a consistent shape.
LibtorchObservationHistoryHelper(unsigned int input_timesteps)
Build an observation-history helper for libtorch inputs.