-
Notifications
You must be signed in to change notification settings - Fork 16
TimeProjector #154
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
Open
pbrubeck
wants to merge
30
commits into
master
Choose a base branch
from
pbrubeck/time-projector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
TimeProjector #154
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
bab0a4f
Allow labeled forms with different quadrature degrees in galerkin
rckirby 9bf2319
aux_indices
pbrubeck 7edacdf
Galerkin: set constant-in-time initial guess
pbrubeck 004e3f5
Conservation of all invariants
pbrubeck 8f64452
mesh constant?
rckirby b08938b
Merge branch 'rckirby/galerkin_label' of github.com:firedrakeproject/…
rckirby 094927a
Fix tests
pbrubeck e545992
Merge branch 'rckirby/galerkin_label' of github.com:firedrakeproject/…
pbrubeck eed8d45
lint
pbrubeck ad23e4d
Refactor auxiliary variables
pbrubeck 2329191
TimeProjector
pbrubeck e9c3849
temp fix (?) on BC for Galerkin; WIP demo on structure preservation
rckirby e6c7750
TimeProjector(Expr)
pbrubeck 170ed04
Merge branch 'pbrubeck/time-projector' of github.com:firedrakeproject…
pbrubeck dee3e7f
Add Labels
pbrubeck eaa1474
minor change
pbrubeck 3a70634
fix quadrature degree
rckirby 3e85826
NSE + auxiliary variables
pbrubeck 569a4ea
Galerkin solve for an additive update
pbrubeck 0544bba
Cleanup test
pbrubeck c63a366
Eliminate w1 only
pbrubeck 384997c
Add more axiliary variables
pbrubeck 6e47125
merge master
pbrubeck 1d28466
Apply suggestions from code review
pbrubeck 471c55e
Update irksome/galerkin_stepper.py
pbrubeck 540d8ca
Fix MeshSequence, fix shape
pbrubeck 2ee730f
merge conflicts
pbrubeck c24081b
fixes
pbrubeck a0f06e6
add time_projector.py
pbrubeck 5892ea2
Merge branch 'master' into pbrubeck/time-projector
pbrubeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| from firedrake import * | ||
| from irksome import Dt, GalerkinTimeStepper | ||
| from irksome.galerkin_stepper import TimeProjector | ||
| from irksome.labeling import TimeQuadratureLabel | ||
| from FIAT import ufc_simplex | ||
| from FIAT.quadrature_schemes import create_quadrature | ||
| from scipy import special | ||
| from firedrake.petsc import PETSc | ||
|
|
||
| print = PETSc.Sys.Print | ||
|
|
||
| ufcline = ufc_simplex(1) | ||
|
|
||
| ''' | ||
| Thanks to Boris Andrews for providing the Hill vortex functions | ||
| ''' | ||
| ''' | ||
| Hill vortex functions | ||
| ''' | ||
| # Bessel function parameters | ||
| bessel_J_root = 5.7634591968945506 | ||
| bessel_J_root_threehalves = bessel_J(3/2, bessel_J_root) | ||
|
|
||
|
|
||
| # (r, theta, phi) components of Hill vortex | ||
| def hill_r(r, theta, radius): | ||
| rho = r / radius | ||
| return 2 * ( | ||
| bessel_J(3/2, bessel_J_root*rho) / rho**(3/2) | ||
| - bessel_J_root_threehalves | ||
| ) * cos(theta) | ||
|
|
||
|
|
||
| def hill_theta(r, theta, radius): | ||
| rho = r / radius | ||
| return ( | ||
| bessel_J_root * bessel_J(5/2, bessel_J_root*rho) / rho**(1/2) | ||
| + 2 * bessel_J_root_threehalves | ||
| - 2 * bessel_J(3/2, bessel_J_root*rho) / rho**(3/2) | ||
| ) * sin(theta) | ||
|
|
||
|
|
||
| def hill_phi(r, theta, radius): | ||
| rho = r / radius | ||
| return bessel_J_root * ( | ||
| bessel_J(3/2, bessel_J_root*rho) / rho**(3/2) | ||
| - bessel_J_root_threehalves | ||
| ) * rho * sin(theta) | ||
|
|
||
|
|
||
| # Hill vortex (Cartesian) | ||
| def hill(vec, radius): | ||
| (x, y, z) = vec | ||
| rho = sqrt(x*x + y*y) | ||
|
|
||
| r = sqrt(dot(vec, vec)) | ||
| theta = pi/2-atan2(z, rho) | ||
| phi = atan2(y, x) | ||
|
|
||
| r_dir = as_vector([cos(phi)*sin(theta), sin(phi)*sin(theta), cos(theta)]) | ||
| theta_dir = as_vector([cos(phi)*cos(theta), sin(phi)*cos(theta), -sin(theta)]) | ||
| phi_dir = as_vector([-sin(phi), cos(phi), 0]) | ||
|
|
||
| return conditional( # If we're outside the vortex... | ||
| ge(r, radius), | ||
| zero((3,)), | ||
| conditional( # If we're at the origin... | ||
| le(r, 1e-13), | ||
| as_vector([0, 0, 2*((bessel_J_root/2)**(3/2)/special.gamma(5/2) - bessel_J_root_threehalves)]), | ||
| (hill_r(r, theta, radius) * r_dir | ||
| + hill_theta(r, theta, radius) * theta_dir | ||
| + hill_phi(r, theta, radius) * phi_dir) | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def stokes_pair(msh): | ||
| V = VectorFunctionSpace(msh, "CG", 2) | ||
| Q = FunctionSpace(msh, "CG", 1) | ||
| return V, Q | ||
|
|
||
|
|
||
| def nse_naive(msh, order, t, dt, Re, solver_parameters=None): | ||
| hill_expr = hill(SpatialCoordinate(msh), 0.25) | ||
| V, Q = stokes_pair(msh) | ||
| Z = V * Q | ||
| up = Function(Z) | ||
| up.subfunctions[0].interpolate(hill_expr) | ||
| # VTKFile("output/hill.pvd").write(up.subfunctions[0]) | ||
|
|
||
| v, q = TestFunctions(Z) | ||
| u, p = split(up) | ||
|
|
||
| Qhigh = create_quadrature(ufcline, 3*order-1) | ||
| Qlow = create_quadrature(ufcline, 2*(order-1)) | ||
| Lhigh = TimeQuadratureLabel(Qhigh.get_points(), Qhigh.get_weights()) | ||
| Llow = TimeQuadratureLabel(Qlow.get_points(), Qlow.get_weights()) | ||
|
|
||
| F = (Llow(inner(Dt(u), v) * dx) | ||
| + Lhigh(-inner(cross(u, curl(u)), v) * dx) | ||
| + 1/Re * inner(grad(u), grad(v)) * dx | ||
| - inner(p, div(v)) * dx | ||
| - inner(div(u), q) * dx) | ||
|
|
||
| bcs = DirichletBC(Z.sub(0), 0, "on_boundary") | ||
|
|
||
| stepper = GalerkinTimeStepper(F, order, t, dt, up, bcs=bcs) | ||
| Q1 = inner(u, u) * dx | ||
| Q2 = inner(u, curl(u)) * dx | ||
| invariants = [Q1, Q2] | ||
| return stepper, invariants | ||
|
|
||
|
|
||
| def nse_aux_variable(msh, order, t, dt, Re, solver_parameters=None): | ||
| hill_expr = hill(SpatialCoordinate(msh), 0.25) | ||
| V, Q = stokes_pair(msh) | ||
| Z = V * V * V * Q * Q | ||
| up = Function(Z) | ||
| up.subfunctions[0].interpolate(hill_expr) | ||
|
|
||
| aux_indices = (1, 2, 4) | ||
| v, v1, v2, q, q1 = TestFunctions(Z) | ||
| u, w1, w2, p, r1 = split(up) | ||
|
|
||
| Qhigh = create_quadrature(ufcline, 3*(order-1)) | ||
| Qlow = create_quadrature(ufcline, 2*(order-1)) | ||
| Lhigh = TimeQuadratureLabel(Qhigh.get_points(), Qhigh.get_weights()) | ||
| Llow = TimeQuadratureLabel(Qlow.get_points(), Qlow.get_weights()) | ||
|
|
||
| F = (Llow(inner(Dt(u), v) * dx) | ||
| + Lhigh(-inner(cross(w1, w2), v) * dx) | ||
| + Llow(1/Re * inner(grad(w1), grad(v)) * dx) | ||
| - inner(p, div(v)) * dx | ||
| - inner(div(u), q) * dx | ||
| + inner(w1 - u, v1) * dx | ||
| + inner(w2 - curl(u), v2) * dx | ||
| - inner(r1, div(v2)) * dx | ||
| - inner(div(w2), q1) * dx | ||
| ) | ||
|
|
||
| bcs = [DirichletBC(Z.sub(i), 0, "on_boundary") for i in range(3)] | ||
|
|
||
| stepper = GalerkinTimeStepper(F, order, t, dt, up, bcs=bcs, aux_indices=aux_indices) | ||
| Q1 = inner(u, u) * dx | ||
| Q2 = inner(u, curl(u)) * dx | ||
| invariants = [Q1, Q2] | ||
| return stepper, invariants | ||
|
|
||
|
|
||
| def nse_project(msh, order, t, dt, Re, solver_parameters=None): | ||
| hill_expr = hill(SpatialCoordinate(msh), 0.25) | ||
| V, Q = stokes_pair(msh) | ||
| Z = V * V * Q * Q | ||
| up = Function(Z) | ||
| up.subfunctions[0].interpolate(hill_expr) | ||
|
|
||
| aux_indices = (1, 3) | ||
| v, v2, q, q1 = TestFunctions(Z) | ||
| u, w2, p, r1 = split(up) | ||
|
|
||
| Qhigh = create_quadrature(ufcline, 3*(order-1)) | ||
| Qlow = create_quadrature(ufcline, 2*(order-1)) | ||
| Lhigh = TimeQuadratureLabel(Qhigh.get_points(), Qhigh.get_weights()) | ||
| Llow = TimeQuadratureLabel(Qlow.get_points(), Qlow.get_weights()) | ||
|
|
||
| # Eliminate w1 only | ||
| Qproj = create_quadrature(ufcline, 2*order-1) | ||
| w1 = TimeProjector(u, order-1, Qproj) | ||
|
|
||
| F = (Llow(inner(Dt(u), v) * dx) | ||
| + Lhigh(-inner(cross(w1, w2), v) * dx) | ||
| + Llow(1/Re * inner(grad(w1), grad(v)) * dx) | ||
| - inner(p, div(v)) * dx | ||
| - inner(div(u), q) * dx | ||
| + inner(w2 - curl(u), v2)*dx | ||
| - inner(r1, div(v2)) * dx | ||
| - inner(div(w2), q1) * dx | ||
| ) | ||
|
|
||
| bcs = [DirichletBC(Z.sub(i), 0, "on_boundary") for i in range(2)] | ||
|
|
||
| stepper = GalerkinTimeStepper(F, order, t, dt, up, bcs=bcs, aux_indices=aux_indices) | ||
| Q1 = inner(u, u) * dx | ||
| Q2 = inner(u, curl(u)) * dx | ||
| invariants = [Q1, Q2] | ||
| return stepper, invariants | ||
|
|
||
|
|
||
| def run_nse(stepper, invariants): | ||
| t = stepper.t | ||
| dt = stepper.dt | ||
| row = [float(t), *map(assemble, invariants)] | ||
| print(*(f"{r:.8e}" for r in row)) | ||
| # while float(t) < 3 * 2**(-6): | ||
| for k in range(2): | ||
| stepper.advance() | ||
| t.assign(t + dt) | ||
|
|
||
| row = [float(t), *map(assemble, invariants)] | ||
| print(*(f"{r:.8e}" for r in row)) | ||
|
|
||
|
|
||
| order = 2 | ||
| N = 8 | ||
| msh = UnitCubeMesh(N, N, N) | ||
| msh.coordinates.dat.data[:, :] -= 0.5 | ||
|
|
||
| t = Constant(0) | ||
| dt = Constant(2**-10) | ||
| Re = Constant(2**16) | ||
|
|
||
| solvers = { | ||
| "naive": nse_naive, | ||
| "project": nse_project, | ||
| "aux": nse_aux_variable, | ||
| } | ||
|
|
||
| for name, solver in solvers.items(): | ||
| print(name) | ||
| t.assign(0) | ||
| run_nse(*solver(msh, order, t, dt, Re)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment is no longer accurate here.