Skip to content

Commit

Permalink
New public function write_results! (#26)
Browse files Browse the repository at this point in the history
`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
```
  • Loading branch information
ahojukka5 authored Jul 3, 2018
1 parent 2dba599 commit 44b89bd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/FEMBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions src/analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 44 additions & 8 deletions test/test_analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ type MyAnalysis1 <: AbstractAnalysis end

type MyAnalysis2 <: AbstractAnalysis end

type TestResultsWriter <: AbstractResultsWriter
end

type MyProblem <: FieldProblem
value :: Bool
end
Expand All @@ -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)

0 comments on commit 44b89bd

Please sign in to comment.