From 3ac132c8319074ef2a72116c170ee668c640fa47 Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 11 Sep 2023 18:28:42 +0200 Subject: [PATCH] Stuff --- README.md | 33 +++++++++++++++++++ src/Makefile | 7 ++-- src/analyticPlot.py | 19 ----------- src/main.cpp | 80 +++++++-------------------------------------- 4 files changed, 50 insertions(+), 89 deletions(-) delete mode 100644 src/analyticPlot.py diff --git a/README.md b/README.md index 61e5d52..914af04 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,36 @@ ## Practical information - [Project](https://anderkve.github.io/FYS3150/book/projects/project1.html) + +## How to compile C++ code + +Make sure that you are inside the **src** directory before compiling the code. + +Now you can execute the command shown under to compile: + +´´´ + make +´´´ + +This will create object files and link them together into 2 executable files. +These files are called **main** and **analyticPlot**. +To run them, you can simply use the commands below: + +´´´ + ./main +´´´ + +´´´ + ./analyticPlot +´´´ + +## How to generate plots + +For generating the plots, there are 4 Python scripts. +You can run each one of them by using this command: + +´´´ + python +´´´ + +The plots will be saved inside **latex/images**. diff --git a/src/Makefile b/src/Makefile index 4e76388..4cf0407 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,11 +2,13 @@ CC=g++ CCFLAGS= -std=c++11 -OBJS=generalAlgorithm.o specialAlgorithm.o +OBJS=generalAlgorithm.o specialAlgorithm.o funcs.o + +EXEC=main analyticPlot .PHONY: clean -all: main analyticPlot +all: $(EXEC) main: main.o $(OBJS) $(CC) $(CCFLAGS) -o $@ $^ @@ -19,3 +21,4 @@ analyticPlot: analyticPlot.o clean: rm *.o + rm $(EXEC) diff --git a/src/analyticPlot.py b/src/analyticPlot.py deleted file mode 100644 index ff12ef5..0000000 --- a/src/analyticPlot.py +++ /dev/null @@ -1,19 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt - -def main(): - FILENAME = "analytical_solution.pdf" - x = [] - v = [] - - with open('analytical_solution.txt') as f: - for line in f: - a, b = line.strip().split() - x.append(float(a)) - v.append(float(b)) - - plt.plot(x, v) - plt.savefig(FILENAME) - -if __name__ == "__main__": - main() diff --git a/src/main.cpp b/src/main.cpp index 27fbeeb..bc742ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,103 +6,50 @@ #include #include +#include "funcs.hpp" #include "generalAlgorithm.hpp" #include "specialAlgorithm.hpp" #define TIMING_ITERATIONS 5 -void error( - std::string filename, - arma::vec* x_vec, - arma::vec* v_vec, - arma::vec* a_vec -) -{ - std::ofstream ofile; - ofile.open(filename); - - if (!ofile.is_open()) { - exit(1); - } - - for (int i=0; i < a_vec->n_elem; i++) { - double sub = (*a_vec)(i) - (*v_vec)(i); - ofile << std::setprecision(8) << std::scientific << (*x_vec)(i) - << std::setprecision(8) << std::scientific << std::log10(std::abs(sub)) - << std::setprecision(8) << std::scientific << std::log10(std::abs(sub/(*a_vec)(i))) - << std::endl; - } - - ofile.close(); -} - -double f(double x) { - return 100*std::exp(-10*x); -} - -void build_g_vec(int n_steps, arma::vec* g_vec) { - g_vec->resize(n_steps-1); - - double step_size = 1./ (double) n_steps; - for (int i=0; i < n_steps-1; i++) { - (*g_vec)(i) = f((i+1)*step_size); - } -} - -void build_array( - int n_steps, - arma::vec* sub_diag, - arma::vec* main_diag, - arma::vec* sup_diag, - arma::vec* g_vec -) -{ - sub_diag->resize(n_steps-2); - main_diag->resize(n_steps-1); - sup_diag->resize(n_steps-2); - - sub_diag->fill(-1); - main_diag->fill(2); - sup_diag->fill(-1); - - build_g_vec(n_steps, g_vec); -} - - void timing() { arma::vec sub_diag, main_diag, sup_diag, g_vec; int n_steps; std::ofstream ofile; - ofile.open("timing.txt"); + ofile.open("output/timing.txt"); // Timing - for (int i=1; i <= 8; i++) { + for (int i=1; i <= 6; i++) { n_steps = std::pow(10, i); clock_t g_1, g_2, s_1, s_2; double g_res = 0, s_res = 0; + // Repeat a number of times to take an average for (int j=0; j < TIMING_ITERATIONS; j++) { - build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec); + + build_arrays(n_steps, sub_diag, main_diag, sup_diag, g_vec); g_1 = clock(); - general_algorithm(&sub_diag, &main_diag, &sup_diag, &g_vec); + general_algorithm(sub_diag, main_diag, sup_diag, g_vec); g_2 = clock(); g_res += (double) (g_2 - g_1) / CLOCKS_PER_SEC; - build_g_vec(n_steps, &g_vec); + // Rebuild g_vec for the special alg + build_g_vec(n_steps, g_vec); s_1 = clock(); - special_algorithm(-1., 2., -1., &g_vec); + special_algorithm(-1., 2., -1., g_vec); s_2 = clock(); s_res += (double) (s_2 - s_1) / CLOCKS_PER_SEC; } + // Write the average time to file ofile << n_steps << "," << g_res / (double) TIMING_ITERATIONS << "," @@ -112,13 +59,10 @@ void timing() { ofile.close(); } -void error_file() { - -} - int main() { timing(); general_algorithm_main(); + general_algorithm_error(); special_algorithm_main(); }