Skip to content

Draft: Possible anisotropy concatenation #40

@miguelmaso

Description

@miguelmaso

FYI @jmartfrut @RogelioOrtigosa , just a reminder to keep the code at some place. This is the cleanest implementation I've reached. To be discussed:

abstract type Elasto end

abstract type IsoElastic <: Elasto end

abstract type AnisoElastic <: Elasto end

struct Foo <: IsoElastic end

struct Bar <: AnisoElastic end

struct MultiAniso <: AnisoElastic  # Not exported
    aniso::NTuple{N,AnisoElastic} where N
end

struct IsoMultiAniso <: AnisoElastic  # Not exported
    iso::IsoElastic
    aniso::MultiAniso
end

Base.hcat(a::AnisoElastic...) = MultiAniso(a)

+(a::IsoElastic, b::AnisoElastic) = IsoMultiAniso(a,MultiAniso((b,)))

+(a::IsoElastic, b::MultiAniso) = IsoMultiAniso(a,b)

transpose(x::NTuple{N, <:Tuple{<:Function, <:Function, <:Function}}) where N = map(i -> getindex.(x, i), 1:3)

function (obj::Foo)(args...)
    Ψ(F) = 0.0
    ∂Ψ∂F(F) = [0.0, 0.0, 0.0]
    ∂Ψ∂FF(F) = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    (Ψ, ∂Ψ∂F, ∂Ψ∂FF)
end

function (obj::Bar)(args...)
    Ψ(F,N) = 0.0
    ∂Ψ∂F(F,N) = [0.0, 0.0, 0.0]
    ∂Ψ∂FF(F,N) = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    (Ψ, ∂Ψ∂F, ∂Ψ∂FF)
end

function (obj::MultiAniso)(args...)
    DΨ = map(a -> a(args...), obj.aniso)
    Ψα, ∂Ψα∂F, ∂Ψα∂FF = transpose(DΨ)
    Ψ(F,N) = mapreduce(Ψi -> Ψi(F,N), Ψα)
    ∂Ψ∂F(F,N) = mapreduce(∂Ψi∂F -> ∂Ψi∂F(F,N), ∂Ψα∂F)
    ∂Ψ∂FF(F,N) = mapreduce(∂Ψi∂FF -> ∂Ψi∂FF(F,N), ∂Ψα∂FF)
    (Ψ, ∂Ψ∂F, ∂Ψ∂FF)
end

function (obj::IsoMultiAniso)(args...)
    Ψi, ∂Ψi∂F, ∂Ψi∂FF = obj.iso(args...)
    Ψa, ∂Ψa∂F, ∂Ψa∂FF = obj.aniso(args...)
    Ψ(F,N) = Ψi(F) + Ψa(F,N)
    ∂Ψ∂F(F,N) = ∂Ψi∂F(F) + ∂Ψa∂F(F,N)
    ∂Ψ∂FF(F,N) = ∂Ψi∂FF(F) + ∂Ψa∂FF(F,N)
    (Ψ, ∂Ψ∂F, ∂Ψ∂FF)
end

f = Foo()
b1 = Bar()
b2 = Bar()

fb = f + b1
bb = [b1 b2]
fbb = f + [b1 b2]

∂Ψ, ∂Ψ∂F, ∂Ψ∂FF = fb()
∂Ψ, ∂Ψ∂F, ∂Ψ∂FF = bb()
∂Ψ, ∂Ψ∂F, ∂Ψ∂FF = fbb()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions