Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New version of paper2_examples #3

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b1cb61a
update paper2_examples/acoustics_1d_ex1 for v5.6.1, with better plots…
rjleveque Nov 24, 2019
8483726
update acoustics_1d_ex2 to v5.6.1
rjleveque Dec 21, 2019
f132860
update paper2_examples/acoustics_2d_ex3 for v5.6.1
rjleveque Dec 22, 2019
9123266
update paper2_examples/acoustics_2d_ex4 to v5.6.1
rjleveque Dec 22, 2019
a4fb398
make it easier to change flagging method in 2d examples
rjleveque Dec 22, 2019
9fe5d76
update paper2_examples/README
rjleveque Dec 22, 2019
8b3c17f
move acoustics_1d_ex1 to acoustics_1d_ex1_old
rjleveque Dec 22, 2019
8781667
move acoustics_1d_ex2 acoustics_1d_example1
rjleveque Dec 22, 2019
9a7d2ad
rename acoustics_2d_ex* directories
rjleveque Dec 22, 2019
849ef67
fixed incorrect renaming of directories in acoustics_2d_example3
rjleveque Dec 23, 2019
f4a286d
600 x 800
rjleveque Dec 26, 2019
c4c6164
updates to example3
rjleveque Dec 27, 2019
c0f1dd6
update example2 with errf1 based on LTE
rjleveque Dec 27, 2019
fef44bc
scripts for plots
rjleveque Dec 27, 2019
06eaa83
change rectangle plotted and color scale for adjoint
rjleveque Dec 27, 2019
c7681f5
increase fontsize
rjleveque Dec 27, 2019
64d1bfb
improve some plots
rjleveque Dec 28, 2019
e918ba1
revert back to original errf1 for forward-error flagging
rjleveque Dec 30, 2019
ebf0add
updates to acoustics_2d_example2 for resubmission
rjleveque Dec 31, 2019
650cf00
update some README files
rjleveque Dec 31, 2019
b3a8db4
rename since no longer using acoustics_2d_ex4_old
rjleveque Dec 31, 2019
c443c73
clean up
rjleveque Dec 31, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update paper2_examples/acoustics_2d_ex3 for v5.6.1
Includes fixed versions of rpn2_vc_acoustics solvers that are not yet in Clawpack release.
rjleveque committed Dec 22, 2019
commit f13286007b055924bcfff2ad21b38edf8e85a50a
10 changes: 5 additions & 5 deletions paper2_examples/acoustics_2d_ex3/Makefile
Original file line number Diff line number Diff line change
@@ -24,14 +24,14 @@ OVERWRITE ?= True # False ==> make a copy of OUTDIR first
# Environment variable FC should be set to fortran compiler, e.g. gfortran

# Compiler flags can be specified here or set as an environment variable
FFLAGS ?= -O2 -fopenmp
FFLAGS ?=

# ---------------------------------
# package sources for this program:
# ---------------------------------

ADJLIB = $(CLAW)/amrclaw/src/2d/adjoint
include $(ADJLIB)/Makefile.adjoint_amr_2d
AMRLIB = $(CLAW)/amrclaw/src/2d
include $(AMRLIB)/Makefile.amr_2d

# ---------------------------------------
# package sources specifically to exclude
@@ -53,8 +53,8 @@ SOURCES += \
qinit.f \
setprob.f \
setaux.f \
$(CLAW)/riemann/src/rpn2_vc_acoustics.f \
$(CLAW)/riemann/src/rpt2_vc_acoustics.f \
rpn2_vc_acoustics.f90 \
$(CLAW)/riemann/src/rpt2_vc_acoustics.f90 \

#-------------------------------------------------------------------
# Include Makefile containing standard definitions and make options:
2 changes: 1 addition & 1 deletion paper2_examples/acoustics_2d_ex3/adjoint/Makefile
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ SOURCES = \
qinit.f \
setprob.f \
setaux.f \
$(CLAW)/riemann/src/rpn2_vc_acoustics_adjoint.f90 \
rpn2_vc_acoustics_adjoint.f90 \
$(CLAW)/riemann/src/rpt2_vc_acoustics_adjoint.f90 \

#-------------------------------------------------------------------
135 changes: 135 additions & 0 deletions paper2_examples/acoustics_2d_ex3/adjoint/rpn2_vc_acoustics_adjoint.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
! =====================================================
subroutine rpn2(ixy,maxm,meqn,mwaves,maux,mbc,mx,ql,qr,auxl,auxr,fwave,s,amdq,apdq)
! =====================================================
! # Adjoint Riemann solver for the acoustics equations in 2d, with varying
! # material properties rho and kappa

! waves: 2
! equations: 3
! aux fields: 2

! Conserved quantities:
! 1 pressure
! 2 x_momentum
! 3 y_momentum

! Auxiliary variables:
! 1 density
! 2 sound_speed

! # Note that although there are 3 eigenvectors, the second eigenvalue
! # is always zero and so we only need to compute 2 waves.
! #
! # solve Riemann problems along one slice of data.

! # On input, ql contains the state vector at the left edge of each cell
! # qr contains the state vector at the right edge of each cell

! # Here it is assumed that auxl=auxr gives the cell values.

! # This data is along a slice in the x-direction if ixy=1
! # or the y-direction if ixy=2.
! # On output, wave contains the waves,
! # s the speeds,
! # amdq the left-going flux difference A^- \Delta q
! # apdq the right-going flux difference A^+ \Delta q


! # Note that the i'th Riemann problem has left state qr(i-1,:)
! # and right state ql(i,:)
! # From the basic clawpack routines, this routine is called with ql = qr

implicit double precision (a-h,o-z)

dimension fwave(meqn, mwaves, 1-mbc:maxm+mbc)
dimension s(mwaves, 1-mbc:maxm+mbc)
dimension ql(meqn, 1-mbc:maxm+mbc)
dimension qr(meqn, 1-mbc:maxm+mbc)
dimension apdq(meqn, 1-mbc:maxm+mbc)
dimension amdq(meqn, 1-mbc:maxm+mbc)
dimension auxl(maux, 1-mbc:maxm+mbc)
dimension auxr(maux, 1-mbc:maxm+mbc)

! local arrays
! ------------
dimension delta(3)

! # set mu to point to the component of the system that corresponds
! # to velocity in the direction of this slice, mv to the orthogonal
! # velocity:

if (ixy == 1) then
mu = 2
mv = 3
else
mu = 3
mv = 2
endif

! # note that notation for u and v reflects assumption that the
! # Riemann problems are in the x-direction with u in the normal
! # direciton and v in the orthogonal direcion, but with the above
! # definitions of mu and mv the routine also works with ixy=2
! # in which case waves come from the
! # Riemann problems u_t + g(u)_y = 0 in the y-direction.


! # split the jump in f(q) at each interface into waves
! # The jump is split into a leftgoing wave traveling at speed -c
! # relative to the material properties to the left of the interface,
! # and a rightgoing wave traveling at speed +c
! # relative to the material properties to the right of the interface,

! # find b1 and b2, the coefficients of the 2 eigenvectors:
do 20 i = 2-mbc, mx+mbc
! # material properties

! # impedances:
zi = auxl(1,i)*auxl(2,i)
zim = auxr(1,i-1)*auxr(2,i-1)

! # sound speed
ci = auxl(2,i)
cim = auxr(2,i-1)

! # density
rhoi = auxl(1,i)
rhoim = auxr(1,i-1)

! # bulk modulus
bulki = rhoi*ci**2
bulkim = rhoim*cim**2

! # f-wave splitting
delta(1) = -(ql(mu,i)/rhoi - qr(mu,i-1)/rhoim)
delta(2) = -(bulki*ql(1,i) - bulkim*qr(1,i-1))

beta1 = (zi*delta(1) + delta(2)) / (zi+zim)
beta2 = (zim*delta(1) - delta(2)) / (zi+zim)

! # Compute the waves. Eigenvectors switched for adjoint:

fwave(1,1,i) = beta1
fwave(mu,1,i) = beta1*zim
fwave(mv,1,i) = 0.d0
s(1,i) = -cim

fwave(1,2,i) = beta2
fwave(mu,2,i) = -beta2*zi
fwave(mv,2,i) = 0.d0
s(2,i) = ci

20 END DO


! # compute the leftgoing and rightgoing flux differences:
! # Note s(i,1) < 0 and s(i,2) > 0.

! # f-wave splitting, so do not multiply by wave speeds:
forall (m=1:meqn, i=2-mbc: mx+mbc)
amdq(m,i) = fwave(m,1,i)
apdq(m,i) = fwave(m,2,i)
end forall

return
end subroutine rpn2
122 changes: 122 additions & 0 deletions paper2_examples/acoustics_2d_ex3/rpn2_vc_acoustics.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
! =====================================================
subroutine rpn2(ixy,maxm,meqn,mwaves,maux,mbc,mx,ql,qr,auxl,auxr,wave,s,amdq,apdq)
! =====================================================

! Riemann solver for the acoustics equations in 2d, with varying
! material properties rho and kappa

! waves: 2
! equations: 3
! aux fields: 2

! Conserved quantities:
! 1 pressure
! 2 x_momentum
! 3 y_momentum

! Auxiliary variables:
! 1 density
! 2 sound_speed

! Note that although there are 3 eigenvectors, the second eigenvalue
! is always zero and so we only need to compute 2 waves.
!
! solve Riemann problems along one slice of data.

! On input, ql contains the state vector at the left edge of each cell
! qr contains the state vector at the right edge of each cell

! Here it is assumed that auxl=auxr gives the cell values.

! On output, wave contains the waves,
! s the speeds,
! amdq the left-going flux difference A^- \Delta q
! apdq the right-going flux difference A^+ \Delta q

! This data is along a slice in the x-direction if ixy=1
! or the y-direction if ixy=2.

! Note that the i'th Riemann problem has left state qr(:,i-1)
! and right state ql(:,i)
! From the basic clawpack routines, this routine is called with ql = qr


implicit double precision (a-h,o-z)

dimension wave(meqn, mwaves, 1-mbc:maxm+mbc)
dimension s(mwaves, 1-mbc:maxm+mbc)
dimension ql(meqn, 1-mbc:maxm+mbc)
dimension qr(meqn, 1-mbc:maxm+mbc)
dimension apdq(meqn, 1-mbc:maxm+mbc)
dimension amdq(meqn, 1-mbc:maxm+mbc)
dimension auxl(maux, 1-mbc:maxm+mbc)
dimension auxr(maux, 1-mbc:maxm+mbc)

! local arrays
! ------------
dimension delta(3)

! # set mu to point to the component of the system that corresponds
! # to velocity in the direction of this slice, mv to the orthogonal
! # velocity.


if (ixy == 1) then
mu = 2
mv = 3
else
mu = 3
mv = 2
endif

! # note that notation for u and v reflects assumption that the
! # Riemann problems are in the x-direction with u in the normal
! # direciton and v in the orthogonal direcion, but with the above
! # definitions of mu and mv the routine also works with ixy=2


! # split the jump in q at each interface into waves
! # The jump is split into a leftgoing wave traveling at speed -c
! # relative to the material properties to the left of the interface,
! # and a rightgoing wave traveling at speed +c
! # relative to the material properties to the right of the interface,

! # find a1 and a2, the coefficients of the 2 eigenvectors:
do 20 i = 2-mbc, mx+mbc
delta(1) = ql(1,i) - qr(1,i-1)
delta(2) = ql(mu,i) - qr(mu,i-1)
! # impedances:
zi = auxl(1,i)*auxl(2,i)
zim = auxr(1,i-1)*auxr(2,i-1)

a1 = (-delta(1) + zi*delta(2)) / (zim + zi)
a2 = (delta(1) + zim*delta(2)) / (zim + zi)


! # Compute the waves.

wave(1,1,i) = -a1*zim
wave(mu,1,i) = a1
wave(mv,1,i) = 0.d0
s(1,i) = -auxr(2,i-1)

wave(1,2,i) = a2*zi
wave(mu,2,i) = a2
wave(mv,2,i) = 0.d0
s(2,i) = auxl(2,i)

20 END DO



! # compute the leftgoing and rightgoing flux differences:
! # Note s(1,i) < 0 and s(2,i) > 0.

do 220 m=1,meqn
do 220 i = 2-mbc, mx+mbc
amdq(m,i) = s(1,i)*wave(m,1,i)
apdq(m,i) = s(2,i)*wave(m,2,i)
220 END DO

return
end subroutine rpn2
5 changes: 3 additions & 2 deletions paper2_examples/acoustics_2d_ex3/setaux.f
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ subroutine setaux(mbc,mx,my,xlower,ylower,
c
c
use amr_module, only : NEEDS_TO_BE_SET
use adjoint_module, only: innerprod_index
use adjoint_module, only: innerprod_index, adjoint_flagging

implicit double precision (a-h,o-z)
double precision aux(maux,1-mbc:mx+mbc,1-mbc:my+mbc)
@@ -22,7 +22,8 @@ subroutine setaux(mbc,mx,my,xlower,ylower,
do i = 1-mbc,mx+mbc
xcell = xlower + (i-0.5d0)*dx

if (aux(1,i,j) .eq. NEEDS_TO_BE_SET) then
if (adjoint_flagging .and.
& (aux(1,i,j) .eq. NEEDS_TO_BE_SET)) then
aux(innerprod_index,i,j) = 0.d0
endif

8 changes: 5 additions & 3 deletions paper2_examples/acoustics_2d_ex3/setplot.py
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ def setplot(plotdata):
from clawpack.visclaw import colormaps

plotdata.clearfigures() # clear any old figures,axes,items data
plotdata.format = 'binary' # 'ascii', 'binary', 'netcdf'
plotdata.format = 'ascii' # 'ascii', 'binary', 'netcdf'


# Figure for pressure
@@ -42,7 +42,7 @@ def setplot(plotdata):
plotitem = plotaxes.new_plotitem(plot_type='2d_pcolor')
plotitem.plot_var = 0
plotitem.pcolor_cmap = colormaps.blue_white_red
plotitem.add_colorbar = False
plotitem.add_colorbar = True
plotitem.show = True # show on plot?
plotitem.pcolor_cmin = -0.3
plotitem.pcolor_cmax = 0.3
@@ -54,6 +54,7 @@ def setplot(plotdata):
#-----------------------------------------
plotfigure = plotdata.new_plotfigure(name='Inner Product', figno=1)
plotfigure.kwargs = {'figsize': (5.5,4)}
#plotfigure.show = False

# Set up for axes in this figure:
plotaxes = plotfigure.new_plotaxes()
@@ -72,7 +73,7 @@ def setplot(plotdata):
plotitem.show = True # show on plot?
plotitem.pcolor_cmin = 0.0 # use when plotting inner product with q
#plotitem.pcolor_cmin = 0.0 # use when plotting inner product with error
plotitem.pcolor_cmax = 0.01 # use when plotting inner product with q
plotitem.pcolor_cmax = 0.0001 # use when plotting inner product with q
#plotitem.pcolor_cmax = 0.00001 # use when plotting inner product with error
plotitem.amr_patchedges_show = [0,0,0]
plotitem.amr_celledges_show = [0,0,0]
@@ -132,6 +133,7 @@ def setplot(plotdata):
plotdata.latex_figsperline = 2 # layout of plots
plotdata.latex_framesperline = 1 # layout of plots
plotdata.latex_makepdf = False # also run pdflatex?
plotdata.parallel = True # make frames in parallel with omp?

return plotdata

118 changes: 35 additions & 83 deletions paper2_examples/acoustics_2d_ex3/setrun.py
Original file line number Diff line number Diff line change
@@ -9,32 +9,16 @@
import os
import numpy as np

#-----------------------------------------------
# Set these parameters for adjoint flagging....

# location of output from computing adjoint:
adjoint_output = os.path.abspath('adjoint/_output')
print('Will flag using adjoint solution from %s' % adjoint_output)

# Time period of interest:
t1 = 20.5
t2 = 21.

# Determining type of adjoint flagging:
# This version is set up for adjoint flagging
#-----------------------------------------------

# taking inner product with forward solution or Richardson error:
flag_forward_adjoint = True
flag_richardson_adjoint = False

# tolerance for adjoint flagging:
adjoint_flag_tolerance = 3e-4 # suggested if using forward solution
#adjoint_flag_tolerance = 3e-3 # suggested if using Richardson error
#-----------------------------------------------

#------------------------------
def setrun(claw_pkg='amrclaw'):
#------------------------------

"""
Define the parameters used for running Clawpack.
@@ -102,7 +86,8 @@ def setrun(claw_pkg='amrclaw'):
clawdata.num_eqn = 3

# Number of auxiliary variables in the aux array (initialized in setaux)
# see setadjoint
rundata.clawdata.num_aux = 2
# Note: as required for original problem - modified below for adjoint

# Index of aux array corresponding to capacity function, if there is one:
clawdata.capa_index = 0
@@ -147,12 +132,12 @@ def setrun(claw_pkg='amrclaw'):

elif clawdata.output_style == 3:
# Output every step_interval timesteps over total_steps timesteps:
clawdata.output_step_interval = 2
clawdata.total_steps = 4
clawdata.output_step_interval = 1
clawdata.total_steps = 10
clawdata.output_t0 = True # output at initial (or restart) time?


clawdata.output_format = 'binary' # 'ascii', 'binary', 'netcdf'
clawdata.output_format = 'ascii' # 'ascii', 'binary', 'netcdf'

clawdata.output_q_components = 'all' # could be list such as [True,True]
clawdata.output_aux_components = 'all' # could be list
@@ -308,17 +293,22 @@ def setrun(claw_pkg='amrclaw'):
# This must be a list of length num_aux, each element of which is one of:
# 'center', 'capacity', 'xleft', or 'yleft' (see documentation).

rundata.amrdata.aux_type = ['center','center']
# Note: as required for original problem - modified below for adjoint

# set tolerances appropriate for adjoint flagging:

# need 1 value, set in setadjoint


# Flag for refinement based on Richardson error estimater:
amrdata.flag_richardson = False

amrdata.flag_richardson = True
amrdata.flag_richardson_tol = 3e-3 # suggested if using adjoint error flag

# Flag for refinement using routine flag2refine:
amrdata.flag2refine = False
rundata.amrdata.flag2refine_tol = 3e-4 # suggested if using adj mag flag

# see setadjoint to set tolerance for adjoint flagging

# steps to take on each level L between regriddings of level L+1:
amrdata.regrid_interval = 2

@@ -340,13 +330,29 @@ def setrun(claw_pkg='amrclaw'):
rundata.regiondata.regions = []
# to specify regions of refinement append lines of the form
# [minlevel,maxlevel,t1,t2,x1,x2,y1,y2]

#------------------------------------------------------------------
# Adjoint specific data:
#------------------------------------------------------------------
rundata = setadjoint(rundata)
# Also need to set flagging method and appropriate tolerances above

adjointdata = rundata.adjointdata
adjointdata.use_adjoint = True

# location of adjoint solution, must first be created:
adjointdata.adjoint_outdir = os.path.abspath('adjoint/_output')

# time period of interest:
adjointdata.t1 = 20.5
adjointdata.t2 = 21.

if adjointdata.use_adjoint:
# need an additional aux variable for inner product:
rundata.amrdata.aux_type.append('center')
rundata.clawdata.num_aux = len(rundata.amrdata.aux_type)
adjointdata.innerprod_index = len(rundata.amrdata.aux_type)


# ----- For developers -----
# Toggle debugging print statements:
amrdata.dprint = False # print domain flags
@@ -365,60 +371,6 @@ def setrun(claw_pkg='amrclaw'):
# end of function setrun
# ----------------------

#-------------------
def setadjoint(rundata):
#-------------------

"""
Setting up adjoint variables and
reading in all of the checkpointed Adjoint files
"""

import glob


# Set these parameters at top of this file:
# adjoint_flag_tolerance, t1, t2, adjoint_output
# Then you don't need to modify this function...

# flag and tolerance for adjoint flagging:
if flag_forward_adjoint == True:
# setting up taking inner product with forward solution
rundata.amrdata.flag2refine = True
rundata.amrdata.flag2refine_tol = adjoint_flag_tolerance
elif flag_richardson_adjoint == True:
# setting up taking inner product with Richardson error
rundata.amrdata.flag_richardson = True
rundata.amrdata.flag_richardson_tol = adjoint_flag_tolerance
else:
print("No refinement flag set!")

rundata.clawdata.num_aux = 3 # 3 required for adjoint flagging
rundata.amrdata.aux_type = ['center','center','center']

adjointdata = rundata.new_UserData(name='adjointdata',fname='adjoint.data')
adjointdata.add_param('adjoint_output',adjoint_output,'adjoint_output')
adjointdata.add_param('t1',t1,'t1, start time of interest')
adjointdata.add_param('t2',t2,'t2, final time of interest')

files = glob.glob(os.path.join(adjoint_output,"fort.b*"))
files.sort()

if (len(files) == 0):
print("No binary files found for adjoint output!")

adjointdata.add_param('numadjoints', len(files), 'Number of adjoint checkpoint files.')
adjointdata.add_param('innerprod_index', 3, 'Index for innerproduct data in aux array.')

counter = 1
for fname in files:
f = open(fname)
adjointdata.add_param('file' + str(counter), fname, 'Binary file' + str(counter))
counter = counter + 1

return rundata
# end of function setadjoint
# ----------------------

if __name__ == '__main__':
# Set up run-time parameters and write all data files.