Chi-Tech
field_copy.cc
Go to the documentation of this file.
1#include "field_copy.h"
2
3#include "ChiObjectFactory.h"
6
8{
9
11
12// ##################################################################
13/**Returns the input parameters.*/
15{
17
18 params.SetDocGroup("DocFieldOperation");
19
20 params.AddRequiredParameter<size_t>(
21 "to",
22 "Handle to the field function that should "
23 "receive the result of the operation.");
24
25 params.AddRequiredParameter<size_t>(
26 "from",
27 "Handle to the field function that should "
28 "be copied from.");
29
31 "to_components",
32 std::vector<size_t>{},
33 "List of component numbers that need to "
34 "be written to. If this parameter is supplied then"
35 "\"from_components\" also need to be specified (and vice versa) and they "
36 "need to be the same length. If neither are supplied, all components are "
37 "copied.");
38
40 "from_components",
41 std::vector<size_t>{},
42 "List of component numbers that need to "
43 "read from. If this parameter is supplied then"
44 "\"to_components\" also need to be specified (and vice versa) and they "
45 "need to be the same length. If neither are supplied, all components are "
46 "copied.");
47
48 return params;
49}
50
51// ##################################################################
52/**Constructor.*/
54 const chi::InputParameters& params)
55 : FieldOperation(params),
56 to_field_handle_(params.GetParamValue<size_t>("to")),
57 from_field_handle_(params.GetParamValue<size_t>("from"))
58{
59 //============================================= Get field functions
60 {
61 auto to_base_ptr = Chi::GetStackItemPtr(
63
64 to_ff_ = std::dynamic_pointer_cast<FieldFunctionGridBased>(to_base_ptr);
65
67 "\"to\" field function must be based on "
68 "FieldFunctionGridBased");
69 }
70
71 {
72 auto from_base_ptr = Chi::GetStackItemPtr(
74
75 from_ff_ = std::dynamic_pointer_cast<FieldFunctionGridBased>(from_base_ptr);
76
78 "\"to\" field function must be based on "
79 "FieldFunctionGridBased");
80 }
81
82 // ============================================ Check number of components
83 // are compatible
84 const auto& user_supplied_params = params.ParametersAtAssignment();
85
86 if (user_supplied_params.Has("to_components") and
87 (not user_supplied_params.Has("from_components")))
88 ChiInvalidArgument("If \"to_components\" is specified then "
89 "\"from_components\" must also be specified");
90
91 if (user_supplied_params.Has("from_components") and
92 (not user_supplied_params.Has("to_components")))
93 ChiInvalidArgument("If \"from_components\" is specified then "
94 "\"to_components\" must also be specified");
95
96 if (user_supplied_params.Has("to_components") and
97 user_supplied_params.Has("from_components"))
98 {
100 user_supplied_params.GetParamVectorValue<size_t>("to_components");
102 user_supplied_params.GetParamVectorValue<size_t>("from_components");
103
105 "\"to_components\" and \"from_components\" must have "
106 "the same number of entries");
107 }
108 else
109 {
111 to_ff_->GetUnknownManager().GetTotalUnknownStructureSize() !=
112 from_ff_->GetUnknownManager().GetTotalUnknownStructureSize(),
113 "The number of components of the unknowns in the field functions are"
114 " not compatible");
115
116 const size_t num_comps =
117 to_ff_->GetUnknownManager().GetTotalUnknownStructureSize();
118 to_components_.reserve(num_comps);
119 from_components_.reserve(num_comps);
120 for (size_t c = 0; c < num_comps; ++c)
121 {
122 to_components_.push_back(c);
123 from_components_.push_back(c);
124 }
125 }
126
127 //============================================= Check grids are compatible
128 ChiInvalidArgumentIf(std::addressof(to_ff_->GetSpatialDiscretization().Grid()) !=
129 std::addressof(from_ff_->GetSpatialDiscretization().Grid()),
130 "Currently the two field functions must operate on the "
131 "same grid");
132}
133
135{
136 typedef const int64_t cint64_t;
137 const auto& sdm = to_ff_->GetSpatialDiscretization();
138 const auto& uk_man = to_ff_->GetUnknownManager();
139 const auto& grid = sdm.Grid();
140
141 const size_t num_comps = to_components_.size();
142
143 for (const auto& cell : grid.local_cells)
144 {
145 const auto& cell_mapping = sdm.GetCellMapping(cell);
146 const size_t num_nodes = cell_mapping.NumNodes();
147
148 const auto nodes_xyz = cell_mapping.GetNodeLocations();
149
150 for (size_t i = 0; i < num_nodes; ++i)
151 {
152 for (size_t c = 0; c < num_comps; ++c)
153 {
154 const size_t cto = to_components_[c];
155 const size_t cfrom = from_components_[c];
156
157 const double value = from_ff_->Evaluate(cell, nodes_xyz[i], cfrom);
158
159 cint64_t dof_map = sdm.MapDOFLocal(cell, i, uk_man, 0, cto);
160
161 to_ff_->FieldVector()[dof_map] = value;
162 } // for component c
163 } // for node i
164 } // for cell
165}
166
167} // namespace chi_physics::field_operations
#define ChiLogicalErrorIf(condition, message)
#define ChiInvalidArgumentIf(condition, message)
#define ChiInvalidArgument(message)
static std::vector< chi_physics::FieldFunctionPtr > field_function_stack
Definition: chi_runtime.h:92
static std::shared_ptr< T > & GetStackItemPtr(std::vector< std::shared_ptr< T > > &stack, const size_t handle, const std::string &calling_function_name="Unknown")
Definition: chi_runtime.h:258
void SetDocGroup(const std::string &doc_group)
void AddRequiredParameter(const std::string &name, const std::string &doc_string)
const ParameterBlock & ParametersAtAssignment() const
void AddOptionalParameterArray(const std::string &name, const std::vector< T > &array, const std::string &doc_string)
std::shared_ptr< const FieldFunctionGridBased > from_ff_
Definition: field_copy.h:22
FieldCopyOperation(const chi::InputParameters &params)
Definition: field_copy.cc:53
std::shared_ptr< FieldFunctionGridBased > to_ff_
Definition: field_copy.h:21
static chi::InputParameters GetInputParameters()
Definition: field_copy.cc:14
static chi::InputParameters GetInputParameters()
RegisterChiObject(chi_physics::field_operations, FieldCopyOperation)