Mercurial > repos > public > sbplib
changeset 17:30ae48efc7ae
Added utility function findFiledWidth. Added function for calculating and saving solutions.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 22 Sep 2015 14:27:21 +0200 |
parents | f7975c054bc3 |
children | 4d8068cb5c65 |
files | +noname/calculateSolution.m findFieldWidth.m |
diffstat | 2 files changed, 116 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+noname/calculateSolution.m Tue Sep 22 14:27:21 2015 +0200 @@ -0,0 +1,101 @@ +% Calculates the solution of discretization for a given set of ms ts and orders. +% discrHand -- function handle to a Discretization constructor +% method -- time stepping method +% m -- grid parameter +% order -- order of accuracy of the approximtion +% T -- time to calculate solution for +% input paramters m, t, order may all be vectors. +function [] = calculateSolution(filename, discrHand, method, m, T, order) + + if exist(filename,'file') + fprintf('File ''%s'' already exist.',filename); + do_append = yesnoQuestion('Do you want to append to it?'); + if ~do_append + fprintf('Exiting...\n'); + return + end + end + + sf = SolutionFile(filename); + + % Make sure times are sorted + T = sort(T); + + % Find out if times to be calulated are integer multiples of the smallest one. + time_multiples = T/T(1); + is_int_multiples = all(time_multiples == int64(time_multiples)); + + if is_int_multiples + fprintf('Calculating time series in increments\n'); + else + fprintf('Restarting for each time in timeseries\n'); + end + + + orderWidth = findFieldWidth('%d',order); + mWidth = findFieldWidth('%d',m); + TWidth = findFieldWidth('%d',T); + + for i = 1:length(order) + for j = 1:length(m) + discr = discrHand(m(j),order(i)); + k_max = discr.getTimestep(method); + + % Do we want to to save the initial conditions? + if T(1) == 0 + v = discr.v0; + t = 0; + saveToFile(sf, method, order(i), m(j),T(1), v, t, NaN, NaN); + T(1) = []; + end + + % T now contains all the times we need to step to, + % if T contained 0 it has now been removed. + + if is_int_multiples + % Times are integer multiples, we can save time + [k,N] = alignedTimestep(k_max,T(1)); + ts = discr.getTimestepper(method,k); + runtime = 0; + for l = 1:length(T) + end_step = N * time_multiples(l); + fprintf('[order = %-*d, m = %-*d, T = %-*d]: ',orderWidth,order(i),mWidth,m(j),TWidth,T(l)); + clock_start = tic(); + [v,t] = ts.stepN(end_step-ts.n,true); + runtime = runtime + toc(clock_start); + saveToFile(sf, method, order(i), m(j),T(l), v, t, runtime, k); + fprintf('Done! (%.3fs)\n',runtime); + end + else + % Times are not interger multiples, we have to start from 0 every time. + for l = 1:length(T) + [k,N] = alignedTimestep(k_max,T(l)); + ts = discr.getTimestepper(method,k); + fprintf('[order = %-*d, m = %-*d, T = %-*d]: ',orderWidth,order(i),mWidth,m(j),TWidth,T(l)); + clock_start = tic(); + [v,t] = ts.stepN(N-ts.n,true); + runtime = toc(clock_start); + saveToFile(sf, method, order(i), m(j),T(l), v, t, runtime, k); + fprintf('Done! (%.3fs)\n',runtime); + end + + end + + end + end +end + + +function saveToFile(sf, method, order, m, T, v, t, runtime, k) + key.method = method; + key.order = order; + key.m = m; + key.T = T; + + entry.v = v; + entry.t = t; + entry.runtime = runtime; + entry.k = k; + + sf.store(key,entry); +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/findFieldWidth.m Tue Sep 22 14:27:21 2015 +0200 @@ -0,0 +1,15 @@ +% Calculates the maximum field width needed width fprintf for a given format fmt for the values in A +% A = [1.13232 10.233 1.1]; +% width = findFieldWidth('%.2f',A); +% Gives wdith = 5 +function width = findFieldWidth(fmt, A) + width = 0; + for i = 1:numel(A) + elem = A(i); + if iscell(elem) + elem = elem{1}; + end + str = sprintf(fmt,elem); + width = max(width,length(str)); + end +end