Skip to content

Commit

Permalink
describe views in the basic interface tutorial (#255)
Browse files Browse the repository at this point in the history
* describe views

* Update basic_interface.md

* Update basic_interface.md
  • Loading branch information
ranocha committed Mar 6, 2024
1 parent d2ecf60 commit 360c87f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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"
Expand Down
51 changes: 50 additions & 1 deletion docs/src/tutorials/basic_interface.md
Expand Up @@ -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
Expand Down Expand Up @@ -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.

3 comments on commit 360c87f

@ranocha
Copy link
Owner Author

@ranocha ranocha commented on 360c87f Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ranocha
Copy link
Owner Author

@ranocha ranocha commented on 360c87f Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/102418

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.55 -m "<description of version>" 360c87f518d17fe305c1c4c1da9a17f037581132
git push origin v0.5.55

Please sign in to comment.