From 44b89bdc4173a976fe1baeb6980a0a9825c2170b Mon Sep 17 00:00:00 2001 From: Jukka Aho Date: Tue, 3 Jul 2018 11:24:00 +0300 Subject: [PATCH] New public function write_results! (#26) `write_results!(analysis, writer)` is a function which is used to implement new results writers for some analysis. Usage example in pseudo-level: ```julia type XdmfWriter <: AbstractResultsWriter # stuff end type Dynamics <: AbstractAnalysis # stuff end function FEMBase.write_results!(analysis::Dynamics, writer::XdmfWriter) # fid = open(writer.filename) # for problem in get_problems(analysis) # field = problem("displacement", analysis.time) # write(fid, field) # end return nothing! end ``` --- src/FEMBase.jl | 3 ++- src/analysis.jl | 21 +++++++++++++++++ test/test_analysis.jl | 52 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/FEMBase.jl b/src/FEMBase.jl index c3f4492..b34f788 100644 --- a/src/FEMBase.jl +++ b/src/FEMBase.jl @@ -38,7 +38,8 @@ export LinearSystem, AbstractLinearSystemSolver, solve! include("analysis.jl") export AbstractAnalysis, Analysis, add_problems!, get_problems, run!, - AbstractResultsWriter, add_results_writer!, get_results_writers + AbstractResultsWriter, add_results_writer!, get_results_writers, + write_results! export FieldProblem, BoundaryProblem, Problem, Element, Assembly export Poi1, Seg2, Seg3, Tri3, Tri6, Tri7, Quad4, Quad8, Quad9, diff --git a/src/analysis.jl b/src/analysis.jl index 96505a5..99fff3d 100644 --- a/src/analysis.jl +++ b/src/analysis.jl @@ -37,3 +37,24 @@ end function run!{A<:AbstractAnalysis}(::Analysis{A}) info("This is a placeholder function for running an analysis $A for a set of problems.") end + +function write_results!(::Analysis{A}, ::W) where {A<:AbstractAnalysis, W<:AbstractResultsWriter} + info("Writing the results of analysis $A is not supported by a results writer $W") + return nothing +end + +function write_results!(analysis) + results_writers = get_results_writers(analysis) + if isempty(results_writers) + info("No result writers attached to the analysis $(analysis.name). ", + "In order to get results of the analysis stored to the disk, one ", + "must attach some results writer to the analysis using ", + "add_results_writer!, e.g. xdmf_writer = Xdmf(\"results\"); ", + "add_results_writer!(analysis, xdmf_writer)") + return nothing + end + for results_writer in results_writers + write_results!(analysis, results_writer) + end + return nothing +end diff --git a/test/test_analysis.jl b/test/test_analysis.jl index d57f294..fbce83c 100644 --- a/test/test_analysis.jl +++ b/test/test_analysis.jl @@ -11,9 +11,6 @@ type MyAnalysis1 <: AbstractAnalysis end type MyAnalysis2 <: AbstractAnalysis end -type TestResultsWriter <: AbstractResultsWriter -end - type MyProblem <: FieldProblem value :: Bool end @@ -38,9 +35,48 @@ end @test problem.properties.value == true end -@testset "test results writer" begin - writer = TestResultsWriter() - analysis1 = Analysis(MyAnalysis1) - add_results_writer!(analysis1, writer) - @test first(get_results_writers(analysis1)) === writer +## Result writers + +# Writing results of some analysis is done, in abstract level, by using +# results writers. They must be subtypes of `AbstractResultsWriter`: + +type TestResultsWriter <: AbstractResultsWriter end + +# Results writer is added to analysis using `add_results_writer!`: + +writer = TestResultsWriter() +analysis1 = Analysis(MyAnalysis1) +add_results_writer!(analysis1, writer) +@test first(get_results_writers(analysis1)) === writer + +# Results are written with function `write_results!`-function, taking analysis +# as input argument. Function applies all results writers (there can be many!) +# to the analysis in the order they are attached to analysis using +# `add_results_writer!`. Internally the function runs +# `write_results!(analysis, results_writer)`, which must be implemented for each +# analysis / results_writer pair. + +write_results!(analysis1) + +# This of course doesn't do anything useful but inform that writing the results +# of MyAnalysis1 using TestResultsWriter is not implemented as the actual +# implementation is missing. In pseudo-level, the actual implementation would +# be something like: + +function FEMBase.write_results!(analysis::MyAnalysis1, writer::TestResultsWriter) + # fid = open(writer.filename) + # for problem in get_problems(analysis) + # field = problem("displacement", analysis.time) + # write(fid, field) + # end + return nothing! +end + +# Lastly, it is not mandatory to have a results writer in the analysis at all +# or the results writer can write directly to stdout or do some other fancy +# stuff. If the results writer is not defined, user gets only info message +# about missing results writers. + +empty!(analysis1.results_writers) +write_results!(analysis1)