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

Dan #3

Closed
wants to merge 22 commits into from
Closed
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
27 changes: 7 additions & 20 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,13 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: sudo apt-get install libgtest-dev cmake -y
- name: Build Gtest
- name: Build Tests
run: |
mkdir -p $HOME/build
cd $HOME/build
sudo cmake /usr/src/googletest/googletest
sudo make
sudo cp lib/libgtest* /usr/lib/
cd ..
sudo rm -rf build
sudo mkdir /usr/local/lib/googletest
sudo ln -s /usr/lib/libgtest.a /usr/local/lib/googletest/libgtest.a
sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/googletest/libgtest_main.a

- name: Compile Test and Main
run: |
rm -rf build
mkdir build && cd build
cmake ..
make
- name: Run tests
run: ./build/MiscadaProjectGp12Tests
make all
- name: Run Tests
run: |
cd build
ctest

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
build/
.vscode/
24 changes: 8 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
cmake_minimum_required(VERSION 3.10)
project(MiscadaProjectGp12)
cmake_minimum_required(VERSION 3.14)
project(MiscadaProjectGp12Tests)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
enable_testing()

#find already installed GoogleTest Packages
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Add executable for the main program
add_executable(MiscadaProjectGp12 gs.cpp)

# Add executable for the tests
add_executable(MiscadaProjectGp12Tests test.cpp gs.cpp)
# 2 executable files should be compiled, Because there are 2 main function in the project
target_compile_definitions(MiscadaProjectGp12Tests PRIVATE TESTS)
target_link_libraries(MiscadaProjectGp12Tests ${GTEST_LIBRARIES} pthread)
add_subdirectory(src)
add_subdirectory(tests)
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_subdirectory(gs)

add_executable(gs_main main.cpp)
target_link_libraries(gs_main gs)
2 changes: 2 additions & 0 deletions src/gs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(gs STATIC gs.cpp)
target_include_directories(gs PUBLIC include)
35 changes: 2 additions & 33 deletions gs.cpp → src/gs/gs.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "gs/gs.h"
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <sstream>
#include <cmath>
#include "gs.h"

// Define simulation parameters
const int width = 256; // Width of the grid
const int height = 256; // Height of the grid
Expand Down Expand Up @@ -108,36 +109,4 @@ double countElementsAboveThreshold(double threshold) {
return (double)(count)/(width*height);
}

#ifndef TESTS
int main(int argc, char* argv[]) {
if (argc != 5){
std::cout << "Usage: " << argv[0] << " <Du> <Dv> <F> <k> <threshold>" << std::endl;
}
else{
Du = std::stod(argv[1]);
Dv = std::stod(argv[2]);
F = std::stod(argv[3]);
k = std::stod(argv[4]);
threshold = std::stod(argv[5]);
}

init();
std::cout << "Simulation initiated." << std::endl;

// Main simulation loop
for (int iteration = 0; iteration < numIterations; ++iteration) {
simulateStep();

// Periodically write to VTK file
if (iteration % outputInterval == 0) {
writeVTKFile(iteration);
}
}

// count the amount of pixels above threshold at end.
double n = countElementsAboveThreshold(threshold);
std::cout << "Simulation completed: P(v > threshold) = " << n << std::endl;

return 0;
}
#endif
3 changes: 2 additions & 1 deletion gs.h → src/gs/include/gs/gs.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#ifndef GS_H
#define GS_H

Expand All @@ -23,4 +24,4 @@ extern const int outputInterval;
extern std::vector<std::vector<double>> u;
extern std::vector<std::vector<double>> v;

#endif // GS_H
#endif
36 changes: 36 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include "gs/gs.h"
#include <iostream>
#include <sstream>

int main(int argc, char* argv[]) {
if (argc != 5){
std::cout << "Usage: " << argv[0] << " <Du> <Dv> <F> <k> <threshold>" << std::endl;
}
else{
Du = std::stod(argv[1]);
Dv = std::stod(argv[2]);
F = std::stod(argv[3]);
k = std::stod(argv[4]);
threshold = std::stod(argv[5]);
}

init();
std::cout << "Simulation initiated." << std::endl;

// Main simulation loop
for (int iteration = 0; iteration < numIterations; ++iteration) {
simulateStep();

// Periodically write to VTK file
if (iteration % outputInterval == 0) {
writeVTKFile(iteration);
}
}

// count the amount of pixels above threshold at end.
double n = countElementsAboveThreshold(threshold);
std::cout << "Simulation completed: P(v > threshold) = " << n << std::endl;

return 0;
}
24 changes: 0 additions & 24 deletions test.cpp

This file was deleted.

25 changes: 25 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

add_library(GTest::GTest INTERFACE IMPORTED)
target_link_libraries(GTest::GTest INTERFACE gtest_main)

add_executable(
gs_test
gs_test.cpp
)

target_link_libraries(
gs_test
PRIVATE
gtest_main
gs
)
include(GoogleTest)
gtest_discover_tests(gs_test)
32 changes: 32 additions & 0 deletions tests/gs_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "gs/gs.h"
#include <gtest/gtest.h>

TEST(GSTests, CountElementsAboveThreshold) {
double threshold = 0.5;
double count = countElementsAboveThreshold(threshold);
ASSERT_GE(count, 0.0);
ASSERT_LE(count, 1.0);
}

TEST(GSTests, GridInitializationTest) {

const int width = 256;
const int height = 256;

init();

for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
EXPECT_EQ(u[x][y], 1.0);

if (x >= 100 && x <= 200 && y >= 100 && y <= 150){
EXPECT_GE(v[x][y], 0.0);
EXPECT_LE(v[x][y], 1.0);
}
else {
EXPECT_EQ(v[x][y], 0.0);
}
}
}
}