Debian Codegraph @

packages/seacas/applications/exomatlab/exomatlab.C

raw
1 /*
2 * Copyright(C) 1999-2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include <cassert>
10 #include <cstdlib>
11 #include <cstring>
12 #include <ctime>
13 #include <exception>
14 #include <fstream>
15 #include <iomanip>
16 #include <iostream>
17 #include <stdexcept>
18 #include <string>
19
20 #include "EML_CodeTypes.h"
21 #include "EML_SystemInterface.h"
22
23 #include <Ionit_Initializer.h>
24 #include <Ioss_CodeTypes.h>
25 #include <Ioss_SubSystem.h>
26 #include <Ioss_SurfaceSplit.h>
27 #include <Ioss_Utils.h>
28
29 #include "add_to_log.h"
30 #include "fmt/ostream.h"
31
32 #ifdef SEACAS_HAVE_MPI
33 #include <mpi.h>
34 #endif
35
36 // ========================================================================
37 namespace {
38 bool file_info(const std::string &inpfile, const std::string &input_type,
39 SystemInterface &interFace);
40
41 void output_names(const std::string &type, const Ioss::NameList &fields,
42 Ioss::GroupingEntity *entity)
43 {
44 fmt::print("\n{} variables on exodus data base:\n", type);
45 for (const auto &field_name : fields) {
46 const Ioss::VariableType *var_type = entity->get_field(field_name).raw_storage();
47 fmt::print("\t{}\t{}\n", field_name, var_type->name());
48 }
49 }
50 } // namespace
51 // ========================================================================
52
53 namespace {
54 std::string codename;
55 } // namespace
56
57 int main(int argc, char *argv[])
58 {
59 #ifdef SEACAS_HAVE_MPI
60 MPI_Init(&argc, &argv);
61 #endif
62 time_t begin_time = time(nullptr);
63 std::string in_type = "exodusII";
64
65 bool ok = false;
66 codename = argv[0];
67 size_t ind = codename.find_last_of('/', codename.size());
68 if (ind != std::string::npos) {
69 codename = codename.substr(ind + 1, codename.size());
70 }
71
72 try {
73 SystemInterface::show_version();
74 Ioss::Init::Initializer io;
75
76 SystemInterface interFace;
77 ok = interFace.parse_options(argc, argv);
78
79 if (ok) {
80 std::string in_file = interFace.input_file();
81 std::string output_file = interFace.output_file();
82
83 fmt::print("Input: '{}', Type: {}\n", in_file, in_type);
84 fmt::print("Output: '{}', Type: matlab script\n\n", output_file);
85
86 ok = file_info(in_file, in_type, interFace);
87 }
88 std::string success = ok ? "successful" : "unsuccessful";
89 fmt::print("\n{} execution {}.\n", codename, success);
90 }
91 catch (std::exception &e) {
92 fmt::print("ERROR: (EXOMATLAB) Standard exception: {}\n", e.what());
93 }
94 time_t end_time = time(nullptr);
95 add_to_log(codename.c_str(), (int)(end_time - begin_time));
96 #ifdef SEACAS_HAVE_MPI
97 MPI_Finalize();
98 #endif
99 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
100 }
101
102 namespace {
103 bool file_info(const std::string &inpfile, const std::string &input_type,
104 SystemInterface &interFace)
105 {
106 //========================================================================
107 // INPUT ...
108 // NOTE: The "READ_RESTART" mode ensures that the node and element ids will be mapped.
109 //========================================================================
110 Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(input_type, inpfile, Ioss::READ_RESTART,
111 Ioss::ParallelUtils::comm_world());
112 if (dbi == nullptr || !dbi->ok(true)) {
113 return false;
114 }
115
116 dbi->set_field_separator(interFace.field_suffix());
117 dbi->set_lower_case_variable_names(false);
118
119 // NOTE: 'region' owns 'db' pointer at this time...
120 Ioss::Region region(dbi, "region_1");
121
122 if (interFace.list_vars()) {
123 StringIdVector types_to_list = interFace.vars_to_list();
124 for (const auto &types : types_to_list) {
125 std::string type = types.first;
126
127 if (type == "all" || type == "global") {
128 Ioss::NameList fields = region.field_describe(Ioss::Field::REDUCTION);
129 output_names("Global", fields, &region);
130 }
131 if (type == "all" || type == "nodal") {
132 Ioss::NodeBlock *nb = region.get_node_blocks()[0];
133 Ioss::NameList fields = nb->field_describe(Ioss::Field::TRANSIENT);
134 output_names("Nodal", fields, nb);
135 }
136 }
137 return true;
138 }
139
140 Ioss::NameList fields;
141 StringIdVector global_vars = interFace.global_var_names();
142 if (!global_vars.empty()) {
143 if (global_vars[0].first == "all") {
144 region.field_describe(Ioss::Field::REDUCTION, &fields);
145 }
146 else if (global_vars[0].first == "none") {
147 ; // do nothing. This will be used when nodal, element, ... supported
148 }
149 else {
150 for (const auto &global_var : global_vars) {
151 std::string field_name = global_var.first;
152 if (region.field_exists(field_name)) {
153 fields.push_back(field_name);
154 }
155 else {
156 fmt::print("WARNING: Global variable named '{}' does not exist; it will be skipped.\n",
157 field_name);
158 ;
159 }
160 }
161 }
162 }
163 else {
164 region.field_describe(Ioss::Field::REDUCTION, &fields);
165 }
166
167 if (fields.empty()) {
168 fmt::print("No variables selected; no output will be written\n");
169 return false;
170 }
171
172 std::ofstream out_stream;
173 out_stream.open(interFace.output_file().c_str());
174
175 out_stream.setf(std::ios::scientific);
176 out_stream.setf(std::ios::showpoint);
177
178 fmt::print(out_stream, "% number of curves\nnvars = {}\n", fields.size() + 1);
179
180 size_t namelen = 4; // length of 'time'
181 for (const auto &field_name : fields) {
182 if (field_name.length() > namelen) {
183 namelen = field_name.length();
184 }
185 }
186
187 fmt::print(out_stream, "names= [\n'{:<{}}';\n", "TIME", namelen);
188
189 for (const auto &field_name : fields) {
190 fmt::print(out_stream, "'{:<{}}';\n", field_name, namelen);
191 }
192 fmt::print(out_stream, "];\n");
193
194 // Get number of timesteps...
195 int num_steps = region.get_optional_property("state_count", 0);
196 if (num_steps == 0) {
197 fmt::print(out_stream, "GENESIS file -- no time steps written\n");
198 return false;
199 }
200
201 // ========================================================================
202 // Calculate min and max times to extract data...
203 int st_min = 1;
204 int st_max = num_steps;
205
206 double tmin = interFace.minimum_time();
207 double tmax = interFace.maximum_time();
208 if (tmax == -1.0) {
209 tmax = region.get_max_time().second;
210 }
211
212 if (tmin > 0.0 || tmax != region.get_max_time().second) {
213 st_min = 0;
214
215 // Count number of active steps...
216 for (int i = 1; i <= num_steps; i++) {
217 double time = region.get_state_time(i);
218 if (time < tmin) {
219 st_min = i;
220 }
221 if (time <= tmax) {
222 st_max = i;
223 }
224 }
225 st_min++;
226 num_steps = st_max - st_min + 1;
227 }
228
229 // ========================================================================
230 // Output time values...
231
232 fmt::print(out_stream, "TIME = zeros(1, {});\n", num_steps);
233 fmt::print(out_stream, "TIME= [\n");
234 for (int i = st_min; i <= st_max; i++) {
235 double time = region.get_state_time(i);
236 fmt::print(out_stream, " {:13.6e}\n", time);
237 }
238 fmt::print(out_stream, "];\n");
239
240 // ========================================================================
241 // Output field values...
242
243 std::vector<double> data(1);
244 for (const auto &field_name : fields) {
245 int comp_count = region.get_field(field_name).raw_storage()->component_count();
246 fmt::print(out_stream, "{} = zeros({}, {});\n", field_name, comp_count, num_steps);
247 fmt::print(out_stream, "{} = [\n", field_name);
248 for (int istep = st_min; istep <= st_max; istep++) {
249 region.begin_state(istep);
250 region.get_field_data(field_name, data);
251 for (int i = 0; i < comp_count; i++) {
252 fmt::print(out_stream, " {:13.6e}", data[i]);
253 }
254 fmt::print(out_stream, ";\n");
255 region.end_state(istep);
256 }
257 fmt::print(out_stream, "];\n");
258 }
259 fmt::print("Wrote data for {} variables at {} timesteps.\n", fields.size(),
260 st_max - st_min + 1);
261 return true;
262 }
263 } // namespace