From eefab4d7aeb4560f43038b4511214ce7d21ff5f3 Mon Sep 17 00:00:00 2001 From: Alexis Montoison Date: Wed, 6 Nov 2024 12:30:40 -0600 Subject: [PATCH] Improve how the code blocks are displayed --- docs/Project.toml | 1 + docs/src/custom_workspaces.md | 14 +++++++++----- docs/src/matrix_free.md | 4 ++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 7b1ecc496..5af3839f4 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -10,6 +10,7 @@ LDLFactorizations = "40e66cde-538c-5869-a4ad-c39174c6795b" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LinearOperators = "5c8ed15e-5a4c-59e4-a42b-c7e8811fb125" MatrixMarket = "4d4711f2-db25-561a-b6b3-d35e7d4047d3" +OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" SuiteSparseMatrixCollection = "ac199af8-68bc-55b8-82c4-7abd6f96ed98" diff --git a/docs/src/custom_workspaces.md b/docs/src/custom_workspaces.md index 331595b7f..2c08008a9 100644 --- a/docs/src/custom_workspaces.md +++ b/docs/src/custom_workspaces.md @@ -65,7 +65,7 @@ It is parameterized by: - **`FC`**: The element type of the vector. - **`D`**: The data array type, which uses `OffsetArray` to enable custom indexing. -```julia +```@example halo-regions; continued = true using OffsetArrays struct HaloVector{FC, D} <: AbstractVector{FC} @@ -110,7 +110,9 @@ The `size` and `getindex` functions support REPL display, aiding interaction, th Using `HaloVector` with `OffsetArray`, we can apply the discrete Laplacian operator in a matrix-free approach with a 5-point stencil, managing halo regions effectively. This layout allows **clean and efficient Laplacian computation** without boundary checks within the core loop. -```julia +```@example halo-regions; continued = true +using LinearAlgebra + # Define a matrix-free Laplacian operator struct LaplacianOperator Nx::Int # Number of grid points in the x-direction @@ -145,7 +147,7 @@ end To integrate `HaloVector` with Krylov.jl, we define essential vector operations, including dot products, norms, scalar multiplication, and element-wise updates. These implementations allow Krylov.jl to leverage custom vector types, enhancing both solver flexibility and performance. -```julia +```@example halo-regions; continued = true using Krylov import Krylov.FloatOrComplex @@ -252,8 +254,8 @@ Note that `Krylov.kref!` is only required for `minres_qlp`. ### 2D Poisson equation solver with Krylov methods -```julia -using Krylov, LinearAlgebra, OffsetArrays +```@example halo-regions +using Krylov, OffsetArrays # Parameters L = 1.0 # Length of the square domain @@ -282,7 +284,9 @@ b = HaloVector(data) # Solve the system with CG u_sol, stats = Krylov.cg(A, b, atol=1e-12, rtol=0.0, verbose=1) +``` +```@example halo-regions # The exact solution is u(x,y) = sin(πx) * sin(πy) u_star = [sin(π * i * Δx) * sin(π * j * Δy) for i=1:Nx, j=1:Ny] norm(u_sol.data[1:Nx, 1:Ny] - u_star, Inf) diff --git a/docs/src/matrix_free.md b/docs/src/matrix_free.md index 95b87bc86..88a61c64b 100644 --- a/docs/src/matrix_free.md +++ b/docs/src/matrix_free.md @@ -284,7 +284,9 @@ A = FFTPoissonOperator(n, L, complex) # Solve the linear system using CG u_sol, stats = cg(A, f, atol=1e-10, rtol=0.0, verbose=1) +``` +```@example fft_poisson # The exact solution is u(x) = -sin(x) u_star = -sin.(x) u_star ≈ u_sol @@ -418,7 +420,9 @@ f = vec(F) # Solve the linear system using MinAres u_sol, stats = minares(A, f, atol=1e-10, rtol=0.0, verbose=1) +``` +```@example helmholtz # Solution as 3D array U_sol = reshape(u_sol, Nx, Ny, Nz)