Made some changes

This commit is contained in:
Cory Balaton 2023-09-07 13:59:19 +02:00
parent b7195f8c17
commit aee15203df
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B
2 changed files with 74 additions and 15 deletions

24
src/main.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "GeneralAlgorithm.hpp"
#include <armadillo>
#include <iostream>
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;
}

View File

@ -1,10 +1,13 @@
#include <armadillo> #include <armadillo>
#include <cmath> #include <cmath>
#include <ctime>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <ios> #include <ios>
#include <string> #include <string>
#define TIMING_ITERATIONS 5
arma::vec* general_algorithm( arma::vec* general_algorithm(
arma::vec* sub_diag, arma::vec* sub_diag,
arma::vec* main_diag, arma::vec* main_diag,
@ -37,7 +40,7 @@ arma::vec* special_algorithm(
arma::vec* g_vec arma::vec* g_vec
) )
{ {
return g_vec;
} }
void error( void error(
@ -66,7 +69,7 @@ void error(
} }
double analytic_solution(double x) { double f(double x) {
return 100*std::exp(-10*x); return 100*std::exp(-10*x);
} }
@ -90,23 +93,55 @@ void build_array(
double step_size = 1./ (double) n_steps; double step_size = 1./ (double) n_steps;
for (int i=0; i < n_steps-1; i++) { 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() int main()
{ {
timing();
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
}
} }