Debian Codegraph @

packages/seacas/libraries/ioss/src/visualization/utils/Iovs_Utils.C

raw
1 // Copyright(C) 1999-2023 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_CodeTypes.h"
8 #include "Ioss_Utils.h"
9 #include "visualization/utils/Iovs_CatalystLogging.h"
10 #include "visualization/utils/Iovs_CatalystVersion.h"
11 #include "visualization/utils/Iovs_Utils.h"
12 #include <cstring>
13 #include <fstream>
14 #include <iostream>
15 #include <sstream>
16
17 #if defined(__IOSS_WINDOWS__)
18 #ifndef NOMINMAX
19 #define NOMINMAX
20 #endif
21 #include <windows.h>
22 #else
23 #include <libgen.h>
24 #endif
25
26 #ifdef IOSS_DLOPEN_ENABLED
27 #include <dlfcn.h>
28 #endif
29
30 #include <sys/stat.h>
31
32 namespace Iovs {
33 std::string persistentLdLibraryPathEnvForCatalyst = "";
34
35 Utils::Utils()
36 {
37 this->dlHandle = nullptr;
38 this->catalystManager = nullptr;
39 }
40
41 Utils::~Utils()
42 {
43 if (this->catalystManager) {
44 delete this->catalystManager;
45 }
46 #ifdef IOSS_DLOPEN_ENABLED
47 if (this->dlHandle != nullptr) {
48 // when loading both libOSMesa.so and the plugin using dlopen,
49 // dlclose seg faults. Only call dlclose() if we only made one
50 // dlopen call
51 if (this->dlHandleLibOSMesa == nullptr) {
52 dlclose(this->dlHandle);
53 }
54 }
55 #endif
56 }
57
58 CatalystManagerBase &Utils::getCatalystManager()
59 {
60 if (this->catalystManager == nullptr) {
61 this->catalystManager = this->createCatalystManagerInstance();
62 this->checkCatalystInterfaceAndPluginVersions();
63 }
64 return *this->catalystManager;
65 }
66
67 CatalystManagerBase *Utils::createCatalystManagerInstance()
68 {
69 #ifdef IOSS_DLOPEN_ENABLED
70 void *dlh = this->getDlHandle();
71
72 if (!dlh) {
73 return nullptr;
74 }
75
76 typedef CatalystManagerBase *(*CatalystManagerInstanceFuncType)();
77
78 #ifdef __GNUC__
79 __extension__
80 #endif
81 CatalystManagerInstanceFuncType mkr = reinterpret_cast<CatalystManagerInstanceFuncType>(
82 dlsym(dlh, "CreateCatalystManagerInstance"));
83 if (mkr == nullptr) {
84 throw std::runtime_error("dlsym call failed to load function "
85 "'CreateCatalystManagerInstance'");
86 }
87 return (*mkr)();
88 #else
89 return nullptr;
90 #endif
91 }
92
93 void Utils::checkCatalystInterfaceAndPluginVersions()
94 {
95 CatalystVersion cv;
96 std::string iVer = cv.getIOSSCatalystInterfaceVersion();
97 std::string pVer = this->catalystManager->getCatalystPluginVersion();
98 if (!cv.isInterfaceCompatibleWithPlugin(iVer, pVer)) {
99 throw std::runtime_error("IOSS Catalyst interface version: " + iVer +
100 ", is not compatible with IOSS Catalyst plugin version: " + pVer);
101 }
102 }
103
104 std::unique_ptr<Iovs_exodus::CatalystExodusMeshBase>
105 Utils::createCatalystExodusMesh(const DatabaseInfo &dbinfo, const Ioss::PropertyManager &props)
106 {
107
108 CatalystManagerBase::CatalystExodusMeshInit cmInit;
109 cmInit.resultsOutputFilename = dbinfo.databaseFilename;
110 this->initMeshFromIOSSProps(cmInit, dbinfo, props);
111
112 cmInit.underScoreVectors = true;
113 if (props.exists("CATALYST_UNDERSCORE_VECTORS")) {
114 cmInit.underScoreVectors = props.get("CATALYST_UNDERSCORE_VECTORS").get_int();
115 }
116
117 cmInit.applyDisplacements = true;
118 if (props.exists("CATALYST_APPLY_DISPLACEMENTS")) {
119 cmInit.applyDisplacements = props.get("CATALYST_APPLY_DISPLACEMENTS").get_int();
120 }
121
122 cmInit.catalystSeparatorCharacter = dbinfo.separatorCharacter;
123 cmInit.restartTag = this->getRestartTag(dbinfo.databaseFilename);
124
125 return this->getCatalystManager().createCatalystExodusMesh(cmInit);
126 }
127
128 std::unique_ptr<Iovs_cgns::CatalystCGNSMeshBase>
129 Utils::createCatalystCGNSMesh(const DatabaseInfo &dbinfo, const Ioss::PropertyManager &props)
130 {
131
132 CatalystManagerBase::CatalystMeshInit cmInit;
133 cmInit.resultsOutputFilename = dbinfo.databaseFilename;
134 this->initMeshFromIOSSProps(cmInit, dbinfo, props);
135 cmInit.catalystSeparatorCharacter = dbinfo.separatorCharacter;
136 cmInit.restartTag = this->getRestartTag(dbinfo.databaseFilename);
137
138 return this->getCatalystManager().createCatalystCGNSMesh(cmInit);
139 }
140
141 void Utils::initMeshFromIOSSProps(CatalystManagerBase::CatalystMeshInit &cmInit,
142 const DatabaseInfo &dbinfo, const Ioss::PropertyManager &props)
143 {
144
145 if (props.exists("CATALYST_BLOCK_PARSE_JSON_STRING")) {
146 cmInit.catalystBlockJSON = props.get("CATALYST_BLOCK_PARSE_JSON_STRING").get_string();
147 }
148 else if (props.exists("PHACTORI_JSON_SCRIPT")) {
149 bool readOkay = false;
150 std::string phactoriJSONFilePath = props.get("PHACTORI_JSON_SCRIPT").get_string();
151 if (dbinfo.parallelUtils->parallel_rank() == 0) {
152 std::ifstream f(phactoriJSONFilePath);
153 if (f) {
154 std::ostringstream ss;
155 ss << f.rdbuf();
156 cmInit.catalystBlockJSON = ss.str();
157 readOkay = true;
158 }
159 }
160 this->broadCastStatusCode(readOkay, dbinfo);
161 if (!readOkay) {
162 std::ostringstream errmsg;
163 errmsg << "Unable to read input file: " << phactoriJSONFilePath << "\n";
164 IOSS_ERROR(errmsg);
165 }
166 else {
167 this->broadCastString(cmInit.catalystBlockJSON, dbinfo);
168 }
169 }
170 else if (props.exists("PHACTORI_INPUT_SYNTAX_SCRIPT")) {
171 std::string phactoriFilePath = props.get("PHACTORI_INPUT_SYNTAX_SCRIPT").get_string();
172 CatalystManagerBase::ParseResult pres;
173 if (dbinfo.parallelUtils->parallel_rank() == 0) {
174 this->getCatalystManager().parsePhactoriFile(phactoriFilePath, pres);
175 }
176 this->broadCastStatusCode(pres.parseFailed, dbinfo);
177 if (pres.parseFailed) {
178 std::ostringstream errmsg;
179 errmsg << "Unable to parse input file: " << phactoriFilePath << "\n";
180 IOSS_ERROR(errmsg);
181 }
182 else {
183 this->broadCastString(pres.jsonParseResult, dbinfo);
184 cmInit.catalystBlockJSON = pres.jsonParseResult;
185 }
186 }
187
188 cmInit.writeCatalystMeshOneFile = false;
189 if (props.exists("WRITE_CATALYST_MESH_ONE_FILE_WITH_PREFIX")) {
190 cmInit.writeCatalystMeshOneFile = true;
191 cmInit.catalystMeshOneFilePrefix =
192 props.get("WRITE_CATALYST_MESH_ONE_FILE_WITH_PREFIX").get_string();
193 }
194
195 cmInit.writeCatalystMeshFilePerProc = false;
196 if (props.exists("WRITE_CATALYST_MESH_FILE_PER_PROC_WITH_PREFIX")) {
197 cmInit.writeCatalystMeshFilePerProc = true;
198 cmInit.catalystMeshFilePerProcPrefix =
199 props.get("WRITE_CATALYST_MESH_FILE_PER_PROC_WITH_PREFIX").get_string();
200 }
201
202 if (props.exists("CATALYST_SCRIPT")) {
203 cmInit.catalystPythonFilename = props.get("CATALYST_SCRIPT").get_string();
204 }
205 else {
206 cmInit.catalystPythonFilename = this->getCatalystPythonDriverPath();
207 }
208
209 if (props.exists("CATALYST_SCRIPT_EXTRA_FILE")) {
210 cmInit.catalystData.push_back(props.get("CATALYST_SCRIPT_EXTRA_FILE").get_string());
211 }
212
213 if (props.exists("CATALYST_BLOCK_PARSE_INPUT_DECK_NAME")) {
214 cmInit.catalystInputDeckName = props.get("CATALYST_BLOCK_PARSE_INPUT_DECK_NAME").get_string();
215 }
216
217 cmInit.enableLogging = false;
218 if (props.exists("CATALYST_ENABLE_LOGGING")) {
219 cmInit.enableLogging = props.get("CATALYST_ENABLE_LOGGING").get_int();
220 }
221
222 cmInit.debugLevel = 0;
223 if (props.exists("CATALYST_DEBUG_LEVEL")) {
224 cmInit.enableLogging = props.get("CATALYST_DEBUG_LEVEL").get_int();
225 }
226
227 cmInit.catalystOutputDirectory = CATALYST_OUTPUT_DIRECTORY;
228 if (props.exists("CATALYST_OUTPUT_DIRECTORY")) {
229 cmInit.catalystOutputDirectory = props.get("CATALYST_OUTPUT_DIRECTORY").get_string();
230 }
231
232 cmInit.catalystInputName = "input";
233 if (props.exists("CATALYST_INPUT_NAME")) {
234 cmInit.catalystInputName = props.get("CATALYST_INPUT_NAME").get_string();
235 }
236
237 cmInit.enableCatalystMultiInputPipeline = false;
238 if (props.exists("CATALYST_MULTI_INPUT_PIPELINE_NAME")) {
239 cmInit.enableCatalystMultiInputPipeline = true;
240 cmInit.catalystMultiInputPipelineName =
241 props.get("CATALYST_MULTI_INPUT_PIPELINE_NAME").get_string();
242 }
243 }
244
245 void Utils::writeToCatalystLogFile(const DatabaseInfo &dbinfo, const Ioss::PropertyManager &props)
246 {
247 if (dbinfo.parallelUtils->parallel_rank() == 0) {
248 CatalystLogging catLog = CatalystLogging();
249 catLog.setProperties(&props);
250 catLog.writeToLogFile();
251 }
252 dbinfo.parallelUtils->barrier();
253 }
254
255 std::string Utils::getRestartTag(const std::string &databaseFilename)
256 {
257 std::string restartTag;
258 std::string::size_type pos = databaseFilename.rfind(".e-s");
259 if (pos != std::string::npos) {
260 if (pos + 3 <= databaseFilename.length()) {
261 restartTag = databaseFilename.substr(pos + 3, 5);
262 }
263 }
264 return restartTag;
265 }
266
267 bool Utils::fileExists(const std::string &filepath)
268 {
269 struct stat buffer
270 {
271 };
272 return (stat(filepath.c_str(), &buffer) == 0);
273 }
274
275 std::string Utils::getDatabaseOutputFilePath(const std::string &databaseFilename,
276 const Ioss::PropertyManager &properties)
277 {
278
279 int numberOfCatalystBlocks = this->getCatalystManager().getCatalystOutputIDNumber();
280 if (!properties.exists("CATALYST_OUTPUT_DIRECTORY")) {
281 std::ostringstream s;
282 s << databaseFilename << "." << numberOfCatalystBlocks << CATALYST_FILE_SUFFIX;
283 return std::string(CATALYST_OUTPUT_DIRECTORY) + "/" + s.str();
284 }
285 else {
286 return databaseFilename;
287 }
288 }
289
290 void *Utils::getDlHandle()
291 {
292 if (this->dlHandle == nullptr) {
293 loadPluginLibrary();
294 }
295 return this->dlHandle;
296 }
297
298 void Utils::loadPluginLibrary()
299 {
300
301 std::string pluginLibraryPath;
302 bool callDlopenLibOSMesa{};
303 std::string libOSMesaPath;
304
305 callDlopenLibOSMesa = this->getCatalystPluginPath(pluginLibraryPath, libOSMesaPath);
306
307 #ifdef IOSS_DLOPEN_ENABLED
308 if (callDlopenLibOSMesa) {
309 this->dlHandleLibOSMesa = dlopen(libOSMesaPath.c_str(), RTLD_NOW | RTLD_GLOBAL);
310 if (this->dlHandleLibOSMesa == nullptr) {
311 throw std::runtime_error(dlerror());
312 }
313 }
314 else {
315 this->dlHandleLibOSMesa = nullptr;
316 }
317 this->dlHandle = dlopen(pluginLibraryPath.c_str(), RTLD_NOW | RTLD_GLOBAL);
318 if (this->dlHandle == nullptr) {
319 throw std::runtime_error(dlerror());
320 }
321 #else
322 this->dlHandle = nullptr;
323 this->dlHandleLibOSMesa = nullptr;
324 #endif
325 }
326
327 bool Utils::getCatalystPluginPath(std::string &catalystPluginPath, std::string &libOSMesaPath)
328 {
329 bool callDlopenLibOSMesa = false;
330
331 if (getenv("CATALYST_PLUGIN") != nullptr) {
332 catalystPluginPath = getenv("CATALYST_PLUGIN");
333 callDlopenLibOSMesa = false;
334 libOSMesaPath = CATALYST_LIB_OSMESA;
335 return callDlopenLibOSMesa;
336 }
337
338 std::string catalystInsDir = this->getCatalystAdapterInstallDirectory();
339
340 if (!catalystInsDir.empty()) {
341 catalystPluginPath =
342 catalystInsDir + std::string(CATALYST_INSTALL_LIB_DIR) + CATALYST_PLUGIN_DYNAMIC_LIBRARY;
343 callDlopenLibOSMesa = false;
344 libOSMesaPath = CATALYST_LIB_OSMESA;
345 return callDlopenLibOSMesa;
346 }
347
348 catalystPluginPath =
349 this->getSierraInstallDirectory() + "/" + std::string(CATALYST_PLUGIN_DYNAMIC_LIBRARY);
350 callDlopenLibOSMesa = true;
351 libOSMesaPath =
352 this->getSierraInstallDirectory() + CATALYST_LIB_OSMESA_DIR + CATALYST_LIB_OSMESA;
353
354 // to avoid depending on name python-3.7, python-3.8, etc., we
355 // expect a link to the python directory inside the sierra catalyst
356 // install along CATALYST_PARAVIEW_PYTHON_ZIP_FILE
357 std::string paraviewPythonZipFile =
358 this->getSierraInstallDirectory() + CATALYST_PARAVIEW_PYTHON_ZIP_FILE;
359 setPythonPathForParaViewPythonZipFile(paraviewPythonZipFile);
360 return callDlopenLibOSMesa;
361 }
362
363 void Utils::setPythonPathForParaViewPythonZipFile(std::string &paraviewPythonZipFilePath)
364 {
365 const char *existingPythonpath = getenv("PYTHONPATH");
366 if (existingPythonpath == nullptr) {
367 persistentLdLibraryPathEnvForCatalyst = paraviewPythonZipFilePath;
368 }
369 else {
370 persistentLdLibraryPathEnvForCatalyst = paraviewPythonZipFilePath + ":" + existingPythonpath;
371 }
372 #if defined(__IOSS_WINDOWS__)
373 SetEnvironmentVariableA("PYTHONPATH", persistentLdLibraryPathEnvForCatalyst.c_str());
374 #else
375 setenv("PYTHONPATH", persistentLdLibraryPathEnvForCatalyst.c_str(), 1);
376 #endif
377 }
378
379 std::string Utils::getCatalystPythonDriverPath()
380 {
381
382 std::string catalystInsDir = this->getCatalystAdapterInstallDirectory();
383
384 if (!catalystInsDir.empty()) {
385 return catalystInsDir + std::string(CATALYST_INSTALL_PHACTORI_DIR) +
386 CATALYST_PLUGIN_PYTHON_MODULE;
387 }
388
389 return this->getSierraInstallDirectory() + "/" + std::string(CATALYST_PLUGIN_PYTHON_MODULE);
390 }
391
392 std::string Utils::getSierraInstallDirectory()
393 {
394
395 std::string sierraInsDir;
396 if (getenv("SIERRA_INSTALL_DIR") != nullptr) {
397 sierraInsDir = getenv("SIERRA_INSTALL_DIR");
398 }
399 else {
400 std::ostringstream errmsg;
401 errmsg << "Environment variable SIERRA_INSTALL_DIR not set.\n"
402 << " Unable to find ParaView catalyst dynamic library.\n";
403 IOSS_ERROR(errmsg);
404 }
405
406 std::string sierraSystem;
407 if (getenv("SIERRA_SYSTEM") != nullptr) {
408 sierraSystem = getenv("SIERRA_SYSTEM");
409 }
410 else {
411 std::ostringstream errmsg;
412 errmsg << "Environment variable SIERRA_SYSTEM not set.\n"
413 << " Unable to find ParaView catalyst dynamic library.\n";
414 IOSS_ERROR(errmsg);
415 }
416
417 #if defined(__IOSS_WINDOWS__)
418 char *cbuf = _fullpath(nullptr, sierraInsDir.c_str(), _MAX_PATH);
419 #else
420 char *cbuf = realpath(sierraInsDir.c_str(), nullptr);
421 #endif
422 std::string sierraInsPath = cbuf;
423 free(cbuf);
424
425 if (!fileExists(sierraInsPath)) {
426 std::ostringstream errmsg;
427 errmsg << "SIERRA_INSTALL_DIR directory does not exist.\n"
428 << "Directory path: " << sierraInsPath << "\n"
429 << " Unable to find ParaView catalyst dynamic library.\n";
430 IOSS_ERROR(errmsg);
431 }
432
433 #if defined(__IOSS_WINDOWS__)
434 {
435 std::ostringstream errmsg;
436 errmsg << "This code is not yet supported on windows...\n";
437 IOSS_ERROR(errmsg);
438 }
439 #else
440 char *cbase = strdup(sierraInsPath.c_str());
441 char *cdir = strdup(sierraInsPath.c_str());
442 char *bname = basename(cbase);
443 char *dname = dirname(cdir);
444
445 while (strcmp(dname, "/") != 0 && strcmp(dname, ".") != 0 && strcmp(bname, "sierra") != 0) {
446 bname = basename(dname);
447 dname = dirname(dname);
448 }
449
450 if (strcmp(bname, "sierra") == 0) {
451 sierraInsPath = dname;
452 }
453
454 free(cbase);
455 free(cdir);
456 #endif
457
458 return sierraInsPath + "/" + CATALYST_PLUGIN_PATH + "/" + sierraSystem +
459 CATALYST_IOSS_CATALYST_PLUGIN_DIR;
460 }
461
462 std::string Utils::getCatalystAdapterInstallDirectory()
463 {
464 std::string catalystInsDir = "";
465 if (getenv("CATALYST_ADAPTER_INSTALL_DIR") != nullptr) {
466 catalystInsDir = getenv("CATALYST_ADAPTER_INSTALL_DIR");
467
468 if (!fileExists(catalystInsDir)) {
469 std::ostringstream errmsg;
470 errmsg << "CATALYST_ADAPTER_INSTALL_DIR directory does\n"
471 << "not exist. Directory path: " << catalystInsDir << "\n"
472 << "Unable to find ParaView catalyst dynamic library.\n";
473 IOSS_ERROR(errmsg);
474 }
475 }
476 return catalystInsDir;
477 }
478
479 void Utils::checkDbUsage(Ioss::DatabaseUsage db_usage)
480 {
481 std::ostringstream errmsg;
482 if (db_usage == Ioss::WRITE_HEARTBEAT) {
483 errmsg << "ParaView catalyst database type cannot be"
484 << " used in a HEARTBEAT block.\n";
485 IOSS_ERROR(errmsg);
486 }
487 else if (db_usage == Ioss::WRITE_HISTORY) {
488 errmsg << "ParaView catalyst database type cannot be"
489 << " used in a HISTORY block.\n";
490 IOSS_ERROR(errmsg);
491 }
492 else if (db_usage == Ioss::READ_MODEL || db_usage == Ioss::READ_RESTART) {
493
494 errmsg << "ParaView catalyst database type cannot be"
495 << " used to read a model.\n";
496 IOSS_ERROR(errmsg);
497 }
498 }
499
500 void Utils::createDatabaseOutputFile(const DatabaseInfo &dbinfo)
501 {
502 std::ostringstream errmsg;
503 if (dbinfo.parallelUtils->parallel_rank() == 0) {
504 if (!Utils::fileExists(dbinfo.databaseFilename)) {
505 std::ofstream output_file;
506 output_file.open(dbinfo.databaseFilename.c_str(), std::ios::out | std::ios::trunc);
507
508 if (!output_file) {
509 errmsg << "Unable to create output file: " << dbinfo.databaseFilename << ".\n";
510 IOSS_ERROR(errmsg);
511 }
512 output_file.close();
513 }
514 }
515 dbinfo.parallelUtils->barrier();
516 }
517
518 void Utils::reportCatalystErrorMessages(const std::vector<int> &error_codes,
519 const std::vector<std::string> &error_messages,
520 int myRank)
521 {
522
523 if (!error_codes.empty() && !error_messages.empty() &&
524 error_codes.size() == error_messages.size()) {
525 for (unsigned int i = 0; i < error_codes.size(); i++) {
526 if (error_codes[i] > 0) {
527 Ioss::WarnOut() << "\n\n** ParaView Catalyst Plugin Warning Message Severity Level "
528 << error_codes[i] << ", On Processor " << myRank << " **\n\n";
529 Ioss::WarnOut() << error_messages[i];
530 }
531 else {
532 std::ostringstream errmsg;
533 errmsg << "\n\n** ParaView Catalyst Plugin Error Message Severity Level "
534 << error_codes[i] << ", On Processor " << myRank << " **\n\n"
535 << error_messages[i];
536 IOSS_ERROR(errmsg);
537 }
538 }
539 }
540 }
541
542 void Utils::broadCastString(IOSS_MAYBE_UNUSED std::string &s,
543 IOSS_MAYBE_UNUSED const DatabaseInfo &dbinfo)
544 {
545 IOSS_PAR_UNUSED(s);
546 IOSS_PAR_UNUSED(dbinfo);
547 #ifdef SEACAS_HAVE_MPI
548 int size = s.size();
549 dbinfo.parallelUtils->broadcast(size);
550 if (dbinfo.parallelUtils->parallel_rank() != 0) {
551 s.resize(size);
552 }
553 dbinfo.parallelUtils->broadcast(s);
554 #endif
555 }
556
557 void Utils::broadCastStatusCode(IOSS_MAYBE_UNUSED bool &statusCode,
558 IOSS_MAYBE_UNUSED const DatabaseInfo &dbinfo)
559 {
560 IOSS_PAR_UNUSED(statusCode);
561 IOSS_PAR_UNUSED(dbinfo);
562 #ifdef SEACAS_HAVE_MPI
563
564 int code = statusCode;
565 dbinfo.parallelUtils->broadcast(code);
566 statusCode = code;
567 #endif
568 }
569 } // namespace Iovs