Stuff
This commit is contained in:
parent
0edaae5b3f
commit
3ac132c831
33
README.md
33
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 <PythonFile>
|
||||
´´´
|
||||
|
||||
The plots will be saved inside **latex/images**.
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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()
|
||||
80
src/main.cpp
80
src/main.cpp
@ -6,103 +6,50 @@
|
||||
#include <ios>
|
||||
#include <string>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user