Commit bf524d64 authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSim ass3: Code cleanup.

parent 52e5f14c
...@@ -5,6 +5,7 @@ FILE *logfile = NULL; ...@@ -5,6 +5,7 @@ FILE *logfile = NULL;
void logger_open(const char* filename) { void logger_open(const char* filename) {
if( logfile ) if( logfile )
logger_close(); logger_close();
logfile = fopen(filename, "w"); logfile = fopen(filename, "w");
} }
...@@ -13,8 +14,10 @@ void logger(double t, int N, double *y) { ...@@ -13,8 +14,10 @@ void logger(double t, int N, double *y) {
logfile = stdout; logfile = stdout;
fprintf(logfile, "%e", t); fprintf(logfile, "%e", t);
for( int i = 0; i < N; i++ ) for( int i = 0; i < N; i++ )
fprintf(logfile, ",%e", y[i]); fprintf(logfile, ",%e", y[i]);
fprintf(logfile, "\n"); fprintf(logfile, "\n");
} }
...@@ -22,4 +25,3 @@ void logger_close() { ...@@ -22,4 +25,3 @@ void logger_close() {
if( logfile && logfile != stdout && fclose(logfile) ) if( logfile && logfile != stdout && fclose(logfile) )
printf("Failed to close logfile. Error no: %d", errno); printf("Failed to close logfile. Error no: %d", errno);
} }
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
//typedef void (*log_function)(double t, double *y);
void logger(double t, int N, double *y); void logger(double t, int N, double *y);
void logger_close(); void logger_close();
......
...@@ -2,12 +2,7 @@ ...@@ -2,12 +2,7 @@
from sys import argv, stdin from sys import argv, stdin
from pylab import figure, clf, plot, show, savefig from pylab import figure, clf, plot, show, savefig
#try: # Collect data
# f = open(argv[1], 'r')
#except IndexError:
f = stdin
lines = f.readlines()
f.close()
x = [] x = []
y = [[] for i in range(lines[0].count(','))] y = [[] for i in range(lines[0].count(','))]
for i in range(len(lines)): for i in range(len(lines)):
...@@ -18,6 +13,8 @@ for i in range(len(lines)): ...@@ -18,6 +13,8 @@ for i in range(len(lines)):
y_values = parts[1].split(',') y_values = parts[1].split(',')
for j in range(len(y_values)): for j in range(len(y_values)):
y[j].append(y_values[j]) y[j].append(y_values[j])
# Plot and optionally save data
figure(1) figure(1)
clf() clf()
for i in range(len(y)): for i in range(len(y)):
......
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include "methods.h" #include "integration.h"
#include "logger.h"
#define DT .001 #define DT .01
extern FILE *logfile;
int f_test_1(double t, double *y, double *dy, void *params) { int f_test_1(double t, double *y, double *dy, void *params) {
*dy = 1.0; *dy = 1.0;
...@@ -53,6 +56,8 @@ int f_test_4(double t, double *y, double *dy, void *params) { ...@@ -53,6 +56,8 @@ int f_test_4(double t, double *y, double *dy, void *params) {
int main(void) { int main(void) {
double y0[1] = {.0}, y1[1] = {.0}; double y0[1] = {.0}, y1[1] = {.0};
logfile = fopen("/dev/null", "w");
COMPARE(.0, .0, 10.0, f_test_1); COMPARE(.0, .0, 10.0, f_test_1);
puts(""); puts("");
COMPARE(.0, .0, 10.0, f_test_2); COMPARE(.0, .0, 10.0, f_test_2);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment