PostProcess
HyperShape.PostProcess — Module
HyperShape.PostProcessPost-processing of an optimisation, one call per design iteration (the pattern of HyperFEM's PostProcessor):
OptPostProcessor: calls the user'sdriver(post, ctx)everyeveryiterations, keeps the.pvdcollection (the iteration is the time) and writes the history of the run tohistory.csv.write_step: writes the fields of the current iteration with aFieldWriter(VTU or VTKHDF, the mesh once, values from the degrees of freedom, optionally without blocking the loop).FieldWriter,write_fields: the writer, usable on its own (also outside an optimisation).extend_field: a field defined on the design zone, on a larger triangulation, with a fixed value outside.
HyperShape.PostProcess.FieldWriter — Type
FieldWriter(Ω, base; format = :vtu, precision = Float64, compress = false, async = Threads.nthreads() > 1,
maxpending = 2, collection = nothing, resume_upto = nothing)Fast writer of fields on the triangulation Ω (one cell type, cells of the dimension of the space; a part of a model is fine). The mesh is built once, continuous (one point per vertex of the grid, not one per cell and vertex as createvtk), and the values come straight from the degrees of freedom:
- P0 fields as cell data; P1/Q1 (and the vertices of higher orders) as point data, scalar, vector or tensor, with their Dirichlet values; pointwise operations on them and on constants (
θ.ρ,E ∘ θ.ρ,1 - ρ);extend_field(points of the interface take the inside value). - Any other
CellField(discontinuous fields, gradients, P0 mixed with P1, a function ofx) is evaluated by Gridap at the vertices and averaged at the points, in the same file, with a warning once per field (slower).
format: :vtu (one file base_<index>.vtu per step, mesh included; with collection, a get_pvd collection, each file is added at its time once written) or :vtkhdf (one file base.vtkhdf with every step and the mesh written once; needs using HDF5, ParaView ≥ 5.12; the same fields at every step and increasing times). precision = Float32 halves the values (the points stay Float64); compress uses zlib (smaller, slower).
With async the values are copied and the file is written by another task: the caller waits only for the copy, at most maxpending steps are queued (then it waits for the disk). A collection lags behind by the queued steps. After an error of a write, later steps are not written and the error is thrown by the next write_fields or by close. close(w) waits for the pending writes (idempotent); wait(w) waits without closing.
resume_upto (VTKHDF): an existing file of the same mesh keeps its steps with time ≤ resume_upto, and the writer appends after them (a resumed optimisation). OptPostProcessor builds and closes these writers itself (see write_step).
HyperShape.PostProcess.OptPostProcessor — Type
OptPostProcessor(driver; every, filepath = nothing, initial = true, history = true, name = "optimization",
format = :vtu, precision = Float64, compress = false, async = Threads.nthreads() > 1,
maxpending = 2)
OptPostProcessor(; every, filepath = nothing, …)Post-processing of an optimisation: pass it to solve!(…; post). solve! calls driver(post, ctx) (written by the user, as the driver of HyperFEM's PostProcessor) with the fields, the variables and the forward state at the accepted design:
- at the starting design (iteration 0, or the one of a resumed checkpoint), if
initial. It costs no extra solve: the forward solve of the first iteration is done before it (so the user'ssolveseesctx.iteration == 0in that solve, and theforwardcolumn of the history counts it in no iteration). Not repeated when the collection already has that iteration, nor in a resumed run whose first iteration is a continuation jump; - after every accepted iteration that is a multiple of
every(mandatory: one call per iteration can fill the disk; the multiples count from iteration 0, also in a resumed run); - at the end of the run (converged, stalled or
maxiter) and when it fails (at the last accepted design, if the forward problem can still be solved there), when that iteration has not been written yet.
ctx is the OptContext (phase :optimization, ctx.iteration the iteration of the design). Nothing is called during check_consistency or for the trial designs of a line search. An error thrown by the driver stops the optimisation, except in the call after a failure (then it is reported and the failure is thrown).
filepath (a folder, created if needed; made absolute when the post-processor is built) enables the collection filepath/<name>.pvd (get_pvd, with the iteration as time; written after every entry, so it can be opened in ParaView during the run) and, if history, filepath/history.csv (the columns of OptHistory, written again at every call). Without filepath the driver writes what it wants (or nothing: a callback). Each solve! starts a new collection (files of an earlier run in the folder are left there, outside it); a resumed run keeps the entries up to the iteration of the checkpoint. The collection file is created with its first entry (none with format = :vtkhdf).
format, precision, compress, async and maxpending are the options of the FieldWriters that write_step builds (with async, the collection lags behind the loop by the queued steps): format = :vtkhdf (one file per triangulation and name with every iteration; needs using HDF5) suits large 3D runs, :vtu (default) any ParaView. The writers are closed (their pending writes finished) when solve! returns, fails or is interrupted. An error of a write during the run stops it (it is thrown by the next write_step); one that appears when the writers are closed at the end is logged with @error and solve! still returns its result. After a stall in a continuation stage whose last iteration was already written, the result of solve! is at the new stage but that iteration is not written again.
The second form has no driver: it takes the output returned by the physics_setup of a problem built with Optimization(model; design_setup, physics_setup) (a function (post, ctx) -> …, built again with the problem when solve! rebuilds it; the writers are closed at each rebuild).
The driver can write with write_step (HyperShape's fast writer), add any file it writes itself (get_pvd(post)[ctx.iteration] = "file.vtu") or write nothing to the collection. Fields: post.filepath, post.every, post.iteration (of the current call) and post.problem (the Optimization being solved, set by solve!; it can be built before or after the post-processor).
post = OptPostProcessor(every = 10, filepath = "results/mbb") do post, ctx
write_step(post, Ω, "rho" => θ.ρ, "x" => x.ρ, "u" => get_state(S))
end
solve!(problem, DensityMethod(OC()); maxiter = 300, post)HyperShape.PostProcess.PVDCollection — Type
PVDCollection(path)ParaView collection (.pvd) written again, under a temporary name and then renamed, after every entry, so it can be opened while the optimisation runs and stays valid if it is interrupted. c[t] = file adds a file written by any means (paths are stored relative to the folder of the collection); c[t] = vtk saves a WriteVTK file (what createvtk returns) and adds it (with a parallel file, only the .pvtu). Several entries at the same time are written as parts of that time.
HyperFEM.ComputationalModels.get_pvd — Method
get_pvd(post::OptPostProcessor)The .pvd collection of the run (needs filepath): get_pvd(post)[ctx.iteration] = createvtk(…) or = "file".
HyperShape.PostProcess.extend_field — Method
extend_field(f::CellField, Ω::Triangulation; outside) -> CellFieldThe field f, defined on a triangulation of the design zone (for example θ.ρ, x.ρ or a state on that zone), as a field on the larger triangulation Ω of the same background model: equal to f on the cells of the design zone and to outside on the others (for example 1.0 for a solid support, 0.0 for air). For a vector or tensor field, outside is a value of that type or a number for every independent component (0.0 gives zero). For writing (also with write_step) and integrating; returns f when Ω is its triangulation. The operation that defines f is also evaluated outside, at a zero value of its arguments (it must not throw there). Costs about 70 bytes per cell of Ω per call.
HyperShape.PostProcess.write_fields — Method
write_fields(w::FieldWriter, t, fields::Pair...; index = <step counter>)Writes fields ("name" => field, distinct names) at time t. The values are copied before returning; with async the file is written by the writer's task. index numbers the VTU file (base_<index>.vtu, 4 digits at least).
HyperShape.PostProcess.write_step — Method
write_step(post::OptPostProcessor, Ω, fields::Pair...; name = post.name)
write_step(post::OptPostProcessor, w::FieldWriter, fields::Pair...)Writes fields ("name" => CellField) of the current iteration on the triangulation Ω, with the FieldWriter of name (built at the first call with the format, precision, compress, async and maxpending of the post-processor): post.filepath/<name>_<iteration>.vtu added to the collection once written, or post.filepath/<name>.vtkhdf. A name belongs to one triangulation: several calls in one iteration (other fields, other triangulations) need different names. The second form writes with a writer built by the user (the user closes it). For createvtk (any field, the discontinuous mesh of Gridap), write in the driver and add the file: get_pvd(post)[ctx.iteration] = createvtk(Ω, file; cellfields = …).