Skip to content

Commit

Permalink
Clean argparse usage to be compatible with -options_left (new firedra…
Browse files Browse the repository at this point in the history
…ke default).
  • Loading branch information
bueler committed Oct 13, 2023
1 parent 5255299 commit 1d76486
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
17 changes: 10 additions & 7 deletions python/ch13/fish.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/usr/bin/env python3

from argparse import ArgumentParser, RawTextHelpFormatter
from firedrake import *
from firedrake.petsc import PETSc

# Read command-line options (in addition to PETSc solver options
# which use -s_ prefix; see below)
from argparse import ArgumentParser, RawTextHelpFormatter
parser = ArgumentParser(description="""
Use Firedrake's nonlinear solver for the Poisson problem
-Laplace(u) = f in the unit square
Expand All @@ -14,7 +11,7 @@
Use -help for PETSc options and -fishhelp for options to fish.py.""",
formatter_class=RawTextHelpFormatter,add_help=False)
parser.add_argument('-fishhelp', action='store_true', default=False,
help='help for fish.py options')
help='print help for fish.py options and exit')
parser.add_argument('-mx', type=int, default=3, metavar='MX',
help='number of grid points in x-direction')
parser.add_argument('-my', type=int, default=3, metavar='MY',
Expand All @@ -27,9 +24,16 @@
help='use quadrilateral finite elements')
parser.add_argument('-refine', type=int, default=-1, metavar='X',
help='number of refinement levels (e.g. for GMG)')
args, unknown = parser.parse_known_args()
args, passthroughoptions = parser.parse_known_args()
if args.fishhelp: # -fishhelp is for help with fish.py
parser.print_help()
import sys
sys.exit(0)

import petsc4py
petsc4py.init(passthroughoptions)
from firedrake import *
from firedrake.petsc import PETSc

# Create mesh, enabling GMG via refinement using hierarchy
mx, my = args.mx, args.my
Expand Down Expand Up @@ -75,4 +79,3 @@
PETSc.Sys.Print('saving solution to %s ...' % args.o)
u.rename('u')
File(args.o).write(u)

4 changes: 2 additions & 2 deletions python/ch13/output/fish.py.test3
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Linear s_ solve converged due to CONVERGED_RTOL iterations 5
Linear s_ solve converged due to CONVERGED_RTOL iterations 6
Linear s_ solve converged due to CONVERGED_RTOL iterations 5
Nonlinear s_ solve converged due to CONVERGED_FNORM_RELATIVE iterations 2
done on 5 x 5 grid with P_1 elements:
error |u-uexact|_inf = 9.290e-04, |u-uexact|_h = 4.697e-04
error |u-uexact|_inf = 9.286e-04, |u-uexact|_h = 4.696e-04
4 changes: 1 addition & 3 deletions python/ch13/output/fish.py.test4
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
done on 3 x 3 grid with P_1 elements:
error |u-uexact|_inf = 3.365e-03, |u-uexact|_h = 1.190e-03
usage: fish.py [-fishhelp] [-mx MX] [-my MY] [-o NAME] [-k K] [-quad]
[-refine X]

Expand All @@ -10,7 +8,7 @@ Compare c/ch6/fish.c. The prefix for PETSC solver options is 's_'.
Use -help for PETSc options and -fishhelp for options to fish.py.

options:
-fishhelp help for fish.py options
-fishhelp print help for fish.py options and exit
-mx MX number of grid points in x-direction
-my MY number of grid points in y-direction
-o NAME output file name ending with .pvd
Expand Down
9 changes: 6 additions & 3 deletions python/ch14/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ runstokes_1:
-@../../c/testit.sh stokes.py "-analytical -mx 4 -my 4 -s_ksp_type minres -s_ksp_converged_reason -s_pc_type none -s_ksp_rtol 1.0e-2" 1 1

runstokes_2:
-@../../c/testit.sh stokes.py "-udegree 3 -pdegree 2 -refine 1 -quad -showinfo -s_ksp_type preonly -s_pc_type lu -s_pc_factor_shift_type inblocks -s_pmat_type aij -s_ksp_monitor_short" 1 2
-@../../c/testit.sh stokes.py "-udegree 3 -pdegree 2 -refine 1 -quad -showinfo -s_ksp_type preonly -s_pc_type lu -s_pc_factor_shift_type inblocks -s_ksp_monitor_short" 1 2

# following test depends on gmsh version
runstokes_3:
Expand All @@ -26,15 +26,18 @@ runstokes_5:
runstokes_6:
-@../../c/testit.sh stokes.py "-vectorlap -analytical -refine 1 -s_ksp_type gmres -s_ksp_converged_reason -schurgmg lower" 1 6

runstokes_7:
-@../../c/testit.sh stokes.py "-stokeshelp" 1 7

test_gmshversion: rungmshversion_1

test_stokes: runstokes_1 runstokes_2 runstokes_3 runstokes_4 runstokes_5 runstokes_6
test_stokes: runstokes_1 runstokes_2 runstokes_3 runstokes_4 runstokes_5 runstokes_6 runstokes_7

test: test_gmshversion test_stokes

# etc

.PHONY: clean rungmshversion_1 runstokes_1 runstokes_2 runstokes_3 runstokes_4 runstokes_5 runstokes_6 test_stokes test
.PHONY: clean rungmshversion_1 runstokes_1 runstokes_2 runstokes_3 runstokes_4 runstokes_5 runstokes_6 runstokes_7 test_stokes test

clean:
@rm -f *.pyc *.geo *.msh *.pvd *.pvtu *.vtu *.m maketmp tmp difftmp
37 changes: 37 additions & 0 deletions python/ch14/output/stokes.py.test7
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
usage: stokes.py [-analytical] [-dp] [-lidscale X] [-mesh INNAME] [-mx MX]
[-my MY] [-mu MU] [-nobase] [-o OUTNAME] [-pdegree L] [-quad]
[-refine R] [-schurgmg X] [-schurpre X] [-showinfo]
[-stokeshelp] [-udegree K] [-vectorlap]

Solve a linear Stokes problem in 2D, an example of a saddle-point system.
Three problem cases:
1. Lid-driven cavity with quadratic velocity on lid and Dirichlet conditions
on all sides and a null space of constant pressures. The default problem.
2. -analytical: Analytical exact solution from Logg et al (2012).
3. -nobase: Same as 1. but with stress-free condition on bottom so the null
space is trivial.
Uses mixed FE method, either Taylor-Hood family (P^k x P^l, or Q^k x Q^l with
-quad) or CD with discontinuous pressure; default is P^2 x P^1. Uses either
uniform mesh or reads a mesh in Gmsh format. See source code for Schur+GMG
PC packages. The prefix for PETSC solver options is 's_'. Use -help for
PETSc options and -stokeshelp for options to stokes.py.

options:
-analytical Stokes problem with exact solution
-dp use discontinuous-Galerkin finite elements for pressure
-lidscale X scale for lid velocity (rightward positive; default=1.0)
-mesh INNAME input file for mesh in Gmsh format (.msh)
-mx MX number of grid points in x-direction (uniform case)
-my MY number of grid points in y-direction (uniform case)
-mu MU constant dynamic viscosity (default=1.0)
-nobase Stokes problem with stress-free boundary condition on base
-o OUTNAME output file name for Paraview format (.pvd)
-pdegree L polynomial degree for pressure (default=1)
-quad use quadrilateral finite elements
-refine R number of refinement levels (e.g. for GMG)
-schurgmg X Schur+GMG PC solver package: diag|lower|full
-schurpre X how Schur block is preconditioned: selfp|mass
-showinfo print function space sizes and solution norms
-stokeshelp print help for stokes.py options and exit
-udegree K polynomial degree for velocity (default=2)
-vectorlap use vector laplacian residual formula
19 changes: 10 additions & 9 deletions python/ch14/stokes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env python3

import sys
from argparse import ArgumentParser, RawTextHelpFormatter
from firedrake import *
from firedrake.petsc import PETSc

parser = ArgumentParser(description="""
Solve a linear Stokes problem in 2D, an example of a saddle-point system.
Three problem cases:
Expand Down Expand Up @@ -51,17 +47,22 @@
parser.add_argument('-showinfo', action='store_true', default=False,
help='print function space sizes and solution norms')
parser.add_argument('-stokeshelp', action='store_true', default=False,
help='help for stokes.py options')
help='print help for stokes.py options and exit')
parser.add_argument('-udegree', type=int, default=2, metavar='K',
help='polynomial degree for velocity (default=2)')
parser.add_argument('-vectorlap', action='store_true', default=False,
help='use vector laplacian residual formula')
args, unknown = parser.parse_known_args()
args, passthroughoptions = parser.parse_known_args()
assert not (args.analytical and args.nobase), 'conflict in problem choice options'

# -stokeshelp is for help with stokes.py
if args.stokeshelp:
if args.stokeshelp: # -stokeshelp is for help with stokes.py
parser.print_help()
import sys
sys.exit(0)

import petsc4py
petsc4py.init(passthroughoptions)
from firedrake import *
from firedrake.petsc import PETSc

# read Gmsh mesh or create uniform mesh
if len(args.mesh) > 0:
Expand Down

0 comments on commit 1d76486

Please sign in to comment.