forked from trixi-framework/Trixi.jl
-
Couldn't load subscription status.
- Fork 1
Elixir: ImplicitEuler solve with sparse jacobian #78
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
sbairos
wants to merge
132
commits into
DanielDoehring:main
Choose a base branch
from
sbairos:elixir_implicit_sparse
base: main
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
Changes from 4 commits
Commits
Show all changes
132 commits
Select commit
Hold shift + click to select a range
e0f73ed
Elixir for implicit solver using sparse jacobian
9dbe05c
Docstring for new semidiscretize
6adff66
PR comments
097e994
Making the hacks into an extension
2e7cebf
Apply suggestions from code review
DanielDoehring 2be59f8
work
DanielDoehring 254a79f
packages
DanielDoehring 9089e6a
reduce time
DanielDoehring 89a7b92
work
DanielDoehring 524ae9a
use sparse ode
DanielDoehring c984c02
comment
DanielDoehring 0dd324d
be more explicit
DanielDoehring 1f5a40b
try nonlinear
DanielDoehring 46b2d92
comment
DanielDoehring ca5f951
comment
DanielDoehring 7bdfc8d
exchange solver
DanielDoehring 2f9d246
work
DanielDoehring c1ebd50
comment
DanielDoehring 7d141cf
comments
DanielDoehring bb6b7d5
Merge branch 'main' into SteveSparseDiff
DanielDoehring 4e163fe
comment
DanielDoehring dad7fe7
rename
DanielDoehring b5db145
comments
DanielDoehring 3ce1a4c
Moving the ext back into the elixirs
a65537c
Adding CI test
27bc102
Adding CI test for implicit_sparse_jacobian elixir
d956961
Update examples/tree_1d_dgsem/elixir_euler_convergence_implicit_spars…
DanielDoehring 4b1dbdd
Update examples/tree_1d_dgsem/elixir_euler_convergence_implicit_spars…
DanielDoehring cc3fa6a
Apply suggestions from code review
DanielDoehring 6e9a0d0
Update examples/tree_2d_dgsem/elixir_advection_implicit_sparse_jacobi…
DanielDoehring 6344c66
comments on constant jacs
DanielDoehring 63c9214
move dir
DanielDoehring 691cedf
comment
DanielDoehring c5f5fcf
order
DanielDoehring 5586491
restart file
DanielDoehring eb6aa90
Apply suggestions from code review
DanielDoehring e851808
compat
DanielDoehring 82caac1
Merge branch 'elixir_implicit_sparse' of github.com:sbairos/Trixi.jl …
DanielDoehring 9d97bcc
remove redundant comments
DanielDoehring 2ed4e38
Elixir for implicit solver using sparse jacobian
7bd5f32
Docstring for new semidiscretize
238cadb
PR comments
8a4ce20
Making the hacks into an extension
d12dca7
Apply suggestions from code review
DanielDoehring 6b3a900
work
DanielDoehring ef31890
packages
DanielDoehring d65fadb
reduce time
DanielDoehring 8289379
work
DanielDoehring 61e8f13
use sparse ode
DanielDoehring 6e3d6ea
comment
DanielDoehring d02e4d1
be more explicit
DanielDoehring d024182
try nonlinear
DanielDoehring 4041aab
comment
DanielDoehring b5f7e10
comment
DanielDoehring 80b3d53
exchange solver
DanielDoehring 979a66b
work
DanielDoehring 914e1dd
comment
DanielDoehring 9cfdbd0
comments
DanielDoehring 0fa5381
comment
DanielDoehring 987c1ea
rename
DanielDoehring 8c7a425
comments
DanielDoehring b526312
Moving the ext back into the elixirs
51a5b77
Adding CI test
9f713b9
Adding CI test for implicit_sparse_jacobian elixir
4ca244b
CI tests for both
6909159
Merge branch 'workwork' into elixir_implicit_sparse
4467be4
Other 2 CI tests
5bb92d6
Uncomment commented tests and delete 1D convergence_implicit elixir
0cc77c0
Replace the test that I accidentally overwrote
a829d5c
Unit test
c303210
Update test/test_unit.jl
DanielDoehring 84a5be8
Update Project.toml
DanielDoehring 747768e
Update examples/tree_2d_dgsem/elixir_advection_implicit_sparse_jacobi…
DanielDoehring 9a164a8
Update examples/tree_2d_dgsem/elixir_advection_implicit_sparse_jacobi…
DanielDoehring 03ac768
revisit
DanielDoehring b0e9083
unit test
DanielDoehring 5c33791
repeat comment
DanielDoehring 5e7e7b1
linear structure
DanielDoehring 9137af2
fmzt
DanielDoehring 5ae5c72
fix unit test
DanielDoehring 026bc8a
Apply suggestions from code review
DanielDoehring 6e8115c
Spellcheck
2a894fe
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring a147c1c
Update examples/tree_2d_dgsem/elixir_advection_implicit_sparse_jacobi…
DanielDoehring 5719d0e
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring 61e4d47
comment
DanielDoehring 122a46e
add sparse arrays
DanielDoehring 47a7a5b
try module
DanielDoehring ea8cf44
rm
DanielDoehring b9a54d2
try more specific overload
DanielDoehring 476b174
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring 309c730
simplify
DanielDoehring 6d6a497
v
DanielDoehring 36ed9b9
v
DanielDoehring 07589c2
Update examples/structured_2d_dgsem/elixir_euler_convergence_implicit…
DanielDoehring 70b3861
Update examples/structured_2d_dgsem/elixir_euler_convergence_implicit…
DanielDoehring f479c92
Update examples/structured_2d_dgsem/elixir_euler_convergence_implicit…
DanielDoehring 8478589
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring aba1e36
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring 25518ef
Update examples/tree_2d_dgsem/elixir_advection_implicit_sparse_jacobi…
DanielDoehring 6ea38f2
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring 5153dab
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring f2f3780
correct comment
DanielDoehring 613f01a
Merge branch 'elixir_implicit_sparse' of github.com:sbairos/Trixi.jl …
DanielDoehring 1b241ae
simplify
DanielDoehring 25b54b5
kwargs
DanielDoehring c026cf9
adapt
DanielDoehring 0f3f00c
shorten
DanielDoehring 9bc0f6c
Update examples/tree_2d_dgsem/elixir_advection_implicit_sparse_jacobi…
DanielDoehring cde1bff
fmt
DanielDoehring eb29838
simplify
DanielDoehring a72dce2
try different warning
DanielDoehring 2c9eb34
warning
DanielDoehring d9e4b51
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring f99ac51
test
DanielDoehring 5c5722f
Merge branch 'elixir_implicit_sparse' of github.com:sbairos/Trixi.jl …
DanielDoehring 8542b3e
up
DanielDoehring 43900a4
revert
DanielDoehring 9d04c00
move comment
DanielDoehring 03c6b7b
compat
DanielDoehring 7824a97
v
DanielDoehring 5357ec3
v
DanielDoehring a16419a
v
DanielDoehring a4f3af4
try plots
DanielDoehring 6991c6e
v
DanielDoehring 63a415e
Update test/Project.toml
DanielDoehring d21a0e2
module replace
DanielDoehring 151bc57
Merge branch 'elixir_implicit_sparse' of github.com:sbairos/Trixi.jl …
DanielDoehring 72ac79f
typo
DanielDoehring b04e814
Merge branch 'main' into elixir_implicit_sparse
DanielDoehring 76754b5
error tol
DanielDoehring c815383
Merge branch 'elixir_implicit_sparse' of github.com:sbairos/Trixi.jl …
DanielDoehring 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
78 changes: 78 additions & 0 deletions
78
examples/tree_2d_dgsem/elixir_advection_sparse_jacobian.jl
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,78 @@ | ||
| using Trixi | ||
| using OrdinaryDiffEq | ||
| using SparseDiffTools, Symbolics | ||
|
|
||
| using .TrixiSparseDiffToolsExt | ||
|
|
||
| ############################################################################### | ||
| ### semidiscretization of the linear advection equation ### | ||
|
|
||
| advection_velocities = (1.0, 1.1) | ||
| equations = LinearScalarAdvectionEquation2D(advection_velocities) | ||
|
|
||
| # Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux | ||
| d = 3 | ||
| # `RealT = Real` requires fewer overloads than the more explicit `RealT = Num` | ||
| # solver_real used for computing the Jacobian | ||
| solver_real = DGSEM(polydeg = d, surface_flux = flux_lax_friedrichs, RealT = Real) | ||
| # solver_float used for solving using the Jacobian | ||
| solver_float = DGSEM(polydeg = d, surface_flux = flux_lax_friedrichs) | ||
DanielDoehring marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| coordinates_min = (-1.0, -1.0) # minimum coordinates (min(x), min(y)) | ||
| coordinates_max = (1.0, 1.0) # maximum coordinates (max(x), max(y)) | ||
|
|
||
| # Create a uniformly refined mesh with periodic boundaries | ||
| RefinementLevel = 5 | ||
| mesh = TreeMesh(coordinates_min, coordinates_max, | ||
| initial_refinement_level = RefinementLevel, | ||
| n_cells_max = 30_000) # set maximum capacity of tree data structure | ||
|
|
||
| # A semidiscretization collects data structures and functions for the spatial discretization | ||
| # semi_real used for computing the Jacobian | ||
| semi_real = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergence_test, | ||
| solver_real) | ||
| # semi_float used for solving using the Jacobian | ||
| semi_float = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergence_test, | ||
| solver_float) | ||
|
|
||
| # 2^RefinementLevel = 16 elements, (d+1) local polynomial coefficients per element | ||
| N = Int(2^RefinementLevel * (d+1)) #64 | ||
| u0_ode = zeros(N*N) | ||
| du_ode = 80 * ones(N*N) # initialize with something | ||
| t0 = 0.0 | ||
| tSpan = (t0, t0 + 10.0) | ||
|
|
||
| ############################################################################### | ||
| ### Compute the Jacobian with SparseDiffTools ### | ||
|
|
||
| # Create a function with two parameters:du_ode and u0_ode | ||
| # to fulfill the requirments of an in_place function in SparseDiffTools | ||
| rhs = (du_ode,u0_ode)->Trixi.rhs!(du_ode, u0_ode, semi_real, t0) | ||
|
|
||
| #From the example to detect the pattern and choose how to do the AutoDiff automatically | ||
| sd = SymbolicsSparsityDetection() | ||
| adtype = AutoSparseFiniteDiff() | ||
|
|
||
| #From the example provided in SparseDiffTools. cache will reduce calculation time when Jacobian will be calculated multiple times | ||
| sparse_cache = sparse_jacobian_cache(adtype, sd, rhs, du_ode, u0_ode) | ||
|
|
||
| ############################################################################### | ||
| # callback functions during the time integration | ||
|
|
||
| ode = semidiscretize(semi_float, tSpan, sparse_cache.jac_prototype, sparse_cache.coloring.colorvec) | ||
|
|
||
| # The AnalysisCallback allows to analyse the solution in regular intervals and prints the results | ||
| analysis_callback = AnalysisCallback(semi_float, interval = 100) | ||
|
|
||
| # At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup | ||
| # and resets the timers | ||
| summary_callback = SummaryCallback() | ||
|
|
||
| # Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver | ||
| callbacks = CallbackSet(summary_callback) | ||
|
|
||
| ############################################################################### | ||
| # Run the simulation using ImplicitEuler method | ||
|
|
||
| sol = solve(ode, ImplicitEuler(; autodiff = AutoFiniteDiff()); | ||
| dt = 1.0, save_everystep = false, callback = callbacks); | ||
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,43 @@ | ||
| # Package extension for using SparseDiffTools with Trixi for implicit solvers | ||
| module TrixiSparseDiffToolsExt | ||
|
|
||
| using Trixi | ||
|
|
||
| import Base: *, zero, one # For overloading | ||
DanielDoehring marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ############################################################################### | ||
| ### Hacks ### | ||
|
|
||
| # Required for setting up the Lobatto Legendre basis for abstract `Real` type | ||
| function Trixi.eps(::Type{Real}, RealT = Float64) | ||
| return eps(RealT) | ||
| end | ||
|
|
||
| # There are several places in trixi where they do one(RealT) or zero(uEltype) where RealT or uEltype is Real | ||
| # this just returns an Int64 1 or 0 respectively. We don't want to use ints so we override this behavior | ||
| # Real(x::Real) = Float64(x) | ||
| Base.one(::Type{Real}) = 1.0 | ||
| Base.zero(::Type{Real}) = 0.0 | ||
|
|
||
| # Multiplying two Matrix{Real}s gives a Matrix{Any}. | ||
| # This causes problems when instantiating the Legendre basis. | ||
| # Called in `calc_{forward,reverse}_{upper, lower}` | ||
DanielDoehring marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| function *(A::Matrix{Real}, B::Matrix{Real})::Matrix{Real} | ||
| m, n = size(A, 1), size(B, 2) | ||
| kA = size(A, 2) | ||
| kB = size(B, 1) | ||
| @assert kA == kB "Matrix dimensions must match for multiplication" | ||
|
|
||
| C = Matrix{Real}(undef, m, n) | ||
| for i in 1:m, j in 1:n | ||
| #acc::Real = zero(promote_type(typeof(A[i,1]), typeof(B[1,j]))) | ||
| acc = zero(Real) | ||
| for k in 1:kA | ||
| acc += A[i,k] * B[k,j] | ||
| end | ||
| C[i,j] = acc | ||
| end | ||
| return C | ||
| end | ||
|
|
||
| end # module TrixiNLsolveExt | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.