Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MEDApp] some fixes in tests #11683

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ void AddCustomUtilitiesToPython(pybind11::module& m)
py::class_<MedTestingUtilities>(m,"MedTestingUtilities")
.def_static("CheckModelPartsAreEqual", &MedTestingUtilities::CheckModelPartsAreEqual)
.def_static("AddGeometriesFromElements", &MedTestingUtilities::AddGeometriesFromElements)
.def_static("ComputeLength", &MedTestingUtilities::ComputeLength)
.def_static("ComputeArea", &MedTestingUtilities::ComputeArea)
.def_static("ComputeVolume", &MedTestingUtilities::ComputeVolume)
.def_static("ComputeDomainSize", &MedTestingUtilities::ComputeDomainSize)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,20 @@ void CheckGeometriesAreEqual(

KRATOS_CHECK_EQUAL(rGeom1.PointsNumber(), rGeom2.PointsNumber());

// make sure to not accidentially compare base-geometries
KRATOS_CHECK_NOT_EQUAL(rGeom1.GetGeometryType(), GeometryData::KratosGeometryType::Kratos_generic_type);
KRATOS_CHECK(GeometryType::IsSame(rGeom1, rGeom2));

KRATOS_CHECK_DOUBLE_EQUAL(rGeom1.DomainSize(), rGeom2.DomainSize());

if (rGeom1.PointsNumber() > 1) {
KRATOS_CHECK_GREATER(rGeom1.DomainSize(), 0.0);
} else if (rGeom1.PointsNumber() == 1) {
KRATOS_CHECK_DOUBLE_EQUAL(rGeom1.DomainSize(), 0.0);
} else {
KRATOS_ERROR << "Geometry with no points found! " << rGeom1 << std::endl;
}

for (std::size_t i=0; i<rGeom1.PointsNumber(); ++i) {
CheckEntitiesAreEqual(rGeom1[i], rGeom2[i]);
}
Expand Down Expand Up @@ -121,14 +133,14 @@ void CheckGeometriesAreEqual(
KRATOS_CHECK_EQUAL(rModelPart1.NumberOfGeometries(), rModelPart2.NumberOfGeometries());

auto check_geoms = [](const ModelPart& rModelPart1, const ModelPart& rModelPart2){
for (auto geom_1 : rModelPart1.Geometries()) {
for (auto geom_2 : rModelPart2.Geometries()) {
if (have_same_nodes(geom_1, geom_2)) {
CheckGeometriesAreEqual(geom_1, geom_2);
for (const auto& r_geom_1 : rModelPart1.Geometries()) {
for (const auto& r_geom_2 : rModelPart2.Geometries()) {
if (have_same_nodes(r_geom_1, r_geom_2)) {
CheckGeometriesAreEqual(r_geom_1, r_geom_2);
goto here;
}
}
KRATOS_ERROR << "no match found for geometry " << geom_1 << std::endl;
KRATOS_ERROR << "no match found for geometry " << r_geom_1 << std::endl;
here:;
}
};
Expand All @@ -140,6 +152,48 @@ void CheckGeometriesAreEqual(
KRATOS_CATCH("")
}

enum class QuantityType
{
LENGTH = 1,
AREA = 2,
VOLUME = 3,
DOMAIN_SIZE,
};


double ComputeGeometricalQuantity(
const ModelPart& rModelPart,
const QuantityType Quantity)
{
std::function<double(const ModelPart::GeometryType&)> access_function;

switch (Quantity)
{
case QuantityType::LENGTH:
access_function = [](const auto& rGeom){return rGeom.Length();};
case QuantityType::AREA:
access_function = [](const auto& rGeom){return rGeom.Area();};
case QuantityType::VOLUME:
access_function = [](const auto& rGeom){return rGeom.Volume();};
default:
access_function = [](const auto& rGeom){return rGeom.DomainSize();};
}

double total_quantity = 0.0;
for (const auto& r_geom : rModelPart.Geometries()) {
if (Quantity == QuantityType::DOMAIN_SIZE ||
r_geom.LocalSpaceDimension() == static_cast<std::size_t>(Quantity)) {
const double quantity = access_function(r_geom);
if (r_geom.LocalSpaceDimension() > 0) {
KRATOS_CHECK_GREATER(quantity, 0.0);
}
total_quantity += quantity;
}
}
return total_quantity;
}


} // helpers namespace

void MedTestingUtilities::CheckModelPartsAreEqual(
Expand Down Expand Up @@ -194,4 +248,25 @@ void MedTestingUtilities::AddGeometriesFromElements(
KRATOS_CATCH("")
}


double MedTestingUtilities::ComputeLength(const ModelPart& rModelPart)
{
return ComputeGeometricalQuantity(rModelPart, QuantityType::LENGTH);
}

double MedTestingUtilities::ComputeArea(const ModelPart& rModelPart)
{
return ComputeGeometricalQuantity(rModelPart, QuantityType::AREA);
}

double MedTestingUtilities::ComputeVolume(const ModelPart& rModelPart)
{
return ComputeGeometricalQuantity(rModelPart, QuantityType::VOLUME);
}

double MedTestingUtilities::ComputeDomainSize(const ModelPart& rModelPart)
{
return ComputeGeometricalQuantity(rModelPart, QuantityType::DOMAIN_SIZE);
}

} // namespace Kratos
121 changes: 4 additions & 117 deletions applications/MedApplication/custom_utilities/med_testing_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,133 +68,20 @@ class KRATOS_API(MED_APPLICATION) MedTestingUtilities
static void AddGeometriesFromElements(
ModelPart& rModelPart);

///@}
///@name Access
///@{


///@}
///@name Inquiry
///@{


///@}
///@name Input and output
///@{

/// Turn back information as a string.
std::string Info() const;

/// Print information about this object.
void PrintInfo(std::ostream& rOStream) const;

/// Print object's data.
void PrintData(std::ostream& rOStream) const;


///@}
///@name Friends
///@{


///@}

protected:
///@name Protected static Member Variables
///@{


///@}
///@name Protected member Variables
///@{


///@}
///@name Protected Operators
///@{
static double ComputeLength(const ModelPart& rModelPart);

static double ComputeArea(const ModelPart& rModelPart);

///@}
///@name Protected Operations
///@{


///@}
///@name Protected Access
///@{


///@}
///@name Protected Inquiry
///@{


///@}
///@name Protected LifeCycle
///@{


///@}

private:
///@name Static Member Variables
///@{


///@}
///@name Member Variables
///@{


///@}
///@name Private Operators
///@{


///@}
///@name Private Operations
///@{


///@}
///@name Private Access
///@{


///@}
///@name Private Inquiry
///@{
static double ComputeVolume(const ModelPart& rModelPart);

static double ComputeDomainSize(const ModelPart& rModelPart);

///@}

}; // Class MedTestingUtilities

///@}

///@name Type Definitions
///@{

///@}
///@name Input and output
///@{

/// input stream function
inline std::istream& operator >> (std::istream& rIStream,
MedTestingUtilities& rThis);

/// output stream function
inline std::ostream& operator << (std::ostream& rOStream,
const MedTestingUtilities& rThis)
{
rThis.PrintInfo(rOStream);
rOStream << std::endl;
rThis.PrintData(rOStream);

return rOStream;
}
///@}

///@} addtogroup block

} // namespace Kratos
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Project includes
#include "containers/model.h"
#include "geometries/quadrilateral_2d_4.h"
#include "geometries/hexahedra_3d_8.h"
#include "processes/structured_mesh_generator_process.h"
#include "testing/testing.h"
#include "custom_io/med_model_part_io.h"
Expand Down Expand Up @@ -133,7 +134,7 @@ KRATOS_TEST_CASE_IN_SUITE(WriteRead2DTriangularMesh, KratosMedFastSuite)
Parameters mesher_parameters(R"(
{
"number_of_divisions": 7,
"element_name": "Element2D3N",
"element_name": "Element3D3N",
"create_skin_sub_model_part" : false,
"create_body_sub_model_part" : false
})");
Expand All @@ -155,7 +156,7 @@ KRATOS_TEST_CASE_IN_SUITE(WriteRead2DQuadrilateralMesh, KratosMedFastSuite)
}

for (std::size_t i=0; i<num_quads; ++i) {
rModelPart.CreateNewGeometry("Quadrilateral2D4", std::vector<ModelPart::IndexType>{
rModelPart.CreateNewGeometry("Quadrilateral3D4", std::vector<ModelPart::IndexType>{
(i*2)+1,
(i*2)+3,
(i*2)+4,
Expand All @@ -177,7 +178,7 @@ KRATOS_TEST_CASE_IN_SUITE(WriteRead2DTriAndQuadMesh, KratosMedFastSuite)

// first writing all quads
for (std::size_t i=0; i<num_quads; ++i) {
rModelPart.CreateNewGeometry("Quadrilateral2D4", std::vector<ModelPart::IndexType>{
rModelPart.CreateNewGeometry("Quadrilateral3D4", std::vector<ModelPart::IndexType>{
(i*2)+1,
(i*2)+3,
(i*2)+4,
Expand All @@ -188,7 +189,7 @@ KRATOS_TEST_CASE_IN_SUITE(WriteRead2DTriAndQuadMesh, KratosMedFastSuite)
// then writing all triangles
for (std::size_t i=0; i<num_quads; ++i) {
rModelPart.CreateNewNode(++node_id, i*1+0.5,1,0);
rModelPart.CreateNewGeometry("Triangle2D3", std::vector<ModelPart::IndexType>{
rModelPart.CreateNewGeometry("Triangle3D3", std::vector<ModelPart::IndexType>{
(i*2)+4,
static_cast<ModelPart::IndexType>(node_id),
(i*2)+2
Expand All @@ -197,4 +198,60 @@ KRATOS_TEST_CASE_IN_SUITE(WriteRead2DTriAndQuadMesh, KratosMedFastSuite)
});
}

KRATOS_TEST_CASE_IN_SUITE(WriteRead3DTetraMesh, KratosMedFastSuite)
{
MedWriteReadModelPart(this->Name(), [](ModelPart& rModelPart){
const double max_x = 1.0;
const double min_x = 0.0;
const double max_y = 1.0;
const double min_y = 0.0;
const double max_z = 1.0;
const double min_z = 0.0;
auto p_point_1 = Kratos::make_intrusive<Node>(1, min_x, min_y, min_z);
auto p_point_2 = Kratos::make_intrusive<Node>(2, max_x, min_y, min_z);
auto p_point_3 = Kratos::make_intrusive<Node>(3, max_x, max_y, min_z);
auto p_point_4 = Kratos::make_intrusive<Node>(4, min_x, max_y, min_z);
auto p_point_5 = Kratos::make_intrusive<Node>(5, min_x, min_y, max_z);
auto p_point_6 = Kratos::make_intrusive<Node>(6, max_x, min_y, max_z);
auto p_point_7 = Kratos::make_intrusive<Node>(7, max_x, max_y, max_z);
auto p_point_8 = Kratos::make_intrusive<Node>(8, min_x, max_y, max_z);
Hexahedra3D8<Node> geometry(p_point_1, p_point_2, p_point_3, p_point_4, p_point_5, p_point_6, p_point_7, p_point_8);

Parameters mesher_parameters(R"(
{
"number_of_divisions": 5,
"element_name": "Element3D4N",
"create_skin_sub_model_part" : false,
"create_body_sub_model_part" : false
})");

StructuredMeshGeneratorProcess(geometry, rModelPart, mesher_parameters).Execute();
// create geometries!
MedTestingUtilities::AddGeometriesFromElements(rModelPart);
});
}

KRATOS_TEST_CASE_IN_SUITE(WriteRead3DHexa, KratosMedFastSuite)
{
MedWriteReadModelPart(this->Name(), [](ModelPart& rModelPart){
const double max_x = 1.0;
const double min_x = 0.0;
const double max_y = 1.0;
const double min_y = 0.0;
const double max_z = 1.0;
const double min_z = 0.0;
auto p_point_1 = Kratos::make_intrusive<Node>(1, min_x, min_y, min_z);
auto p_point_2 = Kratos::make_intrusive<Node>(2, max_x, min_y, min_z);
auto p_point_3 = Kratos::make_intrusive<Node>(3, max_x, max_y, min_z);
auto p_point_4 = Kratos::make_intrusive<Node>(4, min_x, max_y, min_z);
auto p_point_5 = Kratos::make_intrusive<Node>(5, min_x, min_y, max_z);
auto p_point_6 = Kratos::make_intrusive<Node>(6, max_x, min_y, max_z);
auto p_point_7 = Kratos::make_intrusive<Node>(7, max_x, max_y, max_z);
auto p_point_8 = Kratos::make_intrusive<Node>(8, min_x, max_y, max_z);
auto p_geom = Kratos::make_shared<Hexahedra3D8<Node>>(p_point_1, p_point_2, p_point_3, p_point_4, p_point_5, p_point_6, p_point_7, p_point_8);

rModelPart.AddGeometry(p_geom);
});
}

} // Kratos::Testing
5 changes: 5 additions & 0 deletions applications/MedApplication/tests/test_med_model_part_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def _execute_tests(self, med_path, check_fct):
med_io_read_1 = KratosMed.MedModelPartIO(GetMedPath(med_path))
med_io_read_1.ReadModelPart(self.mp_read_1)

self.assertGreaterEqual(KratosMed.MedTestingUtilities.ComputeLength(self.mp_read_1), 0.0)
self.assertGreaterEqual(KratosMed.MedTestingUtilities.ComputeArea(self.mp_read_1), 0.0)
self.assertGreaterEqual(KratosMed.MedTestingUtilities.ComputeVolume(self.mp_read_1), 0.0)
self.assertGreaterEqual(KratosMed.MedTestingUtilities.ComputeDomainSize(self.mp_read_1), 0.0)

with self.subTest("check_model_part"):
check_fct(self.mp_read_1)

Expand Down