From 1dd44db0a56af92a4fd65e9be90fa3d9cce92dba Mon Sep 17 00:00:00 2001 From: Fredrik Robertsen Date: Tue, 2 Sep 2025 19:08:54 +0200 Subject: [PATCH] init exercise1 --- exercise1/Makefile | 15 ++++++ exercise1/README.md | 4 ++ exercise1/plot_image.sh | 10 ++++ exercise1/wave_1d.c | 108 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 exercise1/Makefile create mode 100644 exercise1/README.md create mode 100644 exercise1/plot_image.sh create mode 100644 exercise1/wave_1d.c diff --git a/exercise1/Makefile b/exercise1/Makefile new file mode 100644 index 0000000..60ff9ae --- /dev/null +++ b/exercise1/Makefile @@ -0,0 +1,15 @@ +CFLAGS+= -std=c99 -O2 -Wall -Wextra +LDLIBS+= -lm +TARGETS=wave_1d +IMAGES=$(shell find data -type f | sed s/\\.dat/.png/g | sed s/data/images/g ) +.PHONY: all clean dirs plot movie +all: dirs ${TARGETS} +dirs: + mkdir -p data images +plot: ${IMAGES} +images/%.png: data/%.dat + ./plot_image.sh $< +movie: ${IMAGES} + ffmpeg -y -an -i images/%5d.png -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level 3 -r 12 wave.mp4 +clean: + -rm -fr ${TARGETS} data images wave.mp4 diff --git a/exercise1/README.md b/exercise1/README.md new file mode 100644 index 0000000..473b1a9 --- /dev/null +++ b/exercise1/README.md @@ -0,0 +1,4 @@ +* make : builds the executable 'wave_1d' +* ./wave\_1d : fills the 'data/' directory with stored time steps +* make plot : converts saved time steps to png files under 'images/', using gnuplot. Runs faster if launched with e.g. 4 threads (make -j4 plot). +* make movie : converts collection of png files under 'images' into an mp4 movie file diff --git a/exercise1/plot_image.sh b/exercise1/plot_image.sh new file mode 100644 index 0000000..49830f6 --- /dev/null +++ b/exercise1/plot_image.sh @@ -0,0 +1,10 @@ +#! /usr/bin/env bash +SIZE=1024 +DATAFILE=$1 +IMAGEFILE=`echo $1 | sed s/dat$/png/ | sed s/data/images/` +cat < +#include +#include +#include +#include + + +// Option to change numerical precision. +typedef int64_t int_t; +typedef double real_t; + +// Simulation parameters: size, step count, and how often to save the state. +const int_t + N = 1024, + max_iteration = 4000, + snapshot_freq = 10; + +// Wave equation parameters, time step is derived from the space step. +const real_t + c = 1.0, + dx = 1.0; +real_t + dt; + +// Buffers for three time steps, indexed with 2 ghost points for the boundary. +real_t + *buffers[3] = { NULL, NULL, NULL }; + + +#define U_prv(i) buffers[0][(i)+1] +#define U(i) buffers[1][(i)+1] +#define U_nxt(i) buffers[2][(i)+1] + + +// Save the present time step in a numbered file under 'data/'. +void domain_save ( int_t step ) +{ + char filename[256]; + sprintf ( filename, "data/%.5ld.dat", step ); + FILE *out = fopen ( filename, "wb" ); + fwrite ( &U(0), sizeof(real_t), N, out ); + fclose ( out ); +} + + +// TASK: T1 +// Set up our three buffers, fill two with an initial cosine wave, +// and set the time step. +void domain_initialize ( void ) +{ +// BEGIN: T1 + ; +// END: T1 +} + + +// TASK T2: +// Return the memory to the OS. +// BEGIN: T2 +void domain_finalize ( void ) +{ + ; +} +// END: T2 + + +// TASK: T3 +// Rotate the time step buffers. +// BEGIN: T3 + ; +// END: T3 + + +// TASK: T4 +// Derive step t+1 from steps t and t-1. +// BEGIN: T4 + ; +// END: T4 + + +// TASK: T5 +// Neumann (reflective) boundary condition. +// BEGIN: T5 + ; +// END: T5 + + +// TASK: T6 +// Main time integration. +void simulate( void ) +{ +// BEGIN: T6 + int_t iteration=0; + domain_save ( iteration / snapshot_freq ); +// END: T6 +} + + +int main ( void ) +{ + domain_initialize(); + + simulate(); + + domain_finalize(); + exit ( EXIT_SUCCESS ); +}