From 3d1b4796fdb7ef342052197dc49f92a304bf664c Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 08:15:32 +0100 Subject: [PATCH 01/13] DamarisVar: remove empty string assignment strings are properly empty initialized --- opm/simulators/utils/DamarisVar.hpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index 840c7de0a..f941d89ae 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -94,17 +94,11 @@ namespace DamarisOutput DamarisVarXMLAttributes() { // Additional data needed to complete an XML element - layout_ = ""; - mesh_ = ""; type_ = "scalar"; // This is probably not needed as vector data is defined using // the Layout paramter. Could be useful for cross checking visualizable_ = "false"; - unit_ = ""; time_varying_ = "true"; centering_ = "zonal"; - store_ = ""; - script_ = ""; - select_mem_ = ""; } /** From 3849febe99e798560814261ae77a98ab554c653b Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 08:20:04 +0100 Subject: [PATCH 02/13] DamarisVar: pass parameters by reference --- opm/simulators/utils/DamarisVar.hpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index f941d89ae..26d364a04 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -248,7 +248,10 @@ namespace DamarisOutput * /param [IN] variable_name The name of the Damaris variable (defined in the Damaris XML file) * /param [IN] rank The rank of the process. Used for error output. */ - DamarisVar(int dims, std::vector param_names, std::string variable_name, int rank) + DamarisVar(int dims, + const std::vector& param_names, + const std::string& variable_name, + int rank) : param_names_(param_names) , variable_name_(variable_name) , rank_(rank) @@ -308,9 +311,9 @@ namespace DamarisOutput * /param [IN] rank The rank of the process. Used for error output. */ DamarisVar(int dims, - std::vector param_names, - std::vector param_values, - std::string variable_name, + const std::vector& param_names, + const std::vector& param_values, + const std::string& variable_name, int rank) : param_names_(param_names) , variable_name_(variable_name) @@ -334,7 +337,7 @@ namespace DamarisOutput * Method to check that the template paramater T is the same as the requested * type for the variable in the XML file */ - bool TestType(std::string variable_name) + bool TestType(const std::string& variable_name) { bool resbool = true; // This gets the type of the Damaris XML 's @@ -673,7 +676,9 @@ namespace DamarisOutput } private: - void formatTypeError(std::string& var_name, std::string type_name1, std::string type_name2) + void formatTypeError(const std::string& var_name, + const std::string& type_name1, + const std::string& type_name2) { dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar () variable_name_: \"" << var_name << "\" The template type of Type of DamarisVar in the code: " << type_name1 From 3024379bedca64850bc939e3868ed67f61a6538b Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 08:26:20 +0100 Subject: [PATCH 03/13] DamarisVar: remove void in parameter lists not consistent with the code style --- opm/simulators/utils/DamarisVar.hpp | 34 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index 26d364a04..c11481860 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -104,7 +104,7 @@ namespace DamarisOutput /** * Creates the XML representation of the variable from the available strings */ - std::string ReturnXMLForVariable(void) + std::string ReturnXMLForVariable() { std::ostringstream var_sstr; @@ -139,16 +139,16 @@ namespace DamarisOutput class DamarisVarBase { public: - virtual ~DamarisVarBase(void) {}; - virtual void printError(void) = 0; - virtual bool hasError(void) = 0; + virtual ~DamarisVarBase() {}; + virtual void printError() = 0; + virtual bool hasError() = 0; virtual void setDamarisParameterAndShmem(const std::vector& paramSizeVal) = 0; virtual void setDamarisParameter(const std::vector& paramSizeVal) = 0; virtual void setDamarisPosition(const std::vector& positionsVals) = 0; - virtual void setPointersToDamarisShmem(void) = 0; - virtual void commitVariableDamarisShmem(void) = 0; - virtual void clearVariableDamarisShmem(void) = 0; - virtual std::string& variable_name(void) = 0; + virtual void setPointersToDamarisShmem() = 0; + virtual void commitVariableDamarisShmem() = 0; + virtual void clearVariableDamarisShmem() = 0; + virtual std::string& variable_name() = 0; }; // class DamarisVarBase /** @@ -323,7 +323,7 @@ namespace DamarisOutput setDamarisParameterAndShmem(param_values); // Initialise the memory size in the constructor. } - ~DamarisVar(void) + ~DamarisVar() { if (data_ptr_ != nullptr) { commitVariableDamarisShmem(); @@ -449,12 +449,12 @@ namespace DamarisOutput parameters_set_ = true; } - void printError(void) + void printError() { OPM_THROW(std::runtime_error, dam_err_sstr_.str()); } - bool hasError(void) + bool hasError() { return (has_error_); } @@ -463,7 +463,7 @@ namespace DamarisOutput * Returns the data pointer to shared memory, or nullptr if it has not been * allocated */ - T* data(void) + T* data() { if (parameters_set_ == true) { return (data_ptr_); // This still could be nullptr @@ -472,7 +472,7 @@ namespace DamarisOutput } } - std::string& variable_name(void) + std::string& variable_name() { return (variable_name_); } @@ -480,7 +480,7 @@ namespace DamarisOutput /** * Creates the XML representation of the variable from the available strings */ - std::string returnXMLForVariable(void) + std::string returnXMLForVariable() { std::ostringstream var_sstr; @@ -609,7 +609,7 @@ namespace DamarisOutput * string \ref variable_name_ /implicit : Implicitly uses the * class data element : \ref data_ptr_ */ - void setPointersToDamarisShmem(void) + void setPointersToDamarisShmem() { if (parameters_set_ == true) { // Allocate memory in the shared memory section... @@ -642,7 +642,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name string \ref * variable_name_ */ - void commitVariableDamarisShmem(void) + void commitVariableDamarisShmem() { // Signal to Damaris we are done writing data for this iteration dam_err_ = damaris_commit(variable_name_.c_str()); @@ -662,7 +662,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name string \ref * variable_name_ */ - void clearVariableDamarisShmem(void) + void clearVariableDamarisShmem() { // Signal to Damaris it has complete charge of the memory area dam_err_ = damaris_clear(variable_name_.c_str()); From 8ad5e3a9747c42048d9c6e26a73ca1baab8cbe69 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 08:32:23 +0100 Subject: [PATCH 04/13] DamarisVar: mark virtual method overrides as such --- opm/simulators/utils/DamarisVar.hpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index c11481860..f13738c76 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -139,7 +139,7 @@ namespace DamarisOutput class DamarisVarBase { public: - virtual ~DamarisVarBase() {}; + virtual ~DamarisVarBase() = default; virtual void printError() = 0; virtual bool hasError() = 0; virtual void setDamarisParameterAndShmem(const std::vector& paramSizeVal) = 0; @@ -449,12 +449,12 @@ namespace DamarisOutput parameters_set_ = true; } - void printError() + void printError() override { OPM_THROW(std::runtime_error, dam_err_sstr_.str()); } - bool hasError() + bool hasError() override { return (has_error_); } @@ -472,7 +472,7 @@ namespace DamarisOutput } } - std::string& variable_name() + std::string& variable_name() override { return (variable_name_); } @@ -501,7 +501,7 @@ namespace DamarisOutput * * */ - void setDamarisParameterAndShmem(const std::vector& paramSizeVal) + void setDamarisParameterAndShmem(const std::vector& paramSizeVal) override { this->setDamarisParameter(paramSizeVal); this->setPointersToDamarisShmem(); @@ -531,7 +531,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the array of paramater names: * \ref param_names_ */ - void setDamarisParameter(const std::vector& paramSizeVal) + void setDamarisParameter(const std::vector& paramSizeVal) override { assert(paramSizeVal.size() == static_cast(num_params_)); @@ -581,7 +581,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name: \ref * variable_name_ */ - void setDamarisPosition(const std::vector& positionsVals) + void setDamarisPosition(const std::vector& positionsVals) override { assert(positionsVals.size() == static_cast(num_params_)); @@ -609,7 +609,7 @@ namespace DamarisOutput * string \ref variable_name_ /implicit : Implicitly uses the * class data element : \ref data_ptr_ */ - void setPointersToDamarisShmem() + void setPointersToDamarisShmem() override { if (parameters_set_ == true) { // Allocate memory in the shared memory section... @@ -642,7 +642,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name string \ref * variable_name_ */ - void commitVariableDamarisShmem() + void commitVariableDamarisShmem() override { // Signal to Damaris we are done writing data for this iteration dam_err_ = damaris_commit(variable_name_.c_str()); @@ -662,7 +662,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name string \ref * variable_name_ */ - void clearVariableDamarisShmem() + void clearVariableDamarisShmem() override { // Signal to Damaris it has complete charge of the memory area dam_err_ = damaris_clear(variable_name_.c_str()); From aa2af1101658e534da65fce3b6cd33276ed2f0db Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 08:57:26 +0100 Subject: [PATCH 05/13] DamarisVar: make accessors const --- opm/simulators/utils/DamarisVar.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index f13738c76..e6c64c552 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -140,15 +140,15 @@ namespace DamarisOutput { public: virtual ~DamarisVarBase() = default; - virtual void printError() = 0; - virtual bool hasError() = 0; + virtual void printError() const = 0; + virtual bool hasError() const = 0; virtual void setDamarisParameterAndShmem(const std::vector& paramSizeVal) = 0; virtual void setDamarisParameter(const std::vector& paramSizeVal) = 0; virtual void setDamarisPosition(const std::vector& positionsVals) = 0; virtual void setPointersToDamarisShmem() = 0; virtual void commitVariableDamarisShmem() = 0; virtual void clearVariableDamarisShmem() = 0; - virtual std::string& variable_name() = 0; + virtual const std::string& variable_name() const = 0; }; // class DamarisVarBase /** @@ -449,12 +449,12 @@ namespace DamarisOutput parameters_set_ = true; } - void printError() override + void printError() const override { OPM_THROW(std::runtime_error, dam_err_sstr_.str()); } - bool hasError() override + bool hasError() const override { return (has_error_); } @@ -472,7 +472,7 @@ namespace DamarisOutput } } - std::string& variable_name() override + const std::string& variable_name() const override { return (variable_name_); } From 125431f32655bcb21443489de968cd271ccaace9 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 08:36:47 +0100 Subject: [PATCH 06/13] DamarisVar: remove paranthesis is return statements not consistent with code style --- opm/simulators/utils/DamarisVar.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index e6c64c552..7eb236bfd 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -132,7 +132,7 @@ namespace DamarisOutput if (this->select_subset_ != "") var_sstr << " select-subset=\"" << this->select_subset_ << "\""; - return (var_sstr.str()); + return var_sstr.str(); } }; @@ -153,7 +153,7 @@ namespace DamarisOutput /** * class to store a Damaris variable representation for the XML file - * (can be used with /ref class DamarisKeywords). + * (can be used with \ref class DamarisKeywords). * * It is thought that the details stored in the object can be used to pass * into an XML generation function e.g. DamarisKeywords @@ -347,7 +347,7 @@ namespace DamarisOutput dam_err_sstr_ << " ERROR rankDamarisVar::DamarisVar () damaris_get_type(\"" << variable_name_ << "\", vartype); Damaris error = " << damaris_error_string(dam_err_) << std::endl; has_error_ = true; - return (false); + return false; } T test_id; const std::type_info& t1 = typeid(test_id); @@ -456,7 +456,7 @@ namespace DamarisOutput bool hasError() const override { - return (has_error_); + return has_error_; } /** @@ -466,15 +466,15 @@ namespace DamarisOutput T* data() { if (parameters_set_ == true) { - return (data_ptr_); // This still could be nullptr + return data_ptr_; // This still could be nullptr } else { - return (nullptr); + return nullptr; } } const std::string& variable_name() const override { - return (variable_name_); + return variable_name_; } /** From 4f76bd09722d332d8b28e13b66a47fb5b11608b2 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 08:42:02 +0100 Subject: [PATCH 07/13] DamarisVar: make TestType private --- opm/simulators/utils/DamarisVar.hpp | 208 ++++++++++++++-------------- 1 file changed, 104 insertions(+), 104 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index 7eb236bfd..d6c493bd2 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -333,110 +333,6 @@ namespace DamarisOutput printError(); // flush out any error messages } - /** - * Method to check that the template paramater T is the same as the requested - * type for the variable in the XML file - */ - bool TestType(const std::string& variable_name) - { - bool resbool = true; - // This gets the type of the Damaris XML 's - DAMARIS_TYPE_STR vartype; - dam_err_ = damaris_get_type(variable_name.c_str(), &vartype); - if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rankDamarisVar::DamarisVar () damaris_get_type(\"" << variable_name_ - << "\", vartype); Damaris error = " << damaris_error_string(dam_err_) << std::endl; - has_error_ = true; - return false; - } - T test_id; - const std::type_info& t1 = typeid(test_id); - - if (vartype == DAMARIS_TYPE_DOUBLE) { - double td = 0.0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_FLOAT) { - float td = 0.0f; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_CHAR) { - char td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_UCHAR) { - unsigned char td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_SHORT) { - short td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_USHORT) { - unsigned short td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_INT) { - int td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_UINT) { - unsigned int td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_LONG) { - long td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_ULONG) { - unsigned long td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_UNDEFINED) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name - << "\" has type DAMARIS_TYPE_UNDEFINED" << std::endl; - has_error_ = true; - resbool = false; - } else { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name - << "\" is not of available type " << std::endl; - has_error_ = true; - resbool = false; - } - - return resbool; - } - /** * Allow a user to indicate that the Damaris variable has allocated a size - * This method is usefull as a single parameter can control one or more @@ -676,6 +572,110 @@ namespace DamarisOutput } private: + /** + * Method to check that the template parameter T is the same as the requested + * type for the variable in the XML file + */ + bool TestType(const std::string& variable_name) + { + bool resbool = true; + // This gets the type of the Damaris XML 's + DAMARIS_TYPE_STR vartype; + dam_err_ = damaris_get_type(variable_name.c_str(), &vartype); + if (dam_err_ != DAMARIS_OK) { + dam_err_sstr_ << " ERROR rankDamarisVar::DamarisVar () damaris_get_type(\"" << variable_name_ + << "\", vartype); Damaris error = " << damaris_error_string(dam_err_) << std::endl; + has_error_ = true; + return false; + } + T test_id; + const std::type_info& t1 = typeid(test_id); + + if (vartype == DAMARIS_TYPE_DOUBLE) { + double td = 0.0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_FLOAT) { + float td = 0.0f; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_CHAR) { + char td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_UCHAR) { + unsigned char td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_SHORT) { + short td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_USHORT) { + unsigned short td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_INT) { + int td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_UINT) { + unsigned int td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_LONG) { + long td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_ULONG) { + unsigned long td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_UNDEFINED) { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name + << "\" has type DAMARIS_TYPE_UNDEFINED" << std::endl; + has_error_ = true; + resbool = false; + } else { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name + << "\" is not of available type " << std::endl; + has_error_ = true; + resbool = false; + } + + return resbool; + } + void formatTypeError(const std::string& var_name, const std::string& type_name1, const std::string& type_name2) From 3f842cd67f3e77b5e263804e6dd2a957c07eac28 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 08:53:01 +0100 Subject: [PATCH 08/13] DamarisVar: add translation unit --- CMakeLists_files.cmake | 1 + opm/simulators/utils/DamarisVar.cpp | 381 ++++++++++++++++++++++++++++ opm/simulators/utils/DamarisVar.hpp | 335 +----------------------- 3 files changed, 396 insertions(+), 321 deletions(-) create mode 100644 opm/simulators/utils/DamarisVar.cpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index 7c1f73298..4f39d3a59 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -623,6 +623,7 @@ if (Damaris_FOUND AND MPI_FOUND) list (APPEND PUBLIC_HEADER_FILES ebos/damariswriter.hh) list (APPEND PUBLIC_HEADER_FILES opm/simulators/utils/DamarisVar.hpp) list (APPEND PUBLIC_HEADER_FILES opm/simulators/utils/GridDataOutput.hpp) + list(APPEND MAIN_SOURCE_FILES opm/simulators/utils/DamarisVar.cpp) endif() if(HDF5_FOUND) diff --git a/opm/simulators/utils/DamarisVar.cpp b/opm/simulators/utils/DamarisVar.cpp new file mode 100644 index 000000000..b74fac117 --- /dev/null +++ b/opm/simulators/utils/DamarisVar.cpp @@ -0,0 +1,381 @@ +/* + Copyright 2023 Inria, Bretagne–Atlantique Research Center + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ +#include +#include + +#include + +#include + +#include +#include +#include + +namespace Opm::DamarisOutput { + +DamarisVarXMLAttributes::DamarisVarXMLAttributes() +{ + // Additional data needed to complete an XML element + type_ = "scalar"; // This is probably not needed as vector data is defined using + // the Layout paramter. Could be useful for cross checking + visualizable_ = "false"; + time_varying_ = "true"; + centering_ = "zonal"; +} + +std::string DamarisVarXMLAttributes::ReturnXMLForVariable() +{ + std::ostringstream var_sstr; + + var_sstr << " layout=\"" << this->layout_ << "\""; + if (this->mesh_ != "") + var_sstr << " mesh=\"" << this->mesh_ << "\""; + if (this->type_ != "") + var_sstr << " type=\"" << this->type_ << "\""; + if (this->visualizable_ != "") + var_sstr << " visualizable=\"" << this->visualizable_ << "\""; + if (this->unit_ != "") + var_sstr << " unit=\"" << this->unit_ << "\""; + if (this->time_varying_ != "") + var_sstr << " time_varying=\"" << this->time_varying_ << "\""; + if (this->centering_ != "") + var_sstr << " centering=\"" << this->centering_ << "\""; + if (this->store_ != "") + var_sstr << " store=\"" << this->store_ << "\""; + if (this->script_ != "") + var_sstr << " script=\"" << this->script_ << "\""; + if (this->select_mem_ != "") + var_sstr << " select-mem=\"" << this->select_mem_ << "\""; + if (this->select_file_ != "") + var_sstr << " select-file=\"" << this->select_file_ << "\""; + if (this->select_subset_ != "") + var_sstr << " select-subset=\"" << this->select_subset_ << "\""; + + return var_sstr.str(); +} + +template +DamarisVar::DamarisVar(int dims, + const std::vector& param_names, + const std::string& variable_name, int rank) + : param_names_(param_names) + , variable_name_(variable_name) + , rank_(rank) +{ + dam_err_ = DAMARIS_OK; + + assert(param_names_.size() == static_cast(dims)); + assert(dims > 0); + + has_error_ = false; + + // Check that our template type T matches out Damaris XML type + TestType(variable_name); + if (hasError()) { + printError(); // throws a runtime error, with error message from + // dam_err_sstr_ + } + + current_size_ = 0; + num_params_ = param_names_.size(); + param_sizes_.resize(num_params_); + positions_.resize(dims); + + data_ptr_ = nullptr; + parameters_set_ = false; + has_error_ = false; +} + +template +DamarisVar::DamarisVar(int dims, + const std::vector& param_names, + const std::vector& param_values, + const std::string& variable_name, + int rank) + : param_names_(param_names) + , variable_name_(variable_name) + , rank_(rank) +{ + DamarisVar(dims, param_names, variable_name, rank); + setDamarisParameterAndShmem(param_values); // Initialise the memory size in the constructor. +} + +template +DamarisVar::~DamarisVar() +{ + if (data_ptr_ != nullptr) { + commitVariableDamarisShmem(); + clearVariableDamarisShmem(); + } + if (this->hasError()) + printError(); // flush out any error messages +} + +template +void DamarisVar::printError() const +{ + OPM_THROW(std::runtime_error, dam_err_sstr_.str()); +} + +template +std::string DamarisVar::returnXMLForVariable() +{ + std::ostringstream var_sstr; + + var_sstr << " "; + + return var_sstr.str(); +} + +template +void DamarisVar::setDamarisParameter(const std::vector& paramSizeVal) +{ + assert(paramSizeVal.size() == static_cast(num_params_)); + + bool resbool = true; + std::size_t total_size = 1; + for (int varnum = 0; varnum < num_params_; varnum++) { + param_sizes_[varnum] = paramSizeVal[varnum]; + total_size *= param_sizes_[varnum]; + + dam_err_ = damaris_parameter_set(param_names_[varnum].c_str(), ¶mSizeVal[varnum], sizeof(int)); + if (dam_err_ != DAMARIS_OK) { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_parameter_set(\"" + << param_names_[varnum] << "\", paramSizeVal, sizeof(int)); Damaris error = " + << damaris_error_string(dam_err_) << std::endl; + resbool = false; + has_error_ = true; + } + } + + if (resbool == true) { + parameterIsSet(); // sets parameters_set_ and gets the size of the + // variables block storage (as number of elemnts) + } + + if (total_size > 0) { + current_size_ = total_size; + } else { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar::getDataStoreBlockSize() " + << "The total size of the variable is 0 - please check " + "input paramSizeVal array." + << std::endl; + has_error_ = true; + } + + if (hasError()) { + printError(); + } +} + +template +void DamarisVar::setDamarisPosition(const std::vector& positionsVals) +{ + assert(positionsVals.size() == static_cast(num_params_)); + + for (int pos_dim = 0; pos_dim < num_params_; pos_dim++) { + positions_[pos_dim] = positionsVals[pos_dim]; + } + dam_err_ = damaris_set_position(variable_name_.c_str(), positionsVals.data()); + if (dam_err_ != DAMARIS_OK) { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_set_position(\"" + << variable_name_ + << "\", positionsVals); Damaris error = " << damaris_error_string(dam_err_) << std::endl; + has_error_ = true; + } + + if (hasError()) { + printError(); + } +} + +template +void DamarisVar::setPointersToDamarisShmem() +{ + if (parameters_set_ == true) { + // Allocate memory in the shared memory section... + dam_err_ = damaris_alloc(variable_name_.c_str(), (void**)&data_ptr_); + if (dam_err_ != DAMARIS_OK) { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_alloc(\"" + << variable_name_ << "\", (void **) &ret_ptr)" + << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; + has_error_ = true; + } + } else { + dam_err_ = -1; + dam_err_sstr_ << " ERROR rank =" << rank_ + << " : class DamarisVar : setDamarisParameter() should be " + "called first so as to define the size of the memory " + "block required for variable : " + << variable_name_ << std::endl; + has_error_ = true; + } + + if (hasError()) { + printError(); + } +} + +template +void DamarisVar::commitVariableDamarisShmem() +{ + // Signal to Damaris we are done writing data for this iteration + dam_err_ = damaris_commit(variable_name_.c_str()); + if (dam_err_ != DAMARIS_OK) { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_commit(\"" + << variable_name_ << "\")" + << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; + has_error_ = true; + } +} + +template +void DamarisVar::clearVariableDamarisShmem() +{ + // Signal to Damaris it has complete charge of the memory area + dam_err_ = damaris_clear(variable_name_.c_str()); + if (dam_err_ != DAMARIS_OK) { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_clear(\"" << variable_name_ + << "\")" + << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; + has_error_ = true; + } + data_ptr_ = nullptr; +} + +template +bool DamarisVar::TestType(const std::string& variable_name) +{ + bool resbool = true; + // This gets the type of the Damaris XML 's + DAMARIS_TYPE_STR vartype; + dam_err_ = damaris_get_type(variable_name.c_str(), &vartype); + if (dam_err_ != DAMARIS_OK) { + dam_err_sstr_ << " ERROR rankDamarisVar::DamarisVar () damaris_get_type(\"" << variable_name_ + << "\", vartype); Damaris error = " << damaris_error_string(dam_err_) << std::endl; + has_error_ = true; + return false; + } + T test_id; + const std::type_info& t1 = typeid(test_id); + + if (vartype == DAMARIS_TYPE_DOUBLE) { + double td = 0.0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_FLOAT) { + float td = 0.0f; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_CHAR) { + char td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_UCHAR) { + unsigned char td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_SHORT) { + short td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_USHORT) { + unsigned short td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_INT) { + int td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_UINT) { + unsigned int td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_LONG) { + long td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_ULONG) { + unsigned long td = 0; + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + resbool = false; + } + } else if (vartype == DAMARIS_TYPE_UNDEFINED) { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name + << "\" has type DAMARIS_TYPE_UNDEFINED" << std::endl; + has_error_ = true; + resbool = false; + } else { + dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name + << "\" is not of available type " << std::endl; + has_error_ = true; + resbool = false; + } + + return resbool; +} + +template +void DamarisVar::formatTypeError(const std::string& var_name, + const std::string& type_name1, + const std::string& type_name2) +{ + dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar () variable_name_: \"" << var_name + << "\" The template type of Type of DamarisVar in the code: " << type_name1 + << " does not match type in XML:" << type_name2 << std::endl; + has_error_ = true; +} + +template class DamarisVar; +template class DamarisVar; +template class DamarisVar; + +} // namespace Opm::DamarisOutput diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index d6c493bd2..0c26d0a1e 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -20,20 +20,11 @@ #ifndef DAMARISVAR_HPP #define DAMARISVAR_HPP -#include - -#include #include -#include #include #include -#include #include -#define BOOST_BIND_GLOBAL_PLACEHOLDERS 1 - -#include - /* File: DamarisVar.hpp Author: Joshua Bowden, Inria @@ -91,49 +82,11 @@ namespace DamarisOutput //!< the ranks data (Damaris version 1.8+) public: - DamarisVarXMLAttributes() - { - // Additional data needed to complete an XML element - type_ = "scalar"; // This is probably not needed as vector data is defined using - // the Layout paramter. Could be useful for cross checking - visualizable_ = "false"; - time_varying_ = "true"; - centering_ = "zonal"; - } - + DamarisVarXMLAttributes(); /** * Creates the XML representation of the variable from the available strings */ - std::string ReturnXMLForVariable() - { - std::ostringstream var_sstr; - - var_sstr << " layout=\"" << this->layout_ << "\""; - if (this->mesh_ != "") - var_sstr << " mesh=\"" << this->mesh_ << "\""; - if (this->type_ != "") - var_sstr << " type=\"" << this->type_ << "\""; - if (this->visualizable_ != "") - var_sstr << " visualizable=\"" << this->visualizable_ << "\""; - if (this->unit_ != "") - var_sstr << " unit=\"" << this->unit_ << "\""; - if (this->time_varying_ != "") - var_sstr << " time_varying=\"" << this->time_varying_ << "\""; - if (this->centering_ != "") - var_sstr << " centering=\"" << this->centering_ << "\""; - if (this->store_ != "") - var_sstr << " store=\"" << this->store_ << "\""; - if (this->script_ != "") - var_sstr << " script=\"" << this->script_ << "\""; - if (this->select_mem_ != "") - var_sstr << " select-mem=\"" << this->select_mem_ << "\""; - if (this->select_file_ != "") - var_sstr << " select-file=\"" << this->select_file_ << "\""; - if (this->select_subset_ != "") - var_sstr << " select-subset=\"" << this->select_subset_ << "\""; - - return var_sstr.str(); - } + std::string ReturnXMLForVariable(); }; class DamarisVarBase @@ -251,34 +204,7 @@ namespace DamarisOutput DamarisVar(int dims, const std::vector& param_names, const std::string& variable_name, - int rank) - : param_names_(param_names) - , variable_name_(variable_name) - , rank_(rank) - { - dam_err_ = DAMARIS_OK; - - assert(param_names_.size() == static_cast(dims)); - assert(dims > 0); - - has_error_ = false; - - // Check that our template type T matches out Damaris XML type - TestType(variable_name); - if (hasError()) { - printError(); // throws a runtime error, with error message from - // dam_err_sstr_ - } - - current_size_ = 0; - num_params_ = param_names_.size(); - param_sizes_.resize(num_params_); - positions_.resize(dims); - - data_ptr_ = nullptr; - parameters_set_ = false; - has_error_ = false; - } + int rank); /** * Constructor - Sets private data values and also initialises the Damaris shared memory area for writing (and @@ -314,24 +240,9 @@ namespace DamarisOutput const std::vector& param_names, const std::vector& param_values, const std::string& variable_name, - int rank) - : param_names_(param_names) - , variable_name_(variable_name) - , rank_(rank) - { - DamarisVar(dims, param_names, variable_name, rank); - setDamarisParameterAndShmem(param_values); // Initialise the memory size in the constructor. - } + int rank); - ~DamarisVar() - { - if (data_ptr_ != nullptr) { - commitVariableDamarisShmem(); - clearVariableDamarisShmem(); - } - if (this->hasError()) - printError(); // flush out any error messages - } + ~DamarisVar(); /** * Allow a user to indicate that the Damaris variable has allocated a size - @@ -345,10 +256,7 @@ namespace DamarisOutput parameters_set_ = true; } - void printError() const override - { - OPM_THROW(std::runtime_error, dam_err_sstr_.str()); - } + void printError() const override; bool hasError() const override { @@ -376,17 +284,7 @@ namespace DamarisOutput /** * Creates the XML representation of the variable from the available strings */ - std::string returnXMLForVariable() - { - std::ostringstream var_sstr; - - var_sstr << " "; - - return var_sstr.str(); - } + std::string returnXMLForVariable(); /** * Method to set the Damaris paramater values and set the shmem region \ref @@ -427,45 +325,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the array of paramater names: * \ref param_names_ */ - void setDamarisParameter(const std::vector& paramSizeVal) override - { - assert(paramSizeVal.size() == static_cast(num_params_)); - - bool resbool = true; - std::size_t total_size = 1; - for (int varnum = 0; varnum < num_params_; varnum++) { - param_sizes_[varnum] = paramSizeVal[varnum]; - total_size *= param_sizes_[varnum]; - - dam_err_ = damaris_parameter_set(param_names_[varnum].c_str(), ¶mSizeVal[varnum], sizeof(int)); - if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_parameter_set(\"" - << param_names_[varnum] << "\", paramSizeVal, sizeof(int)); Damaris error = " - << damaris_error_string(dam_err_) << std::endl; - resbool = false; - has_error_ = true; - } - } - - if (resbool == true) { - parameterIsSet(); // sets parameters_set_ and gets the size of the - // variables block storage (as number of elemnts) - } - - if (total_size > 0) { - current_size_ = total_size; - } else { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar::getDataStoreBlockSize() " - << "The total size of the variable is 0 - please check " - "input paramSizeVal array." - << std::endl; - has_error_ = true; - } - - if (hasError()) { - printError(); - } - } + void setDamarisParameter(const std::vector& paramSizeVal) override; /** * Method to set the Damaris position values. @@ -477,25 +337,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name: \ref * variable_name_ */ - void setDamarisPosition(const std::vector& positionsVals) override - { - assert(positionsVals.size() == static_cast(num_params_)); - - for (int pos_dim = 0; pos_dim < num_params_; pos_dim++) { - positions_[pos_dim] = positionsVals[pos_dim]; - } - dam_err_ = damaris_set_position(variable_name_.c_str(), positionsVals.data()); - if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_set_position(\"" - << variable_name_ - << "\", positionsVals); Damaris error = " << damaris_error_string(dam_err_) << std::endl; - has_error_ = true; - } - - if (hasError()) { - printError(); - } - } + void setDamarisPosition(const std::vector& positionsVals) override; /** * Method to set the internal pointer (data_ptr_) to the Damaris shared @@ -505,31 +347,7 @@ namespace DamarisOutput * string \ref variable_name_ /implicit : Implicitly uses the * class data element : \ref data_ptr_ */ - void setPointersToDamarisShmem() override - { - if (parameters_set_ == true) { - // Allocate memory in the shared memory section... - dam_err_ = damaris_alloc(variable_name_.c_str(), (void**)&data_ptr_); - if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_alloc(\"" - << variable_name_ << "\", (void **) &ret_ptr)" - << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; - has_error_ = true; - } - } else { - dam_err_ = -1; - dam_err_sstr_ << " ERROR rank =" << rank_ - << " : class DamarisVar : setDamarisParameter() should be " - "called first so as to define the size of the memory " - "block required for variable : " - << variable_name_ << std::endl; - has_error_ = true; - } - - if (hasError()) { - printError(); - } - } + void setPointersToDamarisShmem() override; /** * Method to commit the memory of the data written to the Damaris variable - @@ -538,17 +356,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name string \ref * variable_name_ */ - void commitVariableDamarisShmem() override - { - // Signal to Damaris we are done writing data for this iteration - dam_err_ = damaris_commit(variable_name_.c_str()); - if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_commit(\"" - << variable_name_ << "\")" - << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; - has_error_ = true; - } - } + void commitVariableDamarisShmem() override; /** * Method to release the memory of the data written to the Damaris variable - @@ -558,133 +366,18 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name string \ref * variable_name_ */ - void clearVariableDamarisShmem() override - { - // Signal to Damaris it has complete charge of the memory area - dam_err_ = damaris_clear(variable_name_.c_str()); - if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_clear(\"" << variable_name_ - << "\")" - << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; - has_error_ = true; - } - data_ptr_ = nullptr; - } + void clearVariableDamarisShmem() override; private: /** * Method to check that the template parameter T is the same as the requested * type for the variable in the XML file */ - bool TestType(const std::string& variable_name) - { - bool resbool = true; - // This gets the type of the Damaris XML 's - DAMARIS_TYPE_STR vartype; - dam_err_ = damaris_get_type(variable_name.c_str(), &vartype); - if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rankDamarisVar::DamarisVar () damaris_get_type(\"" << variable_name_ - << "\", vartype); Damaris error = " << damaris_error_string(dam_err_) << std::endl; - has_error_ = true; - return false; - } - T test_id; - const std::type_info& t1 = typeid(test_id); - - if (vartype == DAMARIS_TYPE_DOUBLE) { - double td = 0.0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_FLOAT) { - float td = 0.0f; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_CHAR) { - char td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_UCHAR) { - unsigned char td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_SHORT) { - short td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_USHORT) { - unsigned short td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_INT) { - int td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_UINT) { - unsigned int td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_LONG) { - long td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_ULONG) { - unsigned long td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } - } else if (vartype == DAMARIS_TYPE_UNDEFINED) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name - << "\" has type DAMARIS_TYPE_UNDEFINED" << std::endl; - has_error_ = true; - resbool = false; - } else { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name - << "\" is not of available type " << std::endl; - has_error_ = true; - resbool = false; - } - - return resbool; - } + bool TestType(const std::string& variable_name); void formatTypeError(const std::string& var_name, const std::string& type_name1, - const std::string& type_name2) - { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar () variable_name_: \"" << var_name - << "\" The template type of Type of DamarisVar in the code: " << type_name1 - << " does not match type in XML:" << type_name2 << std::endl; - has_error_ = true; - } + const std::string& type_name2); }; // class DamarisVar } // namespace DamarisOutput From cfd5af89b0c292dac664ee258797a4730b5b804b Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 09:25:18 +0100 Subject: [PATCH 09/13] DamarisVar: avoid stringstream member --- opm/simulators/utils/DamarisVar.cpp | 69 +++++++++++++++-------------- opm/simulators/utils/DamarisVar.hpp | 4 +- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.cpp b/opm/simulators/utils/DamarisVar.cpp index b74fac117..cfc474b7d 100644 --- a/opm/simulators/utils/DamarisVar.cpp +++ b/opm/simulators/utils/DamarisVar.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -130,7 +131,7 @@ DamarisVar::~DamarisVar() template void DamarisVar::printError() const { - OPM_THROW(std::runtime_error, dam_err_sstr_.str()); + OPM_THROW(std::runtime_error, dam_err_str_); } template @@ -159,9 +160,9 @@ void DamarisVar::setDamarisParameter(const std::vector& paramSizeVal) dam_err_ = damaris_parameter_set(param_names_[varnum].c_str(), ¶mSizeVal[varnum], sizeof(int)); if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_parameter_set(\"" - << param_names_[varnum] << "\", paramSizeVal, sizeof(int)); Damaris error = " - << damaris_error_string(dam_err_) << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: class DamarisVar : damaris_parameter_set(\"{}\"" + ", paramSizeVal, sizeof(int)); Damaris error = {}\n", + rank_, param_names_[varnum], damaris_error_string(dam_err_)); resbool = false; has_error_ = true; } @@ -175,10 +176,9 @@ void DamarisVar::setDamarisParameter(const std::vector& paramSizeVal) if (total_size > 0) { current_size_ = total_size; } else { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar::getDataStoreBlockSize() " - << "The total size of the variable is 0 - please check " - "input paramSizeVal array." - << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: class DamarisVar::getDataStoreBlockSize() " + "The total size of the variable is 0 - please check " + "input paramSizeVal array.\n", rank_); has_error_ = true; } @@ -197,9 +197,9 @@ void DamarisVar::setDamarisPosition(const std::vector& positionsVals } dam_err_ = damaris_set_position(variable_name_.c_str(), positionsVals.data()); if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_set_position(\"" - << variable_name_ - << "\", positionsVals); Damaris error = " << damaris_error_string(dam_err_) << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: class DamarisVar : damaris_set_position(\"{}\"" + ", positionsVals); Damaris error = {}\n", + rank_, variable_name_, damaris_error_string(dam_err_)); has_error_ = true; } @@ -215,18 +215,17 @@ void DamarisVar::setPointersToDamarisShmem() // Allocate memory in the shared memory section... dam_err_ = damaris_alloc(variable_name_.c_str(), (void**)&data_ptr_); if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_alloc(\"" - << variable_name_ << "\", (void **) &ret_ptr)" - << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: class DamarisVar : damaris_alloc(\"{}\"" + ", (void **) &ret_ptr), Damaris error = {}\n", + rank_, variable_name_, damaris_error_string(dam_err_)); has_error_ = true; } } else { dam_err_ = -1; - dam_err_sstr_ << " ERROR rank =" << rank_ - << " : class DamarisVar : setDamarisParameter() should be " - "called first so as to define the size of the memory " - "block required for variable : " - << variable_name_ << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: class DamarisVar : " + "setDamarisParameter() should be " + "called first to define the size of the memory " + "block required for variable: {}\n", rank_, variable_name_); has_error_ = true; } @@ -241,9 +240,9 @@ void DamarisVar::commitVariableDamarisShmem() // Signal to Damaris we are done writing data for this iteration dam_err_ = damaris_commit(variable_name_.c_str()); if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_commit(\"" - << variable_name_ << "\")" - << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: class DamarisVar : damaris_commit(\"{}\")" + ", Damaris error = {}\n", + rank_, variable_name_, damaris_error_string(dam_err_)); has_error_ = true; } } @@ -254,9 +253,9 @@ void DamarisVar::clearVariableDamarisShmem() // Signal to Damaris it has complete charge of the memory area dam_err_ = damaris_clear(variable_name_.c_str()); if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : class DamarisVar : damaris_clear(\"" << variable_name_ - << "\")" - << ", Damaris error = " << damaris_error_string(dam_err_) << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: class DamarisVar : damaris_clear(\"{}\")" + ", Damaris error = {}\n", + rank_, variable_name_, damaris_error_string(dam_err_)); has_error_ = true; } data_ptr_ = nullptr; @@ -270,8 +269,9 @@ bool DamarisVar::TestType(const std::string& variable_name) DAMARIS_TYPE_STR vartype; dam_err_ = damaris_get_type(variable_name.c_str(), &vartype); if (dam_err_ != DAMARIS_OK) { - dam_err_sstr_ << " ERROR rankDamarisVar::DamarisVar () damaris_get_type(\"" << variable_name_ - << "\", vartype); Damaris error = " << damaris_error_string(dam_err_) << std::endl; + dam_err_str_ = fmt::format(" ERROR rank = {}: DamarisVar::DamarisVar () damaris_get_type(\"{}\"" + ", vartype); Damaris error = {}\n", + rank_, variable_name_, damaris_error_string(dam_err_)); has_error_ = true; return false; } @@ -349,13 +349,13 @@ bool DamarisVar::TestType(const std::string& variable_name) resbool = false; } } else if (vartype == DAMARIS_TYPE_UNDEFINED) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name - << "\" has type DAMARIS_TYPE_UNDEFINED" << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: DamarisVar::DamarisVar():: \"{}\"" + " has type DAMARIS_TYPE_UNDEFINED\n", rank_, variable_name); has_error_ = true; resbool = false; } else { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar():: \"" << variable_name - << "\" is not of available type " << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: DamarisVar::DamarisVar():: \"{}\"" + " is not of available type\n", rank_, variable_name); has_error_ = true; resbool = false; } @@ -368,9 +368,10 @@ void DamarisVar::formatTypeError(const std::string& var_name, const std::string& type_name1, const std::string& type_name2) { - dam_err_sstr_ << " ERROR rank =" << rank_ << " : DamarisVar::DamarisVar () variable_name_: \"" << var_name - << "\" The template type of Type of DamarisVar in the code: " << type_name1 - << " does not match type in XML:" << type_name2 << std::endl; + dam_err_str_ += fmt::format(" ERROR rank = {}: DamarisVar::DamarisVar() variable_name: \"{}\"" + " The template type of Type of DamarisVar in the code: {}" + " does not match type in XML: {}\n", + rank_, var_name, type_name1, type_name2); has_error_ = true; } diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index 0c26d0a1e..a8e3a6a50 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -21,7 +21,6 @@ #define DAMARISVAR_HPP #include -#include #include #include @@ -132,8 +131,7 @@ namespace DamarisOutput int dam_err_; //!< Set to != DAMARIS_OK if a Damaris error was returned by a //!< Damaris API function call bool has_error_; - std::ostringstream dam_err_sstr_; //!< Use dam_err_sstr.str() to return an - //!< error string describing detected error + std::string dam_err_str_; //!< Error string describing detected error DamarisVarXMLAttributes xml_attributes_; //!< The extra elements that need to be part of a Damaris //!< type. They are simple string values that //!< may reference other XML elements (and could be From 033943e42e6fe19c244a86c75daa3b291ded5792 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 09:34:00 +0100 Subject: [PATCH 10/13] DamarisXMLAttributes::ReturnXMLForVariable simplify using lambda and for_each --- opm/simulators/utils/DamarisVar.cpp | 52 +++++++++++++++-------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.cpp b/opm/simulators/utils/DamarisVar.cpp index cfc474b7d..15d4cfb64 100644 --- a/opm/simulators/utils/DamarisVar.cpp +++ b/opm/simulators/utils/DamarisVar.cpp @@ -24,8 +24,10 @@ #include #include +#include #include #include +#include #include namespace Opm::DamarisOutput { @@ -42,33 +44,33 @@ DamarisVarXMLAttributes::DamarisVarXMLAttributes() std::string DamarisVarXMLAttributes::ReturnXMLForVariable() { - std::ostringstream var_sstr; + std::string var_str; - var_sstr << " layout=\"" << this->layout_ << "\""; - if (this->mesh_ != "") - var_sstr << " mesh=\"" << this->mesh_ << "\""; - if (this->type_ != "") - var_sstr << " type=\"" << this->type_ << "\""; - if (this->visualizable_ != "") - var_sstr << " visualizable=\"" << this->visualizable_ << "\""; - if (this->unit_ != "") - var_sstr << " unit=\"" << this->unit_ << "\""; - if (this->time_varying_ != "") - var_sstr << " time_varying=\"" << this->time_varying_ << "\""; - if (this->centering_ != "") - var_sstr << " centering=\"" << this->centering_ << "\""; - if (this->store_ != "") - var_sstr << " store=\"" << this->store_ << "\""; - if (this->script_ != "") - var_sstr << " script=\"" << this->script_ << "\""; - if (this->select_mem_ != "") - var_sstr << " select-mem=\"" << this->select_mem_ << "\""; - if (this->select_file_ != "") - var_sstr << " select-file=\"" << this->select_file_ << "\""; - if (this->select_subset_ != "") - var_sstr << " select-subset=\"" << this->select_subset_ << "\""; + using Entry = std::pair; + auto addAttrib = [&var_str](const Entry& entry) + { + if (!entry.second.empty()) { + var_str += fmt::format(" {}=\"{}\"", entry.first, entry.second); + } + }; - return var_sstr.str(); + const auto entries = std::array{ + Entry{"layout", this->layout_}, + Entry{"mesh", this->mesh_}, + Entry{"type", this->type_}, + Entry{"visualizable", this->visualizable_}, + Entry{"unit", this->unit_}, + Entry{"time_varying", this->time_varying_}, + Entry{"centering", this->centering_}, + Entry{"store", this->store_}, + Entry{"select-mem", this->select_mem_}, + Entry{"select-file", this->select_file_}, + Entry{"select-subset", this->select_subset_} + }; + + std::for_each(entries.begin(), entries.end(), addAttrib); + + return var_str; } template From bdc0790eb8cb61237a6dd29ce02b18349a5d1336 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 09:36:31 +0100 Subject: [PATCH 11/13] DamarisVar::returnXMLForVariable: simplify using fmt --- opm/simulators/utils/DamarisVar.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.cpp b/opm/simulators/utils/DamarisVar.cpp index 15d4cfb64..41760cebd 100644 --- a/opm/simulators/utils/DamarisVar.cpp +++ b/opm/simulators/utils/DamarisVar.cpp @@ -26,7 +26,6 @@ #include #include -#include #include #include @@ -139,14 +138,8 @@ void DamarisVar::printError() const template std::string DamarisVar::returnXMLForVariable() { - std::ostringstream var_sstr; - - var_sstr << " "; - - return var_sstr.str(); + return fmt::format("", variable_name_, + xml_attributes_.ReturnXMLForVariable()); } template From bee1555d2599de3b47a84e8c0254531766804731 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 09:54:31 +0100 Subject: [PATCH 12/13] DamarisVar::TestType: simplify using a lambda --- opm/simulators/utils/DamarisVar.cpp | 84 +++++++++-------------------- 1 file changed, 24 insertions(+), 60 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.cpp b/opm/simulators/utils/DamarisVar.cpp index 41760cebd..79e978cc5 100644 --- a/opm/simulators/utils/DamarisVar.cpp +++ b/opm/simulators/utils/DamarisVar.cpp @@ -273,76 +273,40 @@ bool DamarisVar::TestType(const std::string& variable_name) T test_id; const std::type_info& t1 = typeid(test_id); + auto check = [&variable_name,this](auto td) + { + const std::type_info& t2 = typeid(td); + if (t1 != t2) { + formatTypeError(variable_name, t1.name(), t2.name()); + return false; + } + return true; + }; + if (vartype == DAMARIS_TYPE_DOUBLE) { - double td = 0.0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + resbool = check(double{}); } else if (vartype == DAMARIS_TYPE_FLOAT) { - float td = 0.0f; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + resbool = check(float{}); } else if (vartype == DAMARIS_TYPE_CHAR) { - char td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + resbool = check(char{}); } else if (vartype == DAMARIS_TYPE_UCHAR) { - unsigned char td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + using uchar = unsigned char; + resbool = check(uchar{}); } else if (vartype == DAMARIS_TYPE_SHORT) { - short td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + resbool = check(short{}); } else if (vartype == DAMARIS_TYPE_USHORT) { - unsigned short td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + using ushort = unsigned short; + resbool = check(ushort{}); } else if (vartype == DAMARIS_TYPE_INT) { - int td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + resbool = check(int{}); } else if (vartype == DAMARIS_TYPE_UINT) { - unsigned int td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + using uint = unsigned int; + resbool = check(uint{}); } else if (vartype == DAMARIS_TYPE_LONG) { - long td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + resbool = check(long{}); } else if (vartype == DAMARIS_TYPE_ULONG) { - unsigned long td = 0; - const std::type_info& t2 = typeid(td); - if (t1 != t2) { - formatTypeError(variable_name, t1.name(), t2.name()); - resbool = false; - } + using ulong = unsigned long; + resbool = check(ulong{}); } else if (vartype == DAMARIS_TYPE_UNDEFINED) { dam_err_str_ += fmt::format(" ERROR rank = {}: DamarisVar::DamarisVar():: \"{}\"" " has type DAMARIS_TYPE_UNDEFINED\n", rank_, variable_name); From 498f1b6b50cf0ad56b2b49084954d459dfb1bac7 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 30 Jan 2024 09:58:37 +0100 Subject: [PATCH 13/13] DamarisVar: remove unused base class DamarisVarBase no reason to pay the price of virtual dispatch when nothing use the base class --- opm/simulators/utils/DamarisVar.cpp | 3 ++- opm/simulators/utils/DamarisVar.hpp | 35 +++++++++-------------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/opm/simulators/utils/DamarisVar.cpp b/opm/simulators/utils/DamarisVar.cpp index 79e978cc5..22e506ebf 100644 --- a/opm/simulators/utils/DamarisVar.cpp +++ b/opm/simulators/utils/DamarisVar.cpp @@ -125,8 +125,9 @@ DamarisVar::~DamarisVar() commitVariableDamarisShmem(); clearVariableDamarisShmem(); } - if (this->hasError()) + if (this->hasError()) { printError(); // flush out any error messages + } } template diff --git a/opm/simulators/utils/DamarisVar.hpp b/opm/simulators/utils/DamarisVar.hpp index a8e3a6a50..006a62104 100644 --- a/opm/simulators/utils/DamarisVar.hpp +++ b/opm/simulators/utils/DamarisVar.hpp @@ -88,21 +88,6 @@ namespace DamarisOutput std::string ReturnXMLForVariable(); }; - class DamarisVarBase - { - public: - virtual ~DamarisVarBase() = default; - virtual void printError() const = 0; - virtual bool hasError() const = 0; - virtual void setDamarisParameterAndShmem(const std::vector& paramSizeVal) = 0; - virtual void setDamarisParameter(const std::vector& paramSizeVal) = 0; - virtual void setDamarisPosition(const std::vector& positionsVals) = 0; - virtual void setPointersToDamarisShmem() = 0; - virtual void commitVariableDamarisShmem() = 0; - virtual void clearVariableDamarisShmem() = 0; - virtual const std::string& variable_name() const = 0; - }; // class DamarisVarBase - /** * class to store a Damaris variable representation for the XML file * (can be used with \ref class DamarisKeywords). @@ -112,7 +97,7 @@ namespace DamarisOutput * */ template - class DamarisVar : public DamarisVarBase + class DamarisVar { int num_params_; //!< Each paramater name string will need a value and they //!< are set in SetDamarisParameter() @@ -254,9 +239,9 @@ namespace DamarisOutput parameters_set_ = true; } - void printError() const override; + void printError() const; - bool hasError() const override + bool hasError() const { return has_error_; } @@ -274,7 +259,7 @@ namespace DamarisOutput } } - const std::string& variable_name() const override + const std::string& variable_name() const { return variable_name_; } @@ -293,7 +278,7 @@ namespace DamarisOutput * * */ - void setDamarisParameterAndShmem(const std::vector& paramSizeVal) override + void setDamarisParameterAndShmem(const std::vector& paramSizeVal) { this->setDamarisParameter(paramSizeVal); this->setPointersToDamarisShmem(); @@ -323,7 +308,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the array of paramater names: * \ref param_names_ */ - void setDamarisParameter(const std::vector& paramSizeVal) override; + void setDamarisParameter(const std::vector& paramSizeVal); /** * Method to set the Damaris position values. @@ -335,7 +320,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name: \ref * variable_name_ */ - void setDamarisPosition(const std::vector& positionsVals) override; + void setDamarisPosition(const std::vector& positionsVals); /** * Method to set the internal pointer (data_ptr_) to the Damaris shared @@ -345,7 +330,7 @@ namespace DamarisOutput * string \ref variable_name_ /implicit : Implicitly uses the * class data element : \ref data_ptr_ */ - void setPointersToDamarisShmem() override; + void setPointersToDamarisShmem(); /** * Method to commit the memory of the data written to the Damaris variable - @@ -354,7 +339,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name string \ref * variable_name_ */ - void commitVariableDamarisShmem() override; + void commitVariableDamarisShmem(); /** * Method to release the memory of the data written to the Damaris variable - @@ -364,7 +349,7 @@ namespace DamarisOutput * /implicit : Implicitly uses the variable name string \ref * variable_name_ */ - void clearVariableDamarisShmem() override; + void clearVariableDamarisShmem(); private: /**