SciML / MultiScaleArrays.jl

A framework for developing multi-scale arrays for use in scientific machine learning (SciML) simulations
https://docs.sciml.ai/MultiScaleArrays/stable/
Other
73 stars 16 forks source link

print_human_readable #46

Closed HugoMH closed 4 years ago

HugoMH commented 4 years ago

I tried to use the show function insead of print_human_readable, but then, I get this error at the construction of the MultiScaleArrays:

8-element Population{Cell{Float64},Float64}:
Error showing value of type Population{Cell{Float64},Float64}:
ERROR: type Cell has no field nodes
Stacktrace:
 [1] getproperty(::Any, ::Symbol) at ./Base.jl:20
 [2] nodechild(::Array{Cell{Float64},1}, ::Int64, ::Int64) at /home/mathehuh/.julia/packages/MultiScaleArrays/H1D4E/src/indexing.jl:2
 [3] getindex at /home/mathehuh/.julia/packages/MultiScaleArrays/H1D4E/src/indexing.jl:31 [inlined]
 [4] isassigned(::Population{Cell{Float64},Float64}, ::Int64, ::Int64) at ./abstractarray.jl:405
 [5] alignment(::IOContext{REPL.Terminals.TTYTerminal}, ::Population{Cell{Float64},Float64}, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Int64, ::Int64, ::Int64) at ./arrayshow.jl:67
 [6] print_matrix(::IOContext{REPL.Terminals.TTYTerminal}, ::Population{Cell{Float64},Float64}, ::String, ::String, ::String, ::String, ::String, ::String, ::Int64, ::Int64) at ./arrayshow.jl:186
 [7] print_matrix at ./arrayshow.jl:159 [inlined]
 [8] print_array at ./arrayshow.jl:308 [inlined]
 [9] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::Population{Cell{Float64},Float64}) at ./arrayshow.jl:345
 [10] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/REPL/src/REPL.jl:132
 [11] display(::REPL.REPLDisplay, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/REPL/src/REPL.jl:136
 [12] display(::Any) at ./multimedia.jl:323
 [13] #invokelatest#1 at ./essentials.jl:790 [inlined]
 [14] invokelatest at ./essentials.jl:789 [inlined]
 [15] print_response(::IO, ::Any, ::Bool, ::Bool, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/REPL/src/REPL.jl:156
 [16] print_response(::REPL.AbstractREPL, ::Any, ::Bool, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/REPL/src/REPL.jl:141
 [17] (::getfield(REPL, Symbol("#do_respond#38")){Bool,getfield(REPL, Symbol("##48#57")){REPL.LineEditREPL,REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/REPL/src/REPL.jl:718
 [18] #invokelatest#1 at ./essentials.jl:790 [inlined]
 [19] invokelatest at ./essentials.jl:789 [inlined]
 [20] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/REPL/src/LineEdit.jl:2306
 [21] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/REPL/src/REPL.jl:1038
 [22] run_repl(::REPL.AbstractREPL, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/REPL/src/REPL.jl:201
 [23] (::getfield(Base, Symbol("##737#739")){Bool,Bool,Bool,Bool})(::Module) at ./client.jl:390
 [24] #invokelatest#1 at ./essentials.jl:790 [inlined]
 [25] invokelatest at ./essentials.jl:789 [inlined]
 [26] run_main_repl(::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at ./client.jl:374
 [27] exec_options(::Base.JLOptions) at ./client.jl:312
 [28] _start() at ./client.jl:464

I did not find where this error comes from, so kept the "print_human_readable" function. I tested the modified version of the package with the following code, and it worked:

using Pkg
Pkg.add(PackageSpec(url="http://github.com/HugoMH/MultiScaleArrays.jl", rev="master"))
using(MultiScaleArrays)

struct Cell{B} <: AbstractMultiScaleArrayLeaf{B}
    values::Vector{B}
end
struct Population{T<:AbstractMultiScaleArray,B<:Number} <: AbstractMultiScaleArray{B}
    nodes::Vector{T}
    values::Vector{B}
    end_idxs::Vector{Int}
end
struct Tissue{T<:AbstractMultiScaleArray,B<:Number} <: AbstractMultiScaleArray{B}
    nodes::Vector{T}
    values::Vector{B}
    end_idxs::Vector{Int}
end
struct Embryo{T<:AbstractMultiScaleArray,B<:Number} <: AbstractMultiScaleArrayHead{B}
    nodes::Vector{T}
    values::Vector{B}
    end_idxs::Vector{Int}
end
cell1 = Cell([1.0; 2.0; 3.0])
cell2 = Cell([4.0; 5.0])
cell3 = Cell([3.0; 2.0; 5.0])
cell4 = Cell([4.0; 6.0])
population  = construct(Population, deepcopy([cell1, cell3, cell4]))
population2 = construct(Population, deepcopy([cell1, cell3, cell4]))
population3 = construct(Population, deepcopy([cell1, cell3, cell4]))
tissue1 = construct(Tissue, deepcopy([population, population2, population3])) # Make a Tissue from Populations
tissue2 = construct(Tissue, deepcopy([population2, population, population3]))
embryo = construct(Embryo, deepcopy([tissue1, tissue2])) # Make an embryo from Tissues
print_human_readable(embryo)
print_human_readable(embryo;NcharPerName=2)
print_human_readable(embryo;NcharPerName=2,fields=["values"])
print_human_readable(embryo.nodes[1].nodes[1];fields=["values"])
ChrisRackauckas commented 4 years ago

You might need to specialize the dispatch on AbstractMultiScaleArrayLeaf.

BTW, using a more structured package development approach might make things easier. https://www.youtube.com/watch?v=QVmU29rCjaA might be helpful.

ChrisRackauckas commented 4 years ago

Finished in https://github.com/SciML/MultiScaleArrays.jl/pull/50