From aee15203df585e6de95b2532e25469b7382253a0 Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 7 Sep 2023 13:59:19 +0200 Subject: [PATCH] Made some changes --- src/main.cpp | 24 +++++++++++++++++ src/simpleFile.cpp | 65 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 src/main.cpp diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..65bbf4a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,24 @@ +#include "GeneralAlgorithm.hpp" +#include +#include + +double f(double x) { + return 100. * std::exp(-10.*x); +} + +double a_sol(double x) { + return 1. - (1. - std::exp(-10)) * x - std::exp(-10*x); +} + +int main() { + arma::mat A = arma::eye(3,3); + + GeneralAlgorithm ga(3, &A, f, a_sol, 0., 1.); + + ga.solve(); + std::cout << "Time: " << ga.time(5) << std::endl; + ga.error(); + + return 0; + +} diff --git a/src/simpleFile.cpp b/src/simpleFile.cpp index 738b902..7ad5b38 100644 --- a/src/simpleFile.cpp +++ b/src/simpleFile.cpp @@ -1,10 +1,13 @@ #include #include +#include #include #include #include #include +#define TIMING_ITERATIONS 5 + arma::vec* general_algorithm( arma::vec* sub_diag, arma::vec* main_diag, @@ -37,7 +40,7 @@ arma::vec* special_algorithm( arma::vec* g_vec ) { - + return g_vec; } void error( @@ -66,7 +69,7 @@ void error( } -double analytic_solution(double x) { +double f(double x) { return 100*std::exp(-10*x); } @@ -90,23 +93,55 @@ void build_array( double step_size = 1./ (double) n_steps; for (int i=0; i < n_steps-1; i++) { - (*g_vec)(i) = analytic_solution((i+1)*step_size); + (*g_vec)(i) = f((i+1)*step_size); } } +void timing() { + arma::vec sub_diag, main_diag, sup_diag, g_vec; + int n_steps; + + std::ofstream ofile; + ofile.open("timing.txt"); + + // Timing + for (int i=1; i <= 8; i++) { + n_steps = std::pow(10, i); + clock_t g_1, g_2, s_1, s_2; + double g_res = 0, s_res = 0; + + for (int j=0; j < TIMING_ITERATIONS; j++) { + build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec); + + g_1 = clock(); + + general_algorithm(&sub_diag, &main_diag, &sup_diag, &g_vec); + + g_2 = clock(); + + g_res += (double) (g_2 - g_1) / CLOCKS_PER_SEC; + build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec); + + s_1 = clock(); + + special_algorithm(-1., 2., -1., &g_vec); + + s_2 = clock(); + + s_res += (double) (s_2 - s_1) / CLOCKS_PER_SEC; + + } + ofile + << n_steps << "," + << g_res / (double) TIMING_ITERATIONS << "," + << s_res / (double) TIMING_ITERATIONS << std::endl; + } + + ofile.close(); +} + int main() { - - arma::vec sub_diag, main_diag, sup_diag, g_vec; - int n_steps; - // Timing - for (int i=1; i <= 6; i++) { - n_steps = std::pow(10, i); - - - // construct arrays - - } - + timing(); }