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

Test Modules for Reading and Writing #29

Merged
merged 27 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7a4bbb3
Add vector support for preCICE interface
kursatyurt Oct 10, 2021
23bf256
Remove old test files
kursatyurt Oct 10, 2021
af0132a
Add test executables and tests to CMake
kursatyurt Oct 11, 2021
eeffb1b
Collect VTK includes and Remove Duplicates
kursatyurt Oct 11, 2021
f847bac
Bug Fixes
kursatyurt Oct 11, 2021
c6d7ff1
Add vector support for preCICE interface
kursatyurt Oct 10, 2021
9a29b48
Remove old test files
kursatyurt Oct 10, 2021
7e64a43
Add test executables and tests to CMake
kursatyurt Oct 11, 2021
b0e6f05
Collect VTK includes and Remove Duplicates
kursatyurt Oct 11, 2021
0e16779
Bug Fixes
kursatyurt Oct 11, 2021
b124926
Merge branch 'test-read-write' of github.com:kursatyurt/aste into tes…
kursatyurt Oct 11, 2021
63ed703
Add mistakenly removed assertions
kursatyurt Oct 11, 2021
5d48309
Change CMake file for Boost Test
kursatyurt Oct 13, 2021
d12c42d
Convert Read Test to A Boost Unit Test
kursatyurt Oct 13, 2021
7a5103f
Add static_cast to PointId's
kursatyurt Oct 14, 2021
626ac53
Remove md5sum data
kursatyurt Oct 15, 2021
0064b13
Change CMake to single test file
kursatyurt Oct 15, 2021
8c74da7
Change read/write test to void functions
kursatyurt Oct 15, 2021
7fbf001
Add test to a main test file
kursatyurt Oct 15, 2021
c1f5d0e
Ignore vscode directory
kursatyurt Oct 17, 2021
79fd90a
Change from size_t arrays to int array to keep it consistent with pre…
kursatyurt Oct 25, 2021
95fa4e7
Add new functionality to safely cast from vtkIDtype to preCICE
kursatyurt Oct 26, 2021
51fa3a0
Add VID to mesh
kursatyurt Oct 26, 2021
fdef25e
Correct write test cases
kursatyurt Oct 26, 2021
d6f06e6
Adapt VID instead of int type arrays
kursatyurt Oct 26, 2021
7c2efcd
Add init and final time and corrrect timesteps
kursatyurt Oct 26, 2021
ddabf2b
Merge branch 'test-read-write' of github.com:kursatyurt/aste into tes…
kursatyurt Oct 26, 2021
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
34 changes: 33 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ find_package (Threads REQUIRED)

find_package(precice REQUIRED CONFIG)

find_package(Boost 1.65.1 REQUIRED COMPONENTS system program_options filesystem)
find_package(Boost 1.65.1 REQUIRED COMPONENTS system program_options filesystem unit_test_framework)

find_package(VTK REQUIRED)
if (VTK_FOUND)
Expand Down Expand Up @@ -62,6 +62,24 @@ if(METIS_FOUND)
target_link_libraries(preciceMap metisAPI)
endif()

add_executable(write_test src/tests/write_test.cpp src/mesh.cpp)
target_include_directories(write_test PRIVATE src)
target_link_libraries(write_test
Boost::filesystem
Boost::boost
Boost::unit_test_framework
${VTK_LIBRARIES}
)

add_executable(read_test src/tests/read_test.cpp src/mesh.cpp)
target_include_directories(read_test PRIVATE src)
target_link_libraries(read_test
Boost::filesystem
Boost::boost
Boost::unit_test_framework
${VTK_LIBRARIES}
)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/make_mesh.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/visualize_partition.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/partition_mesh.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
Expand All @@ -70,3 +88,17 @@ file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/eval_mesh.py DESTINATION ${C
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/vtk_calculator.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/mesh_io.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/mesh.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

enable_testing()

add_test(write_scalar write_test scalar)
add_test(check_scalar md5sum -c ${CMAKE_CURRENT_SOURCE_DIR}/src/tests/scalarmd5)
set_tests_properties (check_scalar PROPERTIES PASS_REGULAR_EXPRESSION "OK")

add_test(write_vector2d write_test vector2d)
add_test(check_vector2d md5sum -c ${CMAKE_CURRENT_SOURCE_DIR}/src/tests/vector2dmd5)
set_tests_properties (check_vector2d PROPERTIES PASS_REGULAR_EXPRESSION "OK")

add_test(write_vector3d write_test vector3d)
add_test(check_vector3d md5sum -c ${CMAKE_CURRENT_SOURCE_DIR}/src/tests/vector3dmd5)
set_tests_properties (check_vector3d PROPERTIES PASS_REGULAR_EXPRESSION "OK")
59 changes: 28 additions & 31 deletions src/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
#include <sstream>
#include <stdexcept>

#include <vtkDoubleArray.h>
#include <vtkGenericDataObjectReader.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridWriter.h>

#include <vtkCell.h>
#include <vtkCellArray.h>
#include <vtkDoubleArray.h>
Expand Down Expand Up @@ -116,19 +108,20 @@ void readMainFile(Mesh &mesh, const std::string &filename, const std::string &da
}

for (int i = 0; i < reader->GetUnstructuredGridOutput()->GetNumberOfCells(); i++) {
int cellType = reader->GetUnstructuredGridOutput()->GetCell(1)->GetCellType();
int cellType = reader->GetUnstructuredGridOutput()->GetCell(i)->GetCellType();

//Here we use static cast since VTK library returns a long long unsigned int however preCICE uses int for PointId's
if (cellType == VTK_TRIANGLE) {
vtkCell * cell = reader->GetUnstructuredGridOutput()->GetCell(i);
std::array<size_t, 3> elem{cell->GetPointId(0), cell->GetPointId(1), cell->GetPointId(2)};
std::array<size_t, 3> elem{static_cast<int>(cell->GetPointId(0)), static_cast<int>(cell->GetPointId(1)), static_cast<int>(cell->GetPointId(2))};
mesh.triangles.push_back(elem);
} else if (cellType == VTK_LINE) {
vtkCell * cell = reader->GetUnstructuredGridOutput()->GetCell(i);
std::array<size_t, 2> elem{cell->GetPointId(0), cell->GetPointId(1)};
std::array<size_t, 2> elem{static_cast<int>(cell->GetPointId(0)), static_cast<int>(cell->GetPointId(1))};
mesh.edges.push_back(elem);
} else if (cellType == VTK_QUAD) {
vtkCell * cell = reader->GetUnstructuredGridOutput()->GetCell(i);
std::array<size_t, 4> elem{cell->GetPointId(0), cell->GetPointId(1), cell->GetPointId(2), cell->GetPointId(3)};
std::array<size_t, 4> elem{static_cast<int>(cell->GetPointId(0)), static_cast<int>(cell->GetPointId(1)), static_cast<int>(cell->GetPointId(2)), static_cast<int>(cell->GetPointId(3))};
mesh.quadrilaterals.push_back(elem);
} else {
throw std::runtime_error{
Expand All @@ -155,7 +148,7 @@ void MeshName::createDirectories() const

void MeshName::save(const Mesh &mesh, const std::string &dataname) const
{
const int numComp = mesh.positions.size() / mesh.data.size();
const int numComp = mesh.data.size() / mesh.positions.size();
vtkSmartPointer<vtkUnstructuredGrid> unstructuredGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkDoubleArray> data = vtkDoubleArray::New();
Expand All @@ -164,59 +157,63 @@ void MeshName::save(const Mesh &mesh, const std::string &dataname) const
data->SetNumberOfComponents(numComp);

// Insert Points and Point Data
for (size_t i = 0; i < mesh.positions.size() - 1; i++) {
for (size_t i = 0; i < mesh.positions.size(); i++) {
points->InsertNextPoint(mesh.positions[i][0], mesh.positions[i][1], mesh.positions[i][2]);
for (int j = 0; j < numComp; j++)
data->InsertNextTuple(&mesh.data[i * numComp + j]);
std::vector<double> pointData;
for (int j = 0; j < numComp; j++) {
pointData.push_back(mesh.data[i * numComp + j]);
}
data->InsertNextTuple(pointData.data());
}

unstructuredGrid->SetPoints(points);
unstructuredGrid->GetPointData()->AddArray(data);

// Connectivity Information
vtkSmartPointer<vtkCellArray> cellArray = vtkSmartPointer<vtkCellArray>::New();

std::vector<int> cellTypes;
cellTypes.reserve(mesh.quadrilaterals.size() + mesh.triangles.size() + mesh.edges.size());

if (mesh.quadrilaterals.size() > 0) {
vtkSmartPointer<vtkCellArray> quadArray = vtkSmartPointer<vtkCellArray>::New();

for (size_t i = 0; i < mesh.quadrilaterals.size(); i++) {
vtkSmartPointer<vtkQuad> quadrilateral = vtkSmartPointer<vtkQuad>::New();

quadrilateral->GetPointIds()->SetId(0, mesh.quadrilaterals[i][0]);
quadrilateral->GetPointIds()->SetId(0, mesh.quadrilaterals[i][1]);
quadrilateral->GetPointIds()->SetId(0, mesh.quadrilaterals[i][2]);
quadrilateral->GetPointIds()->SetId(0, mesh.quadrilaterals[i][3]);
quadrilateral->GetPointIds()->SetId(1, mesh.quadrilaterals[i][1]);
quadrilateral->GetPointIds()->SetId(2, mesh.quadrilaterals[i][2]);
quadrilateral->GetPointIds()->SetId(3, mesh.quadrilaterals[i][3]);

quadArray->InsertNextCell(quadrilateral);
cellArray->InsertNextCell(quadrilateral);
cellTypes.push_back(VTK_QUAD);
}
unstructuredGrid->SetCells(VTK_QUAD, quadArray);
}

if (mesh.triangles.size() > 0) {
vtkSmartPointer<vtkCellArray> triArray = vtkSmartPointer<vtkCellArray>::New();
for (size_t i = 0; i < mesh.triangles.size(); i++) {
vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();

triangle->GetPointIds()->SetId(0, mesh.triangles[i][0]);
triangle->GetPointIds()->SetId(1, mesh.triangles[i][1]);
triangle->GetPointIds()->SetId(2, mesh.triangles[i][2]);

triArray->InsertNextCell(triangle);
cellArray->InsertNextCell(triangle);
cellTypes.push_back(VTK_TRIANGLE);
}
unstructuredGrid->SetCells(VTK_TRIANGLE, triArray);
}

if (mesh.edges.size() > 0) {
vtkSmartPointer<vtkCellArray> lineArray = vtkSmartPointer<vtkCellArray>::New();
for (size_t i = 0; i < mesh.edges.size(); i++) {
vtkSmartPointer<vtkLine> line = vtkSmartPointer<vtkLine>::New();

line->GetPointIds()->SetId(0, mesh.edges[i][0]);
line->GetPointIds()->SetId(1, mesh.edges[i][1]);

lineArray->InsertNextCell(line);
cellArray->InsertNextCell(line);
cellTypes.push_back(VTK_LINE);
}
unstructuredGrid->SetCells(VTK_LINE, lineArray);
}

unstructuredGrid->SetCells(cellTypes.data(), cellArray);

// Write file
vtkSmartPointer<vtkUnstructuredGridWriter> writer =
vtkSmartPointer<vtkUnstructuredGridWriter>::New();
Expand Down
4 changes: 0 additions & 4 deletions src/tests/edges.conn.txt

This file was deleted.

4 changes: 0 additions & 4 deletions src/tests/edges.txt

This file was deleted.

75 changes: 75 additions & 0 deletions src/tests/read_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

#define BOOST_TEST_MODULE ASTE
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include <iostream>
#include <mesh.hpp>
#include <numeric>
#include <string>
#include <vector>

struct Case {
std::string fname{};
std::string dataname{};
int dim{};
};

std::vector<Case> Cases;

BOOST_AUTO_TEST_SUITE(read_test)

BOOST_AUTO_TEST_CASE(read_test)

{
//Add Cases Here
Cases.push_back(Case{"../src/tests/reference_scalars", "Scalars", 1});
Cases.push_back(Case{"../src/tests/reference_vector2d", "Vector2D", 2});
Cases.push_back(Case{"../src/tests/reference_vector3d", "Vector3D", 3});

for (auto current_case : Cases) {
auto read_test = aste::BaseName{current_case.fname}.with(aste::ExecutionContext());
auto mesh = read_test.load(current_case.dim, current_case.dataname);

BOOST_TEST(mesh.positions.size() == 12);
BOOST_TEST(mesh.edges.size() == 2);
BOOST_TEST(mesh.quadrilaterals.size() == 2);
BOOST_TEST(mesh.triangles.size() == 4);
//std::cout << "Number of mesh elements are correctly loaded\n";

BOOST_TEST(mesh.edges[1][0] == 10);
BOOST_TEST(mesh.edges[1][1] == 11);
//std::cout << "Edges loaded correctly\n";
BOOST_TEST(mesh.triangles[0][0] == 0);
BOOST_TEST(mesh.triangles[0][1] == 1);
BOOST_TEST(mesh.triangles[0][2] == 3);
//std::cout << "Triangles loaded correctly\n";
BOOST_TEST(mesh.quadrilaterals[1][0] == 4);
BOOST_TEST(mesh.quadrilaterals[1][1] == 5);
BOOST_TEST(mesh.quadrilaterals[1][2] == 8);
BOOST_TEST(mesh.quadrilaterals[1][3] == 7);
//std::cout << "Quads loaded correctly\n";

switch (current_case.dim) {
case 1:
BOOST_TEST(mesh.data.size() == 12);
break;
case 2:
BOOST_TEST(mesh.data.size() == 24);
break;
case 3:
BOOST_TEST(mesh.data.size() == 36);
break;
}

std::vector<double> testdata;
testdata.resize(mesh.data.size());

std::iota(testdata.begin(), testdata.end(), 0);

for (size_t i = 0; i < mesh.data.size(); ++i) {
BOOST_TEST(mesh.data[i] == testdata[i]);
}
}
} // End of test suite
BOOST_AUTO_TEST_SUITE_END()
35 changes: 35 additions & 0 deletions src/tests/reference_scalars.vtk
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# vtk DataFile Version 4.1
vtk output
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 12 float
0 0 0 1 0 0 2 0 0
0 1 0 1 1 0 2 1 0
0 2 0 1 2 0 2 2 0
4 0 0 5 0 0 6 0 0

CELLS 8 32
4 3 4 7 6
4 4 5 8 7
3 0 1 3
3 1 4 3
3 1 2 4
3 2 4 5
2 9 10
2 10 11

CELL_TYPES 8
9
9
5
5
5
5
3
3

POINT_DATA 12
FIELD FieldData 1
Scalars 1 12 double
0 1 2 3 4 5 6 7 8
9 10 11
36 changes: 36 additions & 0 deletions src/tests/reference_vector2d.vtk
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# vtk DataFile Version 4.1
vtk output
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 12 float
0 0 0 1 0 0 2 0 0
0 1 0 1 1 0 2 1 0
0 2 0 1 2 0 2 2 0
4 0 0 5 0 0 6 0 0

CELLS 8 32
4 3 4 7 6
4 4 5 8 7
3 0 1 3
3 1 4 3
3 1 2 4
3 2 4 5
2 9 10
2 10 11

CELL_TYPES 8
9
9
5
5
5
5
3
3

POINT_DATA 12
FIELD FieldData 1
Vector2D 2 12 double
0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17
18 19 20 21 22 23
38 changes: 38 additions & 0 deletions src/tests/reference_vector3d.vtk
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# vtk DataFile Version 4.1
vtk output
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 12 float
0 0 0 1 0 0 2 0 0
0 1 0 1 1 0 2 1 0
0 2 0 1 2 0 2 2 0
4 0 0 5 0 0 6 0 0

CELLS 8 32
4 3 4 7 6
4 4 5 8 7
3 0 1 3
3 1 4 3
3 1 2 4
3 2 4 5
2 9 10
2 10 11

CELL_TYPES 8
9
9
5
5
5
5
3
3

POINT_DATA 12
FIELD FieldData 1
Vector3D 3 12 double
0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35

1 change: 1 addition & 0 deletions src/tests/scalarmd5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1f71cdb40a502bf0b2dc8a79e5143f4d write_test_scalars.vtk
4 changes: 0 additions & 4 deletions src/tests/simple.txt

This file was deleted.

2 changes: 0 additions & 2 deletions src/tests/triangles.conn.txt

This file was deleted.

4 changes: 0 additions & 4 deletions src/tests/triangles.txt

This file was deleted.

1 change: 1 addition & 0 deletions src/tests/vector2dmd5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
402bd93cf239b957b07bc0ab91300f0c write_test_vector2d.vtk
Loading