Skip to content

Commit b8c9603

Browse files
authored
Merge pull request #194 from miguelmaso/out-path
Writing output of tutorials to a folder
2 parents c82d090 + 594aaea commit b8c9603

25 files changed

+87
-66
lines changed

deps/build.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ files = [
3636

3737
Sys.rm(notebooks_dir;recursive=true,force=true)
3838
for (i,(title,filename)) in enumerate(files)
39-
notebook_prefix = string("t",@sprintf "%03d_" i)
40-
notebook = string(notebook_prefix,splitext(filename)[1])
39+
notebook_prefix = string("t",@sprintf "%03d" i)
40+
notebook = string(notebook_prefix,"_",splitext(filename)[1])
4141
notebook_title = string("# # Tutorial ", i, ": ", title)
4242
function preprocess_notebook(content)
43+
content = replace(content, "output_path" => notebook_prefix)
4344
return string(notebook_title, "\n\n", content)
4445
end
4546
Literate.notebook(joinpath(repo_src,filename), notebooks_dir; name=notebook, preprocess=preprocess_notebook, documenter=false, execute=false)

src/TopOptEMFocus.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,14 @@ function gf_p(p0::Vector, grad::Vector; r, β, η, phys_params, fem_params)
421421
grad[:] = dgdp
422422
end
423423
gvalue = gf_p(p0::Vector; r, β, η, phys_params, fem_params)
424-
open("gvalue.txt", "a") do io
424+
open("output_path/gvalue.txt", "a") do io
425425
write(io, "$gvalue \n")
426426
end
427427
gvalue
428428
end
429429

430+
mkpath("output_path")
431+
430432
# Using the following codes, we can check if we can get the derivatives correctly from the adjoint method by comparing it with the finite difference results.
431433
#
432434

@@ -494,7 +496,7 @@ ax.aspect = AxisAspect(1)
494496
ax.title = "Design Shape"
495497
rplot = 110 # Region for plot
496498
limits!(ax, -rplot, rplot, (h1)/2-rplot, (h1)/2+rplot)
497-
save("shape.png", fig)
499+
save("output_path/shape.png", fig)
498500

499501

500502
# ![](../assets/TopOptEMFocus/shape.png)
@@ -512,7 +514,7 @@ Colorbar(fig[1,2], plt)
512514
ax.title = "|E|"
513515
ax.aspect = AxisAspect(1)
514516
limits!(ax, -rplot, rplot, (h1)/2-rplot, (h1)/2+rplot)
515-
save("Field.png", fig)
517+
save("output_path/Field.png", fig)
516518

517519
# ![](../assets/TopOptEMFocus/Field.png)
518520
#

src/advection_diffusion.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ uh_non_zero = Gridap.Algebra.solve(op_non_zero)
162162

163163
# Ouput the result as a `.vtk` file.
164164

165-
writevtk(Ω,"results_zero",cellfields=["uh_zero"=>uh_zero])
165+
mkpath("output_path")
166166

167-
writevtk(Ω,"results_non_zero",cellfields=["uh_non_zero"=>uh_non_zero])
167+
writevtk(Ω,"output_path/results_zero",cellfields=["uh_zero"=>uh_zero])
168+
169+
writevtk(Ω,"output_path/results_non_zero",cellfields=["uh_non_zero"=>uh_non_zero])
168170

169171
# ## Visualization
170172

src/darcy.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ uh, ph = xh
136136

137137
# Since this is a multi-field example, the `solve` function returns a multi-field solution `xh`, which can be unpacked in order to finally recover each field of the problem. The resulting single-field objects can be visualized as in previous tutorials (see next figure).
138138

139-
writevtk(trian,"darcyresults",cellfields=["uh"=>uh,"ph"=>ph])
139+
mkpath("output_path")
140+
writevtk(trian,"output_path/darcyresults",cellfields=["uh"=>uh,"ph"=>ph])
140141

141142
# ![](../assets/darcy/darcy_results.png)
142143
#

src/dg_discretization.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ model = CartesianDiscreteModel(domain,partition)
162162
# to be generated in each direction (here $4\times4\times4$ cells). You can
163163
# write the model in vtk format to visualize it (see next figure).
164164

165-
writevtk(model,"model")
165+
mkpath("output_path")
166+
writevtk(model,"output_path/model")
166167

167168
# ![](../assets/dg_discretization/model.png)
168169
#
@@ -240,7 +241,7 @@ U = TrialFESpace(V)
240241
# written into a vtk file for its visualization (see next figure, where the
241242
# interior facets $\mathcal{F}_\Lambda$ are clearly observed).
242243

243-
writevtk(Λ,"strian")
244+
writevtk(Λ,"output_path/strian")
244245

245246
# ![](../assets/dg_discretization/skeleton_trian.png)
246247
#
@@ -329,7 +330,7 @@ uh = solve(op)
329330
# faces. We compute and visualize the jump of these values as follows (see next
330331
# figure):
331332

332-
writevtk(Λ,"jumps",cellfields=["jump_u"=>jump(uh)])
333+
writevtk(Λ,"output_path/jumps",cellfields=["jump_u"=>jump(uh)])
333334

334335
# Note that the jump of the numerical solution is very small, close to the
335336
# machine precision (as expected in this example with manufactured solution).

src/elasticity.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ model = DiscreteModelFromFile("../models/solid.json")
4949

5050
# In order to inspect it, write the model to vtk
5151

52-
writevtk(model,"model")
52+
mkpath("output_path")
53+
writevtk(model,"output_path/model")
5354

5455
# and open the resulting files with Paraview. The boundaries $\Gamma_{\rm B}$ and $\Gamma_{\rm G}$ are identified with the names `"surface_1"` and `"surface_2"` respectively. For instance, if you visualize the faces of the model and color them by the field `"surface_2"` (see next figure), you will see that only the faces on $\Gamma_{\rm G}$ have a value different from zero.
5556
#
@@ -118,7 +119,7 @@ uh = solve(op)
118119
#
119120
# Finally, we write the results to a file. Note that we also include the strain and stress tensors into the results file.
120121

121-
writevtk(Ω,"results",cellfields=["uh"=>uh,"epsi"=>ε(uh),"sigma"=>σε(uh)])
122+
writevtk(Ω,"output_path/results",cellfields=["uh"=>uh,"epsi"=>ε(uh),"sigma"=>σε(uh)])
122123

123124
# It can be clearly observed (see next figure) that the surface $\Gamma_{\rm B}$ is pulled in $x_1$-direction and that the solid deforms accordingly.
124125
#
@@ -186,7 +187,7 @@ uh = solve(op)
186187

187188
# Once the solution is computed, we can store the results in a file for visualization. Note that, we are including the stress tensor in the file (computed with the bi-material law).
188189

189-
writevtk(Ω,"results_bimat",cellfields=
190+
writevtk(Ω,"output_path/results_bimat",cellfields=
190191
["uh"=>uh,"epsi"=>ε(uh),"sigma"=>σ_bimat(ε(uh),tags)])
191192

192193
# #### Constant multi-material law
@@ -200,6 +201,6 @@ tags_field = CellField(tags, Ω)
200201
# `tags_field` is a field which value at $x$ is the tag of the cell containing $x$. `σ_bimat_cst` is used like a constant in (bi)linear form definition and solution export:
201202

202203
a(u,v) = ( σ_bimat_cst * (u)(v))*
203-
writevtk(Ω,"const_law",cellfields= ["sigma"=>σ_bimat_cst])
204+
writevtk(Ω,"output_path/const_law",cellfields= ["sigma"=>σ_bimat_cst])
204205

205206
# Tutorial done!

src/emscatter.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ uh_t = CellField(x->H_t(x,xc,r,ϵ₁,λ),Ω)
291291
# ![](../assets/emscatter/Results.png)
292292

293293
# ### Save to file and view
294-
writevtk(Ω,"demo",cellfields=["Real"=>real(uh),
294+
mkpath("output_path")
295+
writevtk(Ω,"output_path/demo",cellfields=["Real"=>real(uh),
295296
"Imag"=>imag(uh),
296297
"Norm"=>abs2(uh),
297298
"Real_t"=>real(uh_t),

src/fsi_tutorial.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ using Gridap
7676
model = DiscreteModelFromFile("../models/elasticFlag.json")
7777

7878
# We can inspect the loaded geometry and associated parts by printing to a `vtk` file:
79-
writevtk(model,"model")
79+
mkpath("output_path")
80+
writevtk(model,"output_path/model")
8081

8182
# This will produce an output in which we can identify the different parts of the domain, with the associated labels and tags.
8283
#
@@ -357,12 +358,12 @@ uhs, uhf, ph = solve(op)
357358
# ```
358359
# ### Visualization
359360
# The solution fields $[\mathbf{U}^h_{\rm S},\mathbf{U}^h_{\rm F},\mathbf{P}^h_{\rm F}]^T$ are defined over all the domain, extended with zeros on the inactive part. Calling the function `writevtk` passing the global triangulation, we will output the global fields.
360-
writevtk(Ω,"trian", cellfields=["uhs" => uhs, "uhf" => uhf, "ph" => ph])
361+
writevtk(Ω,"output_path/trian", cellfields=["uhs" => uhs, "uhf" => uhf, "ph" => ph])
361362
# ![](../assets/fsi/Global_solution.png)
362363

363364
# However, we can also restrict the fields to the active part by calling the function `restrict` with the field along with the respective active triangulation.
364-
writevtk(Ω_s,"trian_solid",cellfields=["uhs"=>uhs])
365-
writevtk(Ω_f,"trian_fluid",cellfields=["ph"=>ph,"uhf"=>uhf])
365+
writevtk(Ω_s,"output_path/trian_solid",cellfields=["uhs"=>uhs])
366+
writevtk(Ω_f,"output_path/trian_fluid",cellfields=["ph"=>ph,"uhf"=>uhf])
366367
# ![](../assets/fsi/Local_solution.png)
367368

368369
# ```@raw HTML

src/geometry_dev.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ reffes, cell_types = compress_cell_data(cell_reffes)
225225
new_grid = UnstructuredGrid(new_node_coordinates,cell_to_nodes,reffes,cell_types)
226226

227227
# Save for visualization:
228-
writevtk(new_grid,"half_cylinder_linear")
228+
mkpath("output_path")
229+
writevtk(new_grid,"output_path/half_cylinder_linear")
229230

230231
#
231232
# If we visualize the result, we'll notice that despite applying a curved mapping,
@@ -267,7 +268,7 @@ new_node_coordinates = map(F,new_node_coordinates)
267268

268269
# Create the high-order grid:
269270
new_grid = UnstructuredGrid(new_node_coordinates,new_cell_to_nodes,new_reffes,cell_types)
270-
writevtk(new_grid,"half_cylinder_quadratic")
271+
writevtk(new_grid,"output_path/half_cylinder_quadratic")
271272

272273
# The resulting mesh now accurately represents the curved geometry of the half-cylinder,
273274
# with quadratic elements properly capturing the curvature (despite paraview still showing
@@ -427,7 +428,7 @@ node_to_entity = get_face_entity(labels,0) # For each node, its associated entit
427428

428429
# It is usually more convenient to visualise it in Paraview by exporting to vtk:
429430

430-
writevtk(model,"labels_basic",labels=labels)
431+
writevtk(model,"output_path/labels_basic",labels=labels)
431432

432433
# Another useful way to create a `FaceLabeling` is by providing a coloring for the mesh cells,
433434
# where each color corresponds to a different tag.
@@ -436,21 +437,21 @@ writevtk(model,"labels_basic",labels=labels)
436437
cell_to_tag = [1,1,1,2,2,3,2,2,3]
437438
tag_to_name = ["A","B","C"]
438439
labels_cw = Geometry.face_labeling_from_cell_tags(topo,cell_to_tag,tag_to_name)
439-
writevtk(model,"labels_cellwise",labels=labels_cw)
440+
writevtk(model,"output_path/labels_cellwise",labels=labels_cw)
440441

441442
# We can also create a `FaceLabeling` from a vertex filter. The resulting `FaceLabeling` will have
442443
# only one tag, gathering the d-faces whose vertices ALL fullfill `filter(x) == true`.
443444

444445
vfilter(x) = abs(x[1]- 1.0) < 1.e-5
445446
labels_vf = Geometry.face_labeling_from_vertex_filter(topo, "top", vfilter)
446-
writevtk(model,"labels_filter",labels=labels_vf)
447+
writevtk(model,"output_path/labels_filter",labels=labels_vf)
447448

448449
# `FaceLabeling` objects can also be merged together. The resulting `FaceLabeling` will have
449450
# the union of the tags and entities of the original ones.
450451
# Note that this modifies the first `FaceLabeling` in place.
451452

452453
labels = merge!(labels, labels_cw, labels_vf)
453-
writevtk(model,"labels_merged",labels=labels)
454+
writevtk(model,"output_path/labels_merged",labels=labels)
454455

455456
# ### Creating new tags from existing ones
456457
#
@@ -474,7 +475,7 @@ Geometry.add_tag_from_tags_complementary!(labels,"!A",["A"])
474475
# and creates a new tag that contains all the d-faces that are in the first list but not in the second.
475476
Geometry.add_tag_from_tags_setdiff!(labels,"A-B",["A"],["B"]) # set difference
476477

477-
writevtk(model,"labels_setops",labels=labels)
478+
writevtk(model,"output_path/labels_setops",labels=labels)
478479

479480
# ### FaceLabeling queries
480481
#

src/hyperelasticity.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ function run(x0,disp_x,step,nsteps,cache)
9696

9797
uh, cache = solve!(uh,solver,op,cache)
9898

99-
writevtk(Ω,"results_$(lpad(step,3,'0'))",cellfields=["uh"=>uh,"sigma"=>σ(uh)])
99+
mkpath("output_path")
100+
writevtk(Ω,"output_path/results_$(lpad(step,3,'0'))",cellfields=["uh"=>uh,"sigma"=>σ(uh)])
100101

101102
return get_free_dof_values(uh), cache
102103

0 commit comments

Comments
 (0)