IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
igabase.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <filesystem>
18#include <vector>
19
20#include <iganet/core/core.hpp>
21
22namespace iganet {
23
29protected:
35 template <typename T>
36 inline void read_from_xml(const std::string &location, T &obj,
37 std::vector<torch::Tensor> &v) {
38
39 std::filesystem::path path(location);
40
41 if (std::filesystem::exists(path)) {
42 if (std::filesystem::is_regular_file(path)) {
43 try {
44 pugi::xml_document doc;
45 doc.load_file(path.c_str());
46 v.emplace_back(obj.from_xml(doc).as_tensor());
47 } catch (...) {
48 }
49 } else if (std::filesystem::is_directory(path)) {
50 for (const auto &file : std::filesystem::directory_iterator(path)) {
51 if (file.is_regular_file() && file.path().extension() == ".xml") {
52 try {
53 pugi::xml_document doc;
54 doc.load_file(file.path().c_str());
55 v.emplace_back(obj.from_xml(doc).as_tensor());
56 } catch (...) {
57 }
58 }
59 }
60 } else
61 throw std::runtime_error(
62 "The path refers to neither a file nor a directory");
63 } else
64 throw std::runtime_error("The path does not exist");
65 }
66};
67
73template <bool solution = false> class IgADataset;
74
75template <>
76class IgADataset<false>
77 : public IgADatasetBase,
78 public torch::data::Dataset<
79 IgADataset<false>,
80 torch::data::Example<torch::Tensor, torch::data::example::NoTarget>> {
81private:
83 std::vector<torch::Tensor> G_;
84
86 std::vector<torch::Tensor> f_;
87
88public:
91 torch::data::Example<torch::Tensor, torch::data::example::NoTarget>;
92
98 template <typename T> void add_geometryMap(T &obj, std::string location) {
99 read_from_xml(location, obj, G_);
100 }
101
107 template <typename T> void add_geometryMap(T &&obj, std::string location) {
108 read_from_xml(location, obj, G_);
109 }
111
119 template <typename T>
120 void add_geometryMap(T &obj, const pugi::xml_document &doc, int id = 0,
121 const std::string &label = "") {
122 G_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
123 }
124
131 template <typename T>
132 void add_geometryMap(T &&obj, const pugi::xml_document &doc, int id = 0,
133 const std::string &label = "") {
134 G_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
135 }
137
145 template <typename T>
146 void add_geometryMap(T &obj, const pugi::xml_node &root, int id = 0,
147 const std::string &label = "") {
148 G_.emplace_back(obj.from_xml(root, id, label).as_tensor());
149 }
150
157 template <typename T>
158 void add_geometryMap(T &&obj, const pugi::xml_node &root, int id = 0,
159 const std::string &label = "") {
160 G_.emplace_back(obj.from_xml(root, id, label).as_tensor());
161 }
163
169 template <typename T> void add_referenceData(T &obj, std::string location) {
170 read_from_xml(location, obj, f_);
171 }
172
178 template <typename T> void add_referenceData(T &&obj, std::string location) {
179 read_from_xml(location, obj, f_);
180 }
182
190 template <typename T>
191 void add_referenceData(T &obj, const pugi::xml_document &doc, int id = 0,
192 const std::string &label = "") {
193 f_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
194 }
195
202 template <typename T>
203 void add_referenceData(T &&obj, const pugi::xml_document &doc, int id = 0,
204 const std::string &label = "") {
205 f_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
206 }
208
216 template <typename T>
217 void add_referenceData(T &obj, const pugi::xml_node &root, int id = 0,
218 const std::string &label = "") {
219 f_.emplace_back(obj.from_xml(root, id, label).as_tensor());
220 }
221
228 template <typename T>
229 void add_referenceData(T &&obj, const pugi::xml_node &root, int id = 0,
230 const std::string &label = "") {
231 f_.emplace_back(obj.from_xml(root, id, label).as_tensor());
232 }
234
241 template <typename T, typename Func>
242 void add_referenceData(T &obj, Func func) {
243 f_.emplace_back(obj.transform(func).as_tensor());
244 }
245
253 template <typename T, typename Func>
254 void add_referenceData(T &&obj, Func func) {
255 f_.emplace_back(obj.transform(func).as_tensor());
256 }
258
262 inline example_type get(std::size_t index) override {
263
264 std::size_t geo_index = index / (f_.empty() ? 1 : f_.size());
265 std::size_t ref_index = index - geo_index * f_.size();
266
267 if (!G_.empty()) {
268 if (!f_.empty())
269 return torch::cat({G_.at(geo_index), f_.at(ref_index)});
270 else
271 return G_.at(geo_index);
272 } else {
273 if (!f_.empty())
274 return f_.at(ref_index);
275 else
276 throw std::runtime_error("No geometry maps and reference data");
277 }
278 };
281
284 // @brief Return the total size of the data set
285 [[nodiscard]] inline torch::optional<std::size_t> size() const override {
286 return (G_.empty() ? 1 : G_.size()) * (f_.empty() ? 1 : f_.size());
287 }
288};
289
290template <>
291class IgADataset<true>
292 : public IgADatasetBase,
293 public torch::data::Dataset<IgADataset<true>, torch::data::Example<>> {
294private:
296 std::vector<torch::Tensor> G_;
297
299 std::vector<torch::Tensor> f_;
300
302 std::vector<torch::Tensor> u_;
303
304public:
310 template <typename T> void add_geometryMap(T &obj, std::string location) {
311 read_from_xml(location, obj, G_);
312 }
313
319 template <typename T> void add_geometryMap(T &&obj, std::string location) {
320 read_from_xml(location, obj, G_);
321 }
323
331 template <typename T>
332 void add_geometryMap(T &obj, const pugi::xml_document &doc, int id = 0,
333 const std::string &label = "") {
334 G_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
335 }
336
343 template <typename T>
344 void add_geometryMap(T &&obj, const pugi::xml_document &doc, int id = 0,
345 const std::string &label = "") {
346 G_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
347 }
349
357 template <typename T>
358 void add_geometryMap(T &obj, const pugi::xml_node &root, int id = 0,
359 const std::string &label = "") {
360 G_.emplace_back(obj.from_xml(root, id, label).as_tensor());
361 }
362
369 template <typename T>
370 void add_geometryMap(T &&obj, const pugi::xml_node &root, int id = 0,
371 const std::string &label = "") {
372 G_.emplace_back(obj.from_xml(root, id, label).as_tensor());
373 }
375
381 template <typename T> void add_referenceData(T &obj, std::string location) {
382 read_from_xml(location, obj, f_);
383 }
384
390 template <typename T> void add_referenceData(T &&obj, std::string location) {
391 read_from_xml(location, obj, f_);
392 }
394
402 template <typename T>
403 void add_referenceData(T &obj, const pugi::xml_document &doc, int id = 0,
404 const std::string &label = "") {
405 f_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
406 }
407
414 template <typename T>
415 void add_referenceData(T &&obj, const pugi::xml_document &doc, int id = 0,
416 const std::string &label = "") {
417 f_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
418 }
420
428 template <typename T>
429 void add_referenceData(T &obj, const pugi::xml_node &root, int id = 0,
430 const std::string &label = "") {
431 f_.emplace_back(obj.from_xml(root, id, label).as_tensor());
432 }
433
440 template <typename T>
441 void add_referenceData(T &&obj, const pugi::xml_node &root, int id = 0,
442 const std::string &label = "") {
443 f_.emplace_back(obj.from_xml(root, id, label).as_tensor());
444 }
446
453 template <typename T, typename Func>
454 void add_referenceData(T &obj, Func func) {
455 f_.emplace_back(obj.transform(func).as_tensor());
456 }
457
465 template <typename T, typename Func>
466 void add_referenceData(T &&obj, Func func) {
467 f_.emplace_back(obj.transform(func).as_tensor());
468 }
470
476 template <typename T> void add_solution(T &obj, std::string location) {
477 read_from_xml(location, obj, u_);
478 }
479
485 template <typename T> void add_solution(T &&obj, std::string location) {
486 read_from_xml(location, obj, u_);
487 }
489
497 template <typename T>
498 void add_solution(T &obj, const pugi::xml_document &doc, int id = 0,
499 const std::string &label = "") {
500 u_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
501 }
502
509 template <typename T>
510 void add_solution(T &&obj, const pugi::xml_document &doc, int id = 0,
511 const std::string &label = "") {
512 u_.emplace_back(obj.from_xml(doc.child("xml"), id, label).as_tensor());
513 }
515
523 template <typename T>
524 void add_solution(T &obj, const pugi::xml_node &root, int id = 0,
525 const std::string &label = "") {
526 u_.emplace_back(obj.from_xml(root, id, label).as_tensor());
527 }
528
535 template <typename T>
536 void add_solution(T &&obj, const pugi::xml_node &root, int id = 0,
537 const std::string &label = "") {
538 u_.emplace_back(obj.from_xml(root, id, label).as_tensor());
539 }
541
545 inline torch::data::Example<> get(std::size_t index) override {
546
547 std::size_t geo_index = index / (f_.empty() ? 1 : f_.size());
548 std::size_t ref_index = index - geo_index * f_.size();
549
550 if (!G_.empty()) {
551 if (!f_.empty())
552 return {torch::cat({G_.at(geo_index), f_.at(ref_index)}), u_.at(index)};
553 else
554 return {G_.at(geo_index), u_.at(index)};
555 } else {
556 if (!f_.empty())
557 return {f_.at(ref_index), u_.at(index)};
558 else
559 throw std::runtime_error("No geometry maps and reference data");
560 }
561 };
562
565 // @brief Return the total size of the data set
566 [[nodiscard]] inline torch::optional<std::size_t> size() const override {
567 return (G_.empty() ? 1 : G_.size()) * (f_.empty() ? 1 : f_.size());
568 }
569};
571
572} // namespace iganet
void add_referenceData(T &&obj, std::string location)
Provides the add_referenceData operation.
Definition igabase.hpp:178
void add_referenceData(T &obj, Func func)
Adds a reference data set from XML node.
Definition igabase.hpp:242
void add_referenceData(T &obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Adds a reference data set from XML object.
Definition igabase.hpp:191
std::vector< torch::Tensor > G_
Vector of tensors representing the geometry maps.
Definition igabase.hpp:83
void add_referenceData(T &&obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Provides the add_referenceData operation.
Definition igabase.hpp:203
std::vector< torch::Tensor > f_
Vector of tensors representing the reference data.
Definition igabase.hpp:86
void add_referenceData(T &obj, std::string location)
Adds a reference data set from file.
Definition igabase.hpp:169
void add_referenceData(T &obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Adds a reference data set from XML node.
Definition igabase.hpp:217
void add_geometryMap(T &&obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Provides the add_geometryMap operation.
Definition igabase.hpp:132
void add_geometryMap(T &&obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Provides the add_geometryMap operation.
Definition igabase.hpp:158
void add_referenceData(T &&obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Provides the add_referenceData operation.
Definition igabase.hpp:229
void add_geometryMap(T &obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Adds a geometry map from XML object.
Definition igabase.hpp:120
void add_geometryMap(T &&obj, std::string location)
Provides the add_geometryMap operation.
Definition igabase.hpp:107
torch::data::Example< torch::Tensor, torch::data::example::NoTarget > example_type
Example type.
Definition igabase.hpp:91
void add_geometryMap(T &obj, std::string location)
Adds a geometry map from file.
Definition igabase.hpp:98
void add_referenceData(T &&obj, Func func)
Provides the add_referenceData operation.
Definition igabase.hpp:254
example_type get(std::size_t index) override
Returns the data set at location index.
Definition igabase.hpp:262
torch::optional< std::size_t > size() const override
Provides the size operation.
Definition igabase.hpp:285
void add_geometryMap(T &obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Adds a geometry map from XML node.
Definition igabase.hpp:146
torch::optional< std::size_t > size() const override
Provides the size operation.
Definition igabase.hpp:566
void add_referenceData(T &obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Adds a reference data set from XML object.
Definition igabase.hpp:403
std::vector< torch::Tensor > G_
Vector of tensors representing the geometry maps.
Definition igabase.hpp:296
void add_referenceData(T &&obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Provides the add_referenceData operation.
Definition igabase.hpp:415
void add_solution(T &obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Adds a solution from XML node.
Definition igabase.hpp:524
void add_referenceData(T &obj, Func func)
Adds a reference data set from XML node.
Definition igabase.hpp:454
void add_solution(T &&obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Provides the add_solution operation.
Definition igabase.hpp:536
std::vector< torch::Tensor > f_
Vector of tensors representing the reference data.
Definition igabase.hpp:299
void add_geometryMap(T &obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Adds a geometry map from XML node.
Definition igabase.hpp:358
void add_referenceData(T &&obj, Func func)
Provides the add_referenceData operation.
Definition igabase.hpp:466
void add_solution(T &&obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Provides the add_solution operation.
Definition igabase.hpp:510
void add_solution(T &obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Adds a solution from XML object.
Definition igabase.hpp:498
void add_geometryMap(T &&obj, std::string location)
Provides the add_geometryMap operation.
Definition igabase.hpp:319
void add_referenceData(T &&obj, std::string location)
Provides the add_referenceData operation.
Definition igabase.hpp:390
torch::data::Example get(std::size_t index) override
Returns the data set at location index.
Definition igabase.hpp:545
void add_referenceData(T &obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Adds a reference data set from XML node.
Definition igabase.hpp:429
void add_referenceData(T &obj, std::string location)
Adds a reference data set from file.
Definition igabase.hpp:381
void add_geometryMap(T &&obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Provides the add_geometryMap operation.
Definition igabase.hpp:370
void add_geometryMap(T &obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Adds a geometry map from XML object.
Definition igabase.hpp:332
void add_geometryMap(T &&obj, const pugi::xml_document &doc, int id=0, const std::string &label="")
Provides the add_geometryMap operation.
Definition igabase.hpp:344
void add_solution(T &obj, std::string location)
Adds a solution from file.
Definition igabase.hpp:476
void add_solution(T &&obj, std::string location)
Provides the add_solution operation.
Definition igabase.hpp:485
void add_referenceData(T &&obj, const pugi::xml_node &root, int id=0, const std::string &label="")
Provides the add_referenceData operation.
Definition igabase.hpp:441
std::vector< torch::Tensor > u_
Vector of tensors representing the solution data.
Definition igabase.hpp:302
void add_geometryMap(T &obj, std::string location)
Adds a geometry map from file.
Definition igabase.hpp:310
IgA dataset base class.
Definition igabase.hpp:28
void read_from_xml(const std::string &location, T &obj, std::vector< torch::Tensor > &v)
Reads a function space from file.
Definition igabase.hpp:36
Core components.
Definition core.hpp:73
IgA dataset class.
Definition igabase.hpp:73