From 1a20e639b64c0cf476e3cb836a492d2dbcf5aede Mon Sep 17 00:00:00 2001 From: michael Date: Sat, 30 Oct 2021 22:38:14 +0200 Subject: [PATCH] Added new test for field creation and saving --- CDMATH/tests/swig/CMakeLists.txt | 12 ++-- CDMATH/tests/swig/testFieldCreationAndSave.py | 71 +++++++++++++++++++ 2 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 CDMATH/tests/swig/testFieldCreationAndSave.py diff --git a/CDMATH/tests/swig/CMakeLists.txt b/CDMATH/tests/swig/CMakeLists.txt index 416c484..c71fa87 100755 --- a/CDMATH/tests/swig/CMakeLists.txt +++ b/CDMATH/tests/swig/CMakeLists.txt @@ -11,19 +11,19 @@ ADD_TEST(TestsCDMATHSwig ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/mainTe if (CDMATH_WITH_PETSC) ADD_TEST(TestsLinearSolverSwig ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/linearSolverTests.py) INSTALL(FILES linearSolverTests.py DESTINATION bin/unit_tests) - - -else (CDMATH_WITH_PETSC) endif (CDMATH_WITH_PETSC) -if (CDMATH_WITH_PYTHON AND CDMATH_WITH_POSTPRO) +if (CDMATH_WITH_POSTPRO) ADD_TEST(Tests_VTK_routines ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_VTK_routines.py) ADD_TEST(Tests_PV_routines ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_PV_routines.py) add_subdirectory (BoySurface) -endif () -INSTALL(FILES ${MESH_MED} mainTests.py test_VTK_routines.py test_PV_routines.py DESTINATION bin/unit_tests) + + ADD_TEST(Tests_Field_Creation_And_Saving_routines ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/testFieldCreationAndSave.py ) + + INSTALL(FILES ${MESH_MED} mainTests.py test_VTK_routines.py test_PV_routines.py testFieldCreationAndSave.py DESTINATION bin/unit_tests) +endif (CDMATH_WITH_POSTPRO) diff --git a/CDMATH/tests/swig/testFieldCreationAndSave.py b/CDMATH/tests/swig/testFieldCreationAndSave.py new file mode 100644 index 0000000..d7f5e50 --- /dev/null +++ b/CDMATH/tests/swig/testFieldCreationAndSave.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +# -*-coding:utf-8 -* + +from math import sqrt +import cdmath + +print("Loading a triangular mesh of a 2D square") +filename = "./meshSquare" +M=cdmath.Mesh(filename+".med") + +print("Checking boundary group names") +boundaryFaceGroupNames=M.getNameOfFaceGroups() +boundaryNodeGroupNames=M.getNameOfNodeGroups() +print(len(boundaryFaceGroupNames), " Boundary Face Group detected : ", boundaryFaceGroupNames) +print(len(boundaryNodeGroupNames), " Boundary Node Group detected : ", boundaryNodeGroupNames) + +assert(len(boundaryFaceGroupNames)==5) +assert(len(boundaryNodeGroupNames)==5) + +assert boundaryFaceGroupNames[0]=="Top" +assert boundaryFaceGroupNames[1]=="Right" +assert boundaryFaceGroupNames[2]=="Left" +assert boundaryFaceGroupNames[3]=="Bottom" +assert boundaryFaceGroupNames[4]=="Boundary" + +assert boundaryNodeGroupNames[0]=="Top" +assert boundaryNodeGroupNames[1]=="Right" +assert boundaryNodeGroupNames[2]=="Left" +assert boundaryNodeGroupNames[3]=="Bottom" +assert boundaryNodeGroupNames[4]=="Boundary" + +#Extract domain sizes +xmin = M.getXMin() +xmax = M.getXMax() +ymin = M.getYMin() +ymax = M.getYMax() + +radius = min(xmax-xmin,ymax-ymin)/4 +xcentre = (xmax+xmin)/2 +ycentre = (ymax+ymin)/2 + +nbCells = M.getNumberOfCells() +nbNodes = M.getNumberOfNodes() + +# Create scalar fields +temperature_field_cells = cdmath.Field("Temperature", cdmath.CELLS, M, 1) +temperature_field_nodes = cdmath.Field("Temperature", cdmath.NODES, M, 1) +Tin = 300 +Tout = 400 + +for i in range(nbCells): + x = M.getCell(i).x() + y = M.getCell(i).y() + distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) ) + if distance < radius: + temperature_field_cells[i] = Tin + else: + temperature_field_cells[i] = Tout + +temperature_field_cells.writeMED(filename, False) + +for i in range(nbNodes): + x = M.getNode(i).x() + y = M.getNode(i).y() + distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) ) + if distance < radius: + temperature_field_nodes[i] = Tin + else: + temperature_field_nodes[i] = Tout + +temperature_field_nodes.writeMED(filename, False) -- 2.39.2