def show_residual_across_simulation(stats,fname): extract_stats = grep_stats(stats,type='residual') maxsteps = 0 maxiter = 0 minres = 0 maxres = -99 for k,v in extract_stats.items(): maxsteps = max(maxsteps,getattr(k,'step')) maxiter = max(maxiter,getattr(k,'iter')) minres = min(minres,np.log10(v)) maxres = max(maxres,np.log10(v)) # print(getattr(k,'step'),getattr(k,'iter'),v) # print(maxsteps,maxiter,minres,maxres) residual = np.zeros((maxiter,maxsteps+1)) residual[:] = -99 for k,v in extract_stats.items(): step = getattr(k,'step') iter = getattr(k,'iter') if iter is not -1: residual[iter-1,step] = np.log10(v) # Set up latex stuff and fonts # rc('text', usetex=True) # rc('font', **{"sans-serif": ["Arial"], "size": 30}) rc('font', family='serif',size=30) rc('legend', fontsize='small') rc('xtick', labelsize='small') rc('ytick', labelsize='small') fig, ax = plt.subplots(figsize=(15,10)) cmap = plt.get_cmap('Reds') plt.pcolor(residual.T, cmap=cmap, vmin=minres, vmax=maxres) cax = plt.colorbar() cax.set_label('log10(residual)') ax.set_xlabel('iteration') ax.set_ylabel('step') ax.set_xticks(np.arange(maxiter)+0.5, minor=False) ax.set_yticks(np.arange(maxsteps+1)+0.5, minor=False) ax.set_xticklabels(np.arange(maxiter)+1, minor=False) ax.set_yticklabels(np.arange(maxsteps+1), minor=False) plt.tight_layout() plt.savefig(fname, rasterized=True, transparent=True, bbox_inches='tight') plt.show() pass
description['transfer_params'] = tparams # quickly generate block of steps MS = mp.generate_steps(num_procs,sparams,description) # setup parameters "in time" t0 = MS[0].levels[0].prob.t0 dt = 0.5 Tend = 8*dt # get initial values on finest level P = MS[0].levels[0].prob uinit = P.u_exact(t0) # call main function to get things done... uend,stats = mp.run_pfasst(MS,u0=uinit,t0=t0,dt=dt,Tend=Tend) # df.plot(uend.values,interactive=True) # compute exact solution and compare uex = P.u_exact(Tend) print('(classical) error at time %s: %s' %(Tend,abs(uex-uend)/abs(uex))) uex = df.Expression('sin(a*x[0]) * cos(t)',a=np.pi,t=Tend) print('(fenics-style) error at time %s: %s' %(Tend,df.errornorm(uex,uend.values))) extract_stats = grep_stats(stats,iter=-1,type='residual') sortedlist_stats = sort_stats(extract_stats,sortby='step') print(extract_stats,sortedlist_stats)
# setup parameters "in time" t0 = 0 dt = 1/16*np.pi Tend = 16*dt#np.pi # get initial values on finest level P = MS[0].levels[0].prob uinit = P.u_exact(t0) print('Init:',uinit.pos.values,uinit.vel.values) # call main function to get things done... uend,stats = mp.run_pfasst(MS,u0=uinit,t0=t0,dt=dt,Tend=Tend) extract_stats = grep_stats(stats,type='energy') sortedlist_stats = sort_stats(extract_stats,sortby='time') R0 = np.linalg.norm(uinit.pos.values[:]) H0 = 1/2*np.dot(uinit.vel.values[:],uinit.vel.values[:])+0.02/R0 energy_err = [abs(entry[1]-H0)/H0 for entry in sortedlist_stats] fig = plt.figure() plt.plot(energy_err,'bo--') plt.show() # rc('text', usetex=True) # rc('font', family='serif', size=20) # rc('legend', fontsize='small')
MS = mp.generate_steps(num_procs, sparams, description) # setup parameters "in time" t0 = 0 dt = 0.1 Tend = 2 P = MS[0].levels[0].prob uinit = P.u_exact(t0) print('Init:', uinit.values) # call main function to get things done... uend, stats = mp.run_pfasst_serial(MS, u0=uinit, t0=t0, dt=dt, Tend=Tend) # get stats and error extract_stats = grep_stats(stats, type='niter') sortedlist_stats = sort_stats(extract_stats, sortby='time') uex = P.u_exact(Tend) print( 'Error:', np.linalg.norm(uex.values - uend.values, np.inf) / np.linalg.norm(uex.values, np.inf)) print('Min/Max/Sum number of iterations: %s/%s/%s' % (min(entry[1] for entry in sortedlist_stats), max(entry[1] for entry in sortedlist_stats), sum(entry[1] for entry in sortedlist_stats)))
description['num_nodes'] = 3 description['sweeper_class'] = imex_1st_order description['level_params'] = lparams description['transfer_class'] = mesh_to_mesh_1d description['transfer_params'] = tparams # quickly generate block of steps MS = mp.generate_steps(num_procs, sparams, description) # setup parameters "in time" t0 = 0 dt = 0.125 Tend = 4 * dt # get initial values on finest level P = MS[0].levels[0].prob uinit = P.u_exact(t0) # call main function to get things done... uend, stats = mp.run_pfasst_serial(MS, u0=uinit, t0=t0, dt=dt, Tend=Tend) # compute exact solution and compare uex = P.u_exact(Tend) print('error at time %s: %s' % (Tend, np.linalg.norm(uex.values - uend.values, np.inf) / np.linalg.norm(uex.values, np.inf))) extract_stats = grep_stats(stats, iter=-1, type='residual') sortedlist_stats = sort_stats(extract_stats, sortby='step') print(extract_stats, sortedlist_stats)
# quickly generate block of steps MS = mp.generate_steps(num_procs,sparams,description) # setup parameters "in time" t0 = 0 dt = 0.1 Tend = 2 P = MS[0].levels[0].prob uinit = P.u_exact(t0) print('Init:',uinit.values) # call main function to get things done... uend,stats = mp.run_pfasst(MS,u0=uinit,t0=t0,dt=dt,Tend=Tend) # get stats and error extract_stats = grep_stats(stats,type='niter') sortedlist_stats = sort_stats(extract_stats,sortby='time') np.set_printoptions(16) print('u_end:',uend.values,' at time',Tend) # this is for Tend = 2.0, computed with 2k time-steps and M=3 (G-Le) if Tend == 2.0: uex = np.array([1.7092338721248415, -0.17438654047532 ]) print('Error:',np.linalg.norm(uex-uend.values,np.inf)/np.linalg.norm(uex,np.inf)) plt.show()
# quickly generate block of steps MS = mp.generate_steps(num_procs,sparams,description) # setup parameters "in time" t0 = 0 dt = 0.1 Tend = 4*dt # get initial values on finest level P = MS[0].levels[0].prob uinit = P.u_exact(t0) # call main function to get things done... uend,stats = mp.run_pfasst(MS,u0=uinit,t0=t0,dt=dt,Tend=Tend) # compute exact solution and compare uex = P.u_exact(Tend) print('error at time %s: %s' %(Tend,np.linalg.norm(uex.values-uend.values,np.inf)/np.linalg.norm( uex.values,np.inf))) # Get residual at last step (being the max of all residuals.. most likely) on the fine level extract_stats = grep_stats(stats,step=3,level=-1,type='residual') sortedlist_stats = sort_stats(extract_stats,sortby='iter')[1:] # remove '-1' entry print(sortedlist_stats) # Get the error against the analytical solution at the last step extract_stats = grep_stats(stats,step=3,level=-1,type='error') sortedlist_stats = sort_stats(extract_stats,sortby='iter') print(sortedlist_stats)
# setup parameters "in time" t0 = 0 dt = 1 / 16 * np.pi Tend = 16 * dt #np.pi # get initial values on finest level P = MS[0].levels[0].prob uinit = P.u_exact(t0) print('Init:', uinit.pos.values, uinit.vel.values) # call main function to get things done... uend, stats = mp.run_pfasst_serial(MS, u0=uinit, t0=t0, dt=dt, Tend=Tend) extract_stats = grep_stats(stats, type='energy') sortedlist_stats = sort_stats(extract_stats, sortby='time') R0 = np.linalg.norm(uinit.pos.values[:]) H0 = 1 / 2 * np.dot(uinit.vel.values[:], uinit.vel.values[:]) + 0.02 / R0 energy_err = [abs(entry[1] - H0) / H0 for entry in sortedlist_stats] fig = plt.figure() plt.plot(energy_err, 'bo--') plt.show() # rc('text', usetex=True) # rc('font', family='serif', size=20) # rc('legend', fontsize='small')
# setup parameters "in time" t0 = 0 dt = 0.1 Tend = 2 P = MS[0].levels[0].prob uinit = P.u_exact(t0) print("Init:", uinit.values) # call main function to get things done... uend, stats = mp.run_pfasst(MS, u0=uinit, t0=t0, dt=dt, Tend=Tend) # get stats and error extract_stats = grep_stats(stats, type="niter") sortedlist_stats = sort_stats(extract_stats, sortby="time") uex = P.u_exact(Tend) print("Error:", np.linalg.norm(uex.values - uend.values, np.inf) / np.linalg.norm(uex.values, np.inf)) print( "Min/Max/Sum number of iterations: %s/%s/%s" % ( min(entry[1] for entry in sortedlist_stats), max(entry[1] for entry in sortedlist_stats), sum(entry[1] for entry in sortedlist_stats), ) )