From 9f13fd8e323b7c1f83337d6c4780af930e9bdea0 Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Wed, 12 Jun 2024 09:14:37 +0200 Subject: [PATCH 01/11] add unary_exp --- kratos/expression/unary_expression.cpp | 79 ++++++++++++++++++++++++++ kratos/expression/unary_expression.h | 74 ++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 kratos/expression/unary_expression.cpp create mode 100644 kratos/expression/unary_expression.h diff --git a/kratos/expression/unary_expression.cpp b/kratos/expression/unary_expression.cpp new file mode 100644 index 000000000000..3094852677cf --- /dev/null +++ b/kratos/expression/unary_expression.cpp @@ -0,0 +1,79 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Suneth Warnakulasuriya +// + +// System includes +#include +#include + +// Project includes + +// Include base h +#include "unary_expression.h" + +namespace Kratos { + +template +UnaryExpression::UnaryExpression(Expression::ConstPointer pExpression) + : Expression(pExpression->NumberOfEntities()), + mpSourceExpression(pExpression) +{ +} + +template +Expression::Pointer UnaryExpression::Create(Expression::ConstPointer pExpression) +{ + return Kratos::make_intrusive>(std::move(pExpression)); +} + +template +double UnaryExpression::Evaluate( + const IndexType EntityIndex, + const IndexType EntityDataBeginIndex, + const IndexType ComponentIndex) const +{ + return TOperationType::Evaluate(mpSourceExpression->Evaluate(EntityIndex, EntityDataBeginIndex, ComponentIndex)); +} + +template +const std::vector UnaryExpression::GetItemShape() const +{ + return mpSourceExpression->GetItemShape(); +} + +template +std::size_t UnaryExpression::GetMaxDepth() const +{ + return mpSourceExpression->GetMaxDepth() + 1; +} + +template +std::string UnaryExpression::Info() const +{ + std::stringstream msg; + + if constexpr(std::is_same_v) { + msg << "Abs"; + } else if constexpr(std::is_same_v) { + msg << "Log"; + } else { + static_assert(!std::is_same_v, "Unsupported unary operation type."); + } + + msg << "(" << mpSourceExpression << ")"; + return msg.str(); +} + +// template instantiations +template class UnaryExpression; +template class UnaryExpression; + +} // namespace Kratos \ No newline at end of file diff --git a/kratos/expression/unary_expression.h b/kratos/expression/unary_expression.h new file mode 100644 index 000000000000..d87520d65a5d --- /dev/null +++ b/kratos/expression/unary_expression.h @@ -0,0 +1,74 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Suneth Warnakulasuriya +// + +#pragma once + +// System includes +#include +#include + +// Project includes +#include "expression/expression.h" + +namespace Kratos { + +///@name Kratos Classes +///@{ + +namespace UnaryOperations +{ + struct Absolute { static inline constexpr double Evaluate(const double V) { return std::abs(V); } }; + struct Logarithmic { static inline constexpr double Evaluate(const double V) { return std::log(V); } }; +} + +template +class KRATOS_API(KRATOS_CORE) UnaryExpression : public Expression { +public: + ///@name Type definitions + ///@{ + + using IndexType = std::size_t; + + ///@} + ///@name Life cycle + ///@{ + + UnaryExpression(Expression::ConstPointer pExpression); + + ///@} + ///@name Public operations + ///@{ + + static Expression::Pointer Create(Expression::ConstPointer pExpression); + + double Evaluate( + const IndexType EntityIndex, + const IndexType EntityDataBeginIndex, + const IndexType ComponentIndex) const override; + + const std::vector GetItemShape() const override; + + IndexType GetMaxDepth() const override; + + std::string Info() const override; + + ///@} +protected: + ///@name Private member variables + ///@{ + + const Expression::ConstPointer mpSourceExpression; + + ///@} +}; + +} // namespace Kratos \ No newline at end of file From df246829e6270ee7d457df42973975043a290360 Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Wed, 12 Jun 2024 09:14:52 +0200 Subject: [PATCH 02/11] remove unary_abs_exp --- kratos/expression/unary_abs_expression.cpp | 60 ------------------- kratos/expression/unary_abs_expression.h | 69 ---------------------- 2 files changed, 129 deletions(-) delete mode 100644 kratos/expression/unary_abs_expression.cpp delete mode 100644 kratos/expression/unary_abs_expression.h diff --git a/kratos/expression/unary_abs_expression.cpp b/kratos/expression/unary_abs_expression.cpp deleted file mode 100644 index f921b0d197d6..000000000000 --- a/kratos/expression/unary_abs_expression.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// | / | -// ' / __| _` | __| _ \ __| -// . \ | ( | | ( |\__ ` -// _|\_\_| \__,_|\__|\___/ ____/ -// Multi-Physics -// -// License: BSD License -// Kratos default license: kratos/license.txt -// -// Main authors: Suneth Warnakulasuriya -// - -// System includes -#include -#include - -// Project includes - -// Include base h -#include "unary_abs_expression.h" - -namespace Kratos { - -UnaryAbsExpression::UnaryAbsExpression(Expression::ConstPointer pExpression) - : Expression(pExpression->NumberOfEntities()), - mpSourceExpression(pExpression) -{ -} - -Expression::Pointer UnaryAbsExpression::Create(Expression::ConstPointer pExpression) -{ - return Kratos::make_intrusive(std::move(pExpression)); -} - -double UnaryAbsExpression::Evaluate( - const IndexType EntityIndex, - const IndexType EntityDataBeginIndex, - const IndexType ComponentIndex) const -{ - return std::abs(mpSourceExpression->Evaluate(EntityIndex, EntityDataBeginIndex, ComponentIndex)); -} - -const std::vector UnaryAbsExpression::GetItemShape() const -{ - return mpSourceExpression->GetItemShape(); -} - -std::size_t UnaryAbsExpression::GetMaxDepth() const -{ - return mpSourceExpression->GetMaxDepth() + 1; -} - -std::string UnaryAbsExpression::Info() const -{ - std::stringstream msg; - msg << "|" << mpSourceExpression << "|"; - return msg.str(); -} - -} // namespace Kratos \ No newline at end of file diff --git a/kratos/expression/unary_abs_expression.h b/kratos/expression/unary_abs_expression.h deleted file mode 100644 index 62b1c8cc2dda..000000000000 --- a/kratos/expression/unary_abs_expression.h +++ /dev/null @@ -1,69 +0,0 @@ -// | / | -// ' / __| _` | __| _ \ __| -// . \ | ( | | ( |\__ ` -// _|\_\_| \__,_|\__|\___/ ____/ -// Multi-Physics -// -// License: BSD License -// Kratos default license: kratos/license.txt -// -// Main authors: Suneth Warnakulasuriya -// - -#pragma once - -// System includes -#include - -// Project includes -#include "expression/expression.h" - -namespace Kratos { - -///@name Kratos Classes -///@{ - -/** - * @brief Unary abs expression used to get the absolute values of a given input expression. - */ -class KRATOS_API(KRATOS_CORE) UnaryAbsExpression : public Expression { -public: - ///@name Type definitions - ///@{ - - using IndexType = std::size_t; - - ///@} - ///@name Life cycle - ///@{ - - UnaryAbsExpression(Expression::ConstPointer pExpression); - - ///@} - ///@name Public operations - ///@{ - - static Expression::Pointer Create(Expression::ConstPointer pExpression); - - double Evaluate( - const IndexType EntityIndex, - const IndexType EntityDataBeginIndex, - const IndexType ComponentIndex) const override; - - const std::vector GetItemShape() const override; - - IndexType GetMaxDepth() const override; - - std::string Info() const override; - - ///@} -protected: - ///@name Private member variables - ///@{ - - const Expression::ConstPointer mpSourceExpression; - - ///@} -}; - -} // namespace Kratos \ No newline at end of file From 035d88680e03d3d69c7505df64c1e9c833f2976e Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Wed, 12 Jun 2024 09:15:04 +0200 Subject: [PATCH 03/11] add log to exp utils --- kratos/expression/expression_utils.cpp | 13 ++++++- kratos/expression/expression_utils.h | 51 ++++++++++++++++++-------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/kratos/expression/expression_utils.cpp b/kratos/expression/expression_utils.cpp index 472e9924f855..d79448801707 100644 --- a/kratos/expression/expression_utils.cpp +++ b/kratos/expression/expression_utils.cpp @@ -18,7 +18,7 @@ #include "expression/binary_expression.h" #include "expression/literal_expression.h" #include "expression/literal_flat_expression.h" -#include "expression/unary_abs_expression.h" +#include "expression/unary_expression.h" #include "expression/unary_slice_expression.h" #include "expression/unary_statistics_expression.h" #include "utilities/parallel_utilities.h" @@ -90,7 +90,16 @@ Expression::ConstPointer ExpressionUtils::Abs(const Expression::ConstPointer& rp { KRATOS_TRY - return UnaryAbsExpression::Create(rpExpression); + return UnaryExpression::Create(rpExpression); + + KRATOS_CATCH(""); +} + +Expression::ConstPointer ExpressionUtils::Log(const Expression::ConstPointer& rpExpression) +{ + KRATOS_TRY + + return UnaryExpression::Create(rpExpression); KRATOS_CATCH(""); } diff --git a/kratos/expression/expression_utils.h b/kratos/expression/expression_utils.h index 3cf918fb9f6d..99965bad0afb 100644 --- a/kratos/expression/expression_utils.h +++ b/kratos/expression/expression_utils.h @@ -215,7 +215,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns an expression which represents the component wise absolute value of the given expression. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity * , then the returned expression \f$\left|\underline{\mathbb{u}}\right|\f$ * * \f[ @@ -226,10 +226,28 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils */ static Expression::ConstPointer Abs(const Expression::ConstPointer& rpExpression); + /** + * @brief Returns an expression which represents the component wise logarithmic value of the given expression. + * + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity + * , then the returned expression \f$\left|\underline{\mathbb{u}}\right|\f$ + * + * \f[ + * Log\left(\underline{\mathbb{u}}\right) = log\left(u_{ij}\right) + * \f] + * + * @warning Returns nan if the given $\f$u_{ij}\f < 0.0$ + * @warning Returns inf if the given $\f$u_{ij}\f = 0.0$ + * + * @return Expression::ConstPointer Expression which computes component wise logarithmic value. + */ + static Expression::ConstPointer Log(const Expression::ConstPointer& rpExpression); + + /** * @brief Returns an expression which raises each component to the given power. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity * , where P is specified by @a Power. * * \f[ @@ -245,8 +263,8 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns an expression which raises each component to the given power from another expression. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity - * and the @a rpPowerpExpression is \f$\underline{\mathbb{P}}\f$, where \f$p_{ij}\f$ representss \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity + * and the @a rpPowerpExpression is \f$\underline{\mathbb{P}}\f$, where \f$p_{ij}\f$ represents \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity * , then the returned expression can be illustrated as below. * * \f[ @@ -270,7 +288,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns an expression which scales each component to the specified value * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity * , where Scale is specified by @a Scale. * * \f[ @@ -286,8 +304,8 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns an expression which scales each component by a value from another expression. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity - * and the @a rpScaleExpression is \f$\underline{\mathbb{s}}\f$, where \f$s_{ij}\f$ representss \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity + * and the @a rpScaleExpression is \f$\underline{\mathbb{s}}\f$, where \f$s_{ij}\f$ represents \f$j^{th}\f$ component of the flattened entity data for \f$i^{th}\f$ entity * , then the returned expression can be illustrated as below. * * \f[ @@ -311,7 +329,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns an expression having min value from all the components for each entity. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity * , Following illustrates the returned expression which is always a scalar expression having \f$m_i\f$ representing the \f$i^{th}\f$ entity data. * * \f[ @@ -330,7 +348,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns an expression having max value from all the components for each entity. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity * , Following illustrates the returned expression which is always a scalar expression having \f$m_i\f$ representing the \f$i^{th}\f$ entity data. * * \f[ @@ -349,7 +367,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns an expression having sum of component values for each entity. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity * , Following illustrates the returned expression which is always a scalar expression having \f$m_i\f$ representing the \f$i^{th}\f$ entity data. * * \f[ @@ -372,7 +390,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns the sum of the expression assuming it is a flat vector [Shape is not considered]. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) * , Following illustrates the returned value where the entity data is flattened. * * \f[ @@ -392,7 +410,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns the infinity norm of the expression assuming it is a flat vector [Shape is not considered]. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) * , Following illustrates the returned value where the entity data is flattened. * * \f[ @@ -412,7 +430,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns the L2 norm of the expression assuming it is a flat vector [Shape is not considered]. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) * , Following illustrates the returned value where the entity data is flattened. * * \f[ @@ -432,7 +450,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns the P norm of the expression assuming it is a flat vector [Shape is not considered]. * - * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) + * @details If the input @a rpExpression is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) * , Following illustrates the returned value where the entity data is flattened. * * \f[ @@ -454,8 +472,8 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils /** * @brief Returns the inner product between two expressions. * - * @details If the input @a rpExpression1 is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) - * and the input @a rpExpression2 is \f$\underline{\mathbb{v}}\f$, where \f$v_{ij}\f$ representss \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities), + * @details If the input @a rpExpression1 is \f$\underline{\mathbb{u}}\f$, where \f$u_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities) + * and the input @a rpExpression2 is \f$\underline{\mathbb{v}}\f$, where \f$v_{ij}\f$ represents \f$j^{th}\f$ component of the flattened (having \f$N\f$ total components) entity data for \f$i^{th}\f$ entity (having \f$M\f$ entities), * Following illustrates the returned value where the entity data is flattened. This does not consider shapes of the expressions. They should have same size of flattened vectors. * * \f[ @@ -553,6 +571,7 @@ class KRATOS_API(KRATOS_CORE) ExpressionUtils KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1(Collapse) KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1(Abs) + KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1(Log) KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1(EntityMin) KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1(EntityMax) KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1(EntitySum) From f8a45d2d480f0cddce68b59b0b5d55b08c8e64d4 Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Wed, 12 Jun 2024 09:15:13 +0200 Subject: [PATCH 04/11] expose log to python --- kratos/python/add_container_expression_to_python.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/kratos/python/add_container_expression_to_python.cpp b/kratos/python/add_container_expression_to_python.cpp index 84676d51f3ae..0122e5d5aa5e 100644 --- a/kratos/python/add_container_expression_to_python.cpp +++ b/kratos/python/add_container_expression_to_python.cpp @@ -89,6 +89,7 @@ void AddContainerExpressionUtilsToPython(pybind11::module& m, const std::string& m.def("Collapse", &ExpressionUtils::Collapse, py::arg(rName.c_str())); m.def("Abs", &ExpressionUtils::Abs, py::arg(rName.c_str())); + m.def("Log", &ExpressionUtils::Log, py::arg(rName.c_str())); m.def("EntityMin", &ExpressionUtils::EntityMin, py::arg(rName.c_str())); m.def("EntityMax", &ExpressionUtils::EntityMax, py::arg(rName.c_str())); m.def("EntitySum", &ExpressionUtils::EntitySum, py::arg(rName.c_str())); From fbd27e3deb185c6b6f4a416c0ab4f0836b9ff0c5 Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Wed, 12 Jun 2024 09:15:19 +0200 Subject: [PATCH 05/11] add a test --- kratos/tests/test_container_expression.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kratos/tests/test_container_expression.py b/kratos/tests/test_container_expression.py index f8f283b59948..f7b0289f49f9 100644 --- a/kratos/tests/test_container_expression.py +++ b/kratos/tests/test_container_expression.py @@ -2,6 +2,7 @@ import numpy from abc import ABC from abc import abstractmethod +from math import log from typing import Union import KratosMultiphysics as Kratos from KratosMultiphysics.testing.utilities import ReadModelPart @@ -721,6 +722,15 @@ def test_Abs(self): self.assertEqual(v1, -v2) self.assertEqual(v3, abs(v2)) + def test_Log(self): + a = self._GetContainerExpression() + self._Read(a, Kratos.PRESSURE) + b = a * 2.0 + c = Kratos.Expression.Utils.Log(b) + for v1, v2, v3 in zip(a.Evaluate(), b.Evaluate(), c.Evaluate()): + self.assertEqual(v1 * 2, v2) + self.assertEqual(v3, log(v2)) + def test_EntityMin(self): a = self._GetContainerExpression() self._Read(a, Kratos.GREEN_LAGRANGE_STRAIN_TENSOR) From 4878b3b2f41942f5ae7a01c2e28611594bda82af Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Wed, 12 Jun 2024 12:49:35 +0200 Subject: [PATCH 06/11] change Logarithmic to Logarithm --- kratos/expression/expression_utils.cpp | 2 +- kratos/expression/unary_expression.cpp | 2 +- kratos/expression/unary_expression.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kratos/expression/expression_utils.cpp b/kratos/expression/expression_utils.cpp index d79448801707..3e443e6b60f7 100644 --- a/kratos/expression/expression_utils.cpp +++ b/kratos/expression/expression_utils.cpp @@ -99,7 +99,7 @@ Expression::ConstPointer ExpressionUtils::Log(const Expression::ConstPointer& rp { KRATOS_TRY - return UnaryExpression::Create(rpExpression); + return UnaryExpression::Create(rpExpression); KRATOS_CATCH(""); } diff --git a/kratos/expression/unary_expression.cpp b/kratos/expression/unary_expression.cpp index 3094852677cf..001d2a9d3349 100644 --- a/kratos/expression/unary_expression.cpp +++ b/kratos/expression/unary_expression.cpp @@ -74,6 +74,6 @@ std::string UnaryExpression::Info() const // template instantiations template class UnaryExpression; -template class UnaryExpression; +template class UnaryExpression; } // namespace Kratos \ No newline at end of file diff --git a/kratos/expression/unary_expression.h b/kratos/expression/unary_expression.h index d87520d65a5d..fbd55cc0209f 100644 --- a/kratos/expression/unary_expression.h +++ b/kratos/expression/unary_expression.h @@ -26,8 +26,8 @@ namespace Kratos { namespace UnaryOperations { - struct Absolute { static inline constexpr double Evaluate(const double V) { return std::abs(V); } }; - struct Logarithmic { static inline constexpr double Evaluate(const double V) { return std::log(V); } }; + struct Absolute { static inline constexpr double Evaluate(const double V) { return std::abs(V); } }; + struct Logarithm { static inline constexpr double Evaluate(const double V) { return std::log(V); } }; } template From d36cb440ce06ce3058bb1449210d7ab71ab5bb4d Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Wed, 12 Jun 2024 12:59:29 +0200 Subject: [PATCH 07/11] Update kratos/expression/unary_expression.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Máté Kelemen <44344022+matekelemen@users.noreply.github.com> --- kratos/expression/unary_expression.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/expression/unary_expression.cpp b/kratos/expression/unary_expression.cpp index 001d2a9d3349..5f31e753d875 100644 --- a/kratos/expression/unary_expression.cpp +++ b/kratos/expression/unary_expression.cpp @@ -62,7 +62,7 @@ std::string UnaryExpression::Info() const if constexpr(std::is_same_v) { msg << "Abs"; - } else if constexpr(std::is_same_v) { + } else if constexpr(std::is_same_v) { msg << "Log"; } else { static_assert(!std::is_same_v, "Unsupported unary operation type."); From fcbd454b6855693572c1e72cff33a5d14d705973 Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Thu, 13 Jun 2024 06:13:03 +0200 Subject: [PATCH 08/11] remove constexpr --- kratos/expression/unary_expression.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kratos/expression/unary_expression.h b/kratos/expression/unary_expression.h index fbd55cc0209f..8b358ef7f0a5 100644 --- a/kratos/expression/unary_expression.h +++ b/kratos/expression/unary_expression.h @@ -26,8 +26,8 @@ namespace Kratos { namespace UnaryOperations { - struct Absolute { static inline constexpr double Evaluate(const double V) { return std::abs(V); } }; - struct Logarithm { static inline constexpr double Evaluate(const double V) { return std::log(V); } }; + struct Absolute { static inline double Evaluate(const double V) { return std::abs(V); } }; + struct Logarithm { static inline double Evaluate(const double V) { return std::log(V); } }; } template From c757d397626aaf36913d17678ab7c04280483687 Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya Date: Thu, 13 Jun 2024 06:15:43 +0200 Subject: [PATCH 09/11] add documentation --- .../pages/Kratos/Expressions/Utilities/Log.md | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 docs/pages/Kratos/Expressions/Utilities/Log.md diff --git a/docs/pages/Kratos/Expressions/Utilities/Log.md b/docs/pages/Kratos/Expressions/Utilities/Log.md new file mode 100644 index 000000000000..cc92d103ff2b --- /dev/null +++ b/docs/pages/Kratos/Expressions/Utilities/Log.md @@ -0,0 +1,66 @@ +--- +title: Abs +keywords: +tags: [abs, expression] +sidebar: kratos_expressions +summary: +--- + +## Introduction + +This computes the componentwise natural logarithm of the given expression. Assume the input expression is given by $$\underline{\mathbf{u}} = \left\lbrace u_{ij}, \forall (i,j)\in\left[0, M\right)\times\left[0, N\right)\right\rbrace$$ where the $$i^{th}$$ entity's $$j^{th}$$ component is represented by $$u_{ij}$$ with $$i\in \left[0, M\right)$$ for each entity and $$j\in \left[0, N\right)$$ for each component in each entity. The Following equation illustrates the formulation of the resulting expression. + +

$$ Log\left(\underline{\mathbf{u}}\right) = \left\lbrace log\left(u_{ij}\right), \forall (i,j)\in\left[0, M\right)\times\left[0, N\right)\right\rbrace$$

+ +## Use cases + +### Using to compute absolute values +```python +import KratosMultiphysics as Kratos +model = Kratos.Model() +model_part = model.CreateModelPart("test") + +node_1 = model_part.CreateNewNode(1, 0, 0, 0) +node_2 = model_part.CreateNewNode(2, 1, 0, 0) +node_3 = model_part.CreateNewNode(3, 1, 1, 0) + +# setting VELOCITY of each node +node_1.SetValue(Kratos.VELOCITY, Kratos.Array3([-1,-2,-3])) +node_2.SetValue(Kratos.VELOCITY, Kratos.Array3([-4,-5,-6])) +node_3.SetValue(Kratos.VELOCITY, Kratos.Array3([-7,-8,-9])) + +# create the nodal expression +nodal_expression = Kratos.Expression.NodalExpression(model_part) + +# read the VELOCITY from non-historical nodal container +Kratos.Expression.VariableExpressionIO.Read(nodal_expression, Kratos.VELOCITY, False) + +log_nodal_expression = Kratos.Expression.Utils.Log(nodal_expression) + +# now write the absolute value for checking to the ACCELERATION +Kratos.Expression.VariableExpressionIO.Write(log_nodal_expression, Kratos.ACCELERATION, False) + +# now printing +for node in model_part.Nodes: + velocity = node.GetValue(Kratos.VELOCITY) + acceleration = node.GetValue(Kratos.ACCELERATION) + + print(f"node_id: {node.Id}, velocity=[{velocity[0]}, {velocity[1]}, {velocity[2]}], acceleration = [{acceleration[0]}, {acceleration[1]}, {acceleration[2]}]") +``` + +Expected output: +```console + | / | + ' / __| _` | __| _ \ __| + . \ | ( | | ( |\__ \ +_|\_\_| \__,_|\__|\___/ ____/ + Multi-Physics 9.4."3"-docs/add_python_processing_locally-eb00abccc7-FullDebug-x86_64 + Compiled for GNU/Linux and Python3.11 with GCC-13.2 +Compiled with threading and MPI support. +Maximum number of threads: 30. +Running without MPI. +Process Id: 494037 +node_id: 1, velocity=[-1.0, -2.0, -3.0], acceleration = [1.0, 2.0, 3.0] +node_id: 2, velocity=[-4.0, -5.0, -6.0], acceleration = [4.0, 5.0, 6.0] +node_id: 3, velocity=[-7.0, -8.0, -9.0], acceleration = [7.0, 8.0, 9.0] +``` From c718615bd59a0eb5a491c90a2d8de64874a75f69 Mon Sep 17 00:00:00 2001 From: sunethwarna Date: Thu, 13 Jun 2024 07:19:54 +0200 Subject: [PATCH 10/11] update docs --- .../pages/Kratos/Expressions/Utilities/Log.md | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/docs/pages/Kratos/Expressions/Utilities/Log.md b/docs/pages/Kratos/Expressions/Utilities/Log.md index cc92d103ff2b..aba2fe44a42f 100644 --- a/docs/pages/Kratos/Expressions/Utilities/Log.md +++ b/docs/pages/Kratos/Expressions/Utilities/Log.md @@ -1,14 +1,16 @@ --- -title: Abs +title: Log keywords: -tags: [abs, expression] +tags: [log, expression] sidebar: kratos_expressions summary: --- ## Introduction -This computes the componentwise natural logarithm of the given expression. Assume the input expression is given by $$\underline{\mathbf{u}} = \left\lbrace u_{ij}, \forall (i,j)\in\left[0, M\right)\times\left[0, N\right)\right\rbrace$$ where the $$i^{th}$$ entity's $$j^{th}$$ component is represented by $$u_{ij}$$ with $$i\in \left[0, M\right)$$ for each entity and $$j\in \left[0, N\right)$$ for each component in each entity. The Following equation illustrates the formulation of the resulting expression. +This computes the component-wise natural logarithm of the given expression. Assume the input expression is given by $$\underline{\mathbf{u}} = \left\lbrace u_{ij}, \forall (i,j)\in\left[0, M\right)\times\left[0, N\right)\right\rbrace$$ where the $$i^{th}$$ entity's $$j^{th}$$ component is represented by $$u_{ij}$$ with $$i\in \left[0, M\right)$$ for each entity and $$j\in \left[0, N\right)$$ for each component in each entity. The Following equation illustrates the formulation of the resulting expression. + +:warning: **This method returns nan for any component which is $$u_{ij} < 0.0$$ and inf for any component $$u_{ij} = 0.0$$**.

$$ Log\left(\underline{\mathbf{u}}\right) = \left\lbrace log\left(u_{ij}\right), \forall (i,j)\in\left[0, M\right)\times\left[0, N\right)\right\rbrace$$

@@ -25,9 +27,9 @@ node_2 = model_part.CreateNewNode(2, 1, 0, 0) node_3 = model_part.CreateNewNode(3, 1, 1, 0) # setting VELOCITY of each node -node_1.SetValue(Kratos.VELOCITY, Kratos.Array3([-1,-2,-3])) -node_2.SetValue(Kratos.VELOCITY, Kratos.Array3([-4,-5,-6])) -node_3.SetValue(Kratos.VELOCITY, Kratos.Array3([-7,-8,-9])) +node_1.SetValue(Kratos.VELOCITY, Kratos.Array3([1,2,3])) +node_2.SetValue(Kratos.VELOCITY, Kratos.Array3([4,5,6])) +node_3.SetValue(Kratos.VELOCITY, Kratos.Array3([7,8,9])) # create the nodal expression nodal_expression = Kratos.Expression.NodalExpression(model_part) @@ -50,17 +52,16 @@ for node in model_part.Nodes: Expected output: ```console - | / | - ' / __| _` | __| _ \ __| - . \ | ( | | ( |\__ \ + | / | + ' / __| _` | __| _ \ __| + . \ | ( | | ( |\__ \ _|\_\_| \__,_|\__|\___/ ____/ - Multi-Physics 9.4."3"-docs/add_python_processing_locally-eb00abccc7-FullDebug-x86_64 - Compiled for GNU/Linux and Python3.11 with GCC-13.2 + Multi-Physics 9.5."0"-core/expression/feature/add_log_expression-c757d39762-Release-x86_64 + Compiled for GNU/Linux and Python3.12 with GCC-14.1 Compiled with threading and MPI support. -Maximum number of threads: 30. +Maximum number of threads: 36. Running without MPI. -Process Id: 494037 -node_id: 1, velocity=[-1.0, -2.0, -3.0], acceleration = [1.0, 2.0, 3.0] -node_id: 2, velocity=[-4.0, -5.0, -6.0], acceleration = [4.0, 5.0, 6.0] -node_id: 3, velocity=[-7.0, -8.0, -9.0], acceleration = [7.0, 8.0, 9.0] +node_id: 1, velocity=[1.0, 2.0, 3.0], acceleration = [0.0, 0.6931471805599453, 1.0986122886681098] +node_id: 2, velocity=[4.0, 5.0, 6.0], acceleration = [1.3862943611198906, 1.6094379124341003, 1.791759469228055] +node_id: 3, velocity=[7.0, 8.0, 9.0], acceleration = [1.9459101490553132, 2.0794415416798357, 2.1972245773362196] ``` From 6bd5de4ce139e74bc445decc266a030e5b8a51fc Mon Sep 17 00:00:00 2001 From: sunethwarna Date: Thu, 13 Jun 2024 07:31:43 +0200 Subject: [PATCH 11/11] minor --- docs/pages/Kratos/Expressions/Utilities/Log.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/pages/Kratos/Expressions/Utilities/Log.md b/docs/pages/Kratos/Expressions/Utilities/Log.md index aba2fe44a42f..84c37e467b1f 100644 --- a/docs/pages/Kratos/Expressions/Utilities/Log.md +++ b/docs/pages/Kratos/Expressions/Utilities/Log.md @@ -1,16 +1,18 @@ --- title: Log -keywords: +keywords: tags: [log, expression] sidebar: kratos_expressions -summary: +summary: --- ## Introduction This computes the component-wise natural logarithm of the given expression. Assume the input expression is given by $$\underline{\mathbf{u}} = \left\lbrace u_{ij}, \forall (i,j)\in\left[0, M\right)\times\left[0, N\right)\right\rbrace$$ where the $$i^{th}$$ entity's $$j^{th}$$ component is represented by $$u_{ij}$$ with $$i\in \left[0, M\right)$$ for each entity and $$j\in \left[0, N\right)$$ for each component in each entity. The Following equation illustrates the formulation of the resulting expression. -:warning: **This method returns nan for any component which is $$u_{ij} < 0.0$$ and inf for any component $$u_{ij} = 0.0$$**. +> ##### WARNING +> +> This method returns nan for any component which is $$u_{ij} < 0.0$$ and inf for $$u_{ij} = 0.0$$.

$$ Log\left(\underline{\mathbf{u}}\right) = \left\lbrace log\left(u_{ij}\right), \forall (i,j)\in\left[0, M\right)\times\left[0, N\right)\right\rbrace$$

@@ -52,9 +54,9 @@ for node in model_part.Nodes: Expected output: ```console - | / | - ' / __| _` | __| _ \ __| - . \ | ( | | ( |\__ \ + | / | + ' / __| _` | __| _ \ __| + . \ | ( | | ( |\__ \ _|\_\_| \__,_|\__|\___/ ____/ Multi-Physics 9.5."0"-core/expression/feature/add_log_expression-c757d39762-Release-x86_64 Compiled for GNU/Linux and Python3.12 with GCC-14.1