Skip to content

Commit aa455bf

Browse files
authored
Merge pull request #11 from Fuad-HH/assemble_load
Assemble Load Vector. It just atomic adds. Merging without review. Tests are passing.
2 parents 045d9ef + 8e475c1 commit aa455bf

6 files changed

Lines changed: 81 additions & 2 deletions

File tree

src/StiffnessMatrix.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,24 @@ void StiffnessMatrix::printDenseMatrix() const {
182182
printf("\n");
183183
}
184184
}
185+
186+
Kokkos::View<double *> assembleLoadVector(Kokkos::View<double *> elementLoad,
187+
Mesh mesh) {
188+
Kokkos::Profiling::pushRegion("Assemble Load Vector");
189+
190+
Kokkos::View<double *> loadVector("loadVector", mesh.GetNumVertices());
191+
auto mesh_data = mesh.GetData();
192+
int node_per_elem = mesh.GetMeshType();
193+
194+
Kokkos::parallel_for(
195+
"Fill Load Vector", mesh.GetNumElements(),
196+
KOKKOS_LAMBDA(const int elem_id) {
197+
for (int node = 0; node < node_per_elem; ++node) {
198+
int node_id = int(mesh_data(elem_id, node, 0));
199+
Kokkos::atomic_add(&loadVector(node_id), elementLoad(elem_id));
200+
}
201+
});
202+
203+
Kokkos::Profiling::popRegion();
204+
return loadVector;
205+
}

src/StiffnessMatrix.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
#include "Mesh.h"
1111

12+
// This function assembles the element load vector into the global load vector
13+
[[nodiscard]]
14+
Kokkos::View<double *> assembleLoadVector(Kokkos::View<double *> elementLoad,
15+
Mesh mesh);
16+
1217
// Contains the global indices of the element stiffness matrix
1318
// This is paired so that we can sort
1419
// data using Kokkos bitonic sorting algorithm

src/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,21 @@ int main(int argc, char** argv) {
5454
stiffnessMatrix.sortDataByRowCol(element_stiffness);
5555
stiffnessMatrix.assemble(element_stiffness);
5656

57+
auto load_vector =
58+
assembleLoadVector(el_stiffness_and_load.allElementLoadVector, mesh);
59+
5760
#ifndef NDEBUG
5861
stiffnessMatrix.printStiffnessMatrix();
5962
printf("=>----------- Dense Matrix -----------<=\n");
6063
stiffnessMatrix.printDenseMatrix();
64+
65+
printf("=>----------- Load Vector -----------<=\n");
66+
auto load_vector_host = Kokkos::create_mirror_view(load_vector);
67+
Kokkos::deep_copy(load_vector_host, load_vector);
68+
printf("Load vector:\n");
69+
for (int i = 0; i < load_vector_host.size(); i++) {
70+
printf("%f, \n", load_vector_host(i));
71+
}
6172
#endif
6273
}
6374
Kokkos::finalize();

tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ add_executable(test_element_stiffness test_element_stiffness.cpp ${CMAKE_SOURCE_
4646
target_link_libraries(test_element_stiffness PRIVATE Catch2::Catch2WithMain Kokkos::kokkos)
4747
target_include_directories(test_element_stiffness PRIVATE ${CMAKE_SOURCE_DIR}/src)
4848
catch_discover_tests(test_element_stiffness WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
49+
50+
# Test Load Vector
51+
add_executable(test_load_vector test_load_vector_assembly.cpp ${CMAKE_SOURCE_DIR}/src/Mesh.cpp ${CMAKE_SOURCE_DIR}/src/StiffnessMatrix.cpp ${CMAKE_SOURCE_DIR}/src/CalculateStiffnessMatrixAndLoadVector.cpp)
52+
target_link_libraries(test_load_vector PRIVATE Catch2::Catch2WithMain Kokkos::kokkos)
53+
target_include_directories(test_load_vector PRIVATE ${CMAKE_SOURCE_DIR}/src)
54+
catch_discover_tests(test_load_vector WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Created by Fuad Hasan on 4/17/25.
3+
//
4+
5+
#include <catch2/catch_approx.hpp>
6+
#include <catch2/catch_test_macros.hpp>
7+
8+
#include "CalculateStiffnessMatrixAndLoadVector.hpp"
9+
#include "Mesh.h"
10+
#include "StiffnessMatrix.h"
11+
12+
TEST_CASE("Test Load Assembly") {
13+
Kokkos::initialize();
14+
{
15+
std::string mesh_filename = "assets/TriMesh.msh";
16+
Mesh mesh(mesh_filename);
17+
18+
auto el_stiffness_load =
19+
calculateAllElementStiffnessMatrixAndLoadVector(mesh, 1.0);
20+
auto load_vector =
21+
assembleLoadVector(el_stiffness_load.allElementLoadVector, mesh);
22+
23+
REQUIRE(load_vector.size() == mesh.GetNumVertices());
24+
25+
auto load_vector_host = Kokkos::create_mirror_view(load_vector);
26+
Kokkos::deep_copy(load_vector_host, load_vector);
27+
28+
std::vector<double> expected_load_vector = {0.166667, 0.166667, 0.166667,
29+
0.166667, 0.333333};
30+
31+
for (size_t i = 0; i < expected_load_vector.size(); ++i) {
32+
REQUIRE(load_vector_host(i) ==
33+
Catch::Approx(expected_load_vector[i]).epsilon(0.0001));
34+
}
35+
}
36+
Kokkos::finalize();
37+
}

tests/test_matvec_mult.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <Kokkos_Core.hpp>
66
#include <catch2/catch_test_macros.hpp>
7-
#include <iostream>
87

98
#include "MatVecMult.h"
109
#include "catch2/matchers/catch_matchers_floating_point.hpp"
@@ -85,7 +84,7 @@ TEST_CASE("Test matrix-vector multiplication") {
8584

8685
// Expected result
8786
std::vector<double> expected_y = {5.0, 9.0, 18.0};
88-
printf("y size = %d\n", y_h.size());
87+
printf("y size = %zu\n", y_h.size());
8988
REQUIRE(y_h.size() == N);
9089
for (int i = 0; i < N; ++i) {
9190
printf("Expected y[%d] = %f, Actual y[%d] = %f\n", i, expected_y[i], i,

0 commit comments

Comments
 (0)