Skip to content

Instantly share code, notes, and snippets.

@j-fu
Created December 9, 2020 19:28
Show Gist options
  • Save j-fu/a5cbbb5bd758717f35a3beccd36c6fbd to your computer and use it in GitHub Desktop.
Save j-fu/a5cbbb5bd758717f35a3beccd36c6fbd to your computer and use it in GitHub Desktop.
CPU eating notebook
### A Pluto.jl notebook ###
# v0.12.16
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 60941eaa-1aea-11eb-1277-97b991548781
begin
using Pkg
Pkg.activate(mktempdir())
Pkg.add("TetGen")
Pkg.add("PyPlot")
Pkg.add("PlutoUI")
using TetGen
using PyPlot
using PlutoUI
end
# ╔═╡ 00820e3a-3a13-11eb-379d-57b3eaa8db4d
function t3dcube(;vol=0.1)
tio=RawTetGenIO{Cdouble}()
tio.pointlist=Cdouble[0 0 0;
1 0 0;
1 1 0;
0 1 0;
0 0 1;
1 0 1;
1 1 1;
0 1 1]'
TetGen.facetlist!(tio,[1 2 3 4;
5 6 7 8;
1 2 6 5;
2 3 7 6;
3 4 8 7;
4 1 5 8]')
tetrahedralize(tio,"pAqa$(vol)")
end
# ╔═╡ 7d3e106c-3a4d-11eb-0eed-2fb459d3ab3e
md"""
The next cell works well upon the first invocation, and as long as plotting is not enabled.
- enable 2D here: $(@bind do_plot2d CheckBox(default=false))
- enable 3D here: $(@bind do_plot3d CheckBox(default=false))
If we change the parameter (e.g. set it to 0.001), which does not create a really large grid, it starts running forever and eats up the memory, after the first plot was done, regardless of 2d or 3d.
It just gets stuck in a ccall (to tetrahedralize).
The same series of commands in the repl works well:
````julia
include("ttet.jl")
grd=t3dcube(vol=0.01)
plot2d(grd,f)
plot3d(grd,f)
grd=t3dcube(vol=0.001)
plot2d(grd,f)
plot3d(grd,f)
````
"""
# ╔═╡ a3633174-3a13-11eb-2d95-2974f9382c05
grd=t3dcube(vol=0.001)
# ╔═╡ f03f51c8-3a48-11eb-014d-31d19b52426d
f(x,y,z)=sin(x)*cos(2y)*cos(3z)
# ╔═╡ c72c700e-3a48-11eb-3001-8bdbc57e52be
function plot2d(grd,func)
facets=grd.trifacelist
coord=grd.pointlist
f=[func(coord[:,i]...) for i=1:size(coord,2)]
clf()
PyPlot.tricontour(coord[1,:],coord[2,:],transpose(facets.-1),f,cmap="hot")
gcf()
end
# ╔═╡ 06d838b4-3a49-11eb-32ac-4bc2811c8ee0
function plot3d(grd,func)
facets=grd.trifacelist
coord=grd.pointlist
f=[func(coord[:,i]...) for i=1:size(coord,2)]
clf()
collection=PyPlot.plot_trisurf(coord[1,:],coord[2,:],transpose(facets.-1),coord[3,:],cmap="hot")
color=[0.333*mapreduce(j->f[j],+,facets[:,i]) for i=1:size(facets,2)]
collection.set_array(color)
collection.autoscale()
gcf()
end
# ╔═╡ 32084150-3a49-11eb-1f9a-77b442156e0e
if do_plot2d
plot2d(grd,f)
end
# ╔═╡ dd1d7f74-3a4e-11eb-2104-bdd3eaf6638c
if do_plot3d
plot3d(grd,f)
end
# ╔═╡ Cell order:
# ╠═60941eaa-1aea-11eb-1277-97b991548781
# ╠═00820e3a-3a13-11eb-379d-57b3eaa8db4d
# ╟─7d3e106c-3a4d-11eb-0eed-2fb459d3ab3e
# ╠═a3633174-3a13-11eb-2d95-2974f9382c05
# ╠═f03f51c8-3a48-11eb-014d-31d19b52426d
# ╠═c72c700e-3a48-11eb-3001-8bdbc57e52be
# ╠═06d838b4-3a49-11eb-32ac-4bc2811c8ee0
# ╠═32084150-3a49-11eb-1f9a-77b442156e0e
# ╠═dd1d7f74-3a4e-11eb-2104-bdd3eaf6638c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment