From 360c87f518d17fe305c1c4c1da9a17f037581132 Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Wed, 6 Mar 2024 20:20:44 +0100 Subject: [PATCH] describe views in the basic interface tutorial (#255) * describe views * Update basic_interface.md * Update basic_interface.md --- Project.toml | 2 +- docs/src/tutorials/basic_interface.md | 51 ++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 53e75885..2ccc1915 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SummationByPartsOperators" uuid = "9f78cca6-572e-554e-b819-917d2f1cf240" author = ["Hendrik Ranocha"] -version = "0.5.54" +version = "0.5.55" [deps] ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197" diff --git a/docs/src/tutorials/basic_interface.md b/docs/src/tutorials/basic_interface.md index a848ac7f..6d7cda66 100644 --- a/docs/src/tutorials/basic_interface.md +++ b/docs/src/tutorials/basic_interface.md @@ -31,7 +31,9 @@ D * u As you can see above, calling `D * u` allocates a new vector for the result. If you want to apply an SBP operator multiple times and need -good performance, you should consider using an in-place update instead. +good performance, you should consider using pre-allocating the output +and using in-place update instead. This strategy is also described in +the [performance tips in the Julia manual](https://docs.julialang.org/en/v1/manual/performance-tips/#Pre-allocating-outputs). Julia provides the function `mul!` for this purpose. ```@repl @@ -161,3 +163,50 @@ difference = D * x.^3 - 3 * x.^2 error_l2 = sqrt(integrate(abs2, difference, D)) ``` + + +## Multi-dimensional cases or multiple variables + +If you want to work with multiple space dimensions, you can still use +the 1D operators provided by +[SummationByPartsOperators.jl](https://github.com/ranocha/SummationByPartsOperators.jl) +if you apply them in a tensor product fashion along each space dimension. + +```@repl +using SummationByPartsOperators + +D = derivative_operator(MattssonNordström2004(), + derivative_order = 1, accuracy_order = 4, + xmin = 0.0, xmax = 1.0, N = 9) + +x = y = grid(D) + +u = x .* y'.^2 # u(x, y) = x y^2 + +let du_dx = zero(u) + for j in axes(u, 2) + mul!(view(du_dx, :, j), D, view(u, :, j)) + end + # The derivative of x*y^2 with respect to x is just y^2. + # Thus, the result is constant in each column and varies + # in the rows. + du_dx +end + +let du_dy = zero(u) + for i in axes(u, 1) + mul!(view(du_dy, i, :), D, view(u, i, :)) + end + # The derivative of x*y^2 with respect to y is 2*x*y. + du_dy +end + +2 .* x .* y' +``` + +Here, we have used `view`s to interpret parts of the memory of the +multi-dimensional arrays as one-diemnsional vectors that can be used +together with the operators of +[SummationByPartsOperators.jl](https://github.com/ranocha/SummationByPartsOperators.jl). +You can use the same trick if you collect values of multiple variables +in a multi-dimensional array.