ONSAS / ONSAS.jl

An Open Nonlinear Structural Analysis Solver in Julia
MIT License
7 stars 1 forks source link

Use meta to define repeatedly methods with symbolic patterns #31

Closed mvanzulli closed 2 months ago

mvanzulli commented 1 year ago

Create access with @eval :

# Composed accessors 
#TODO:fixme
 for m in methodswith(AbstractBoundaryCondition)
     @eval $m(fbc::FixedDisplacementBoundaryCondition) = $m(fbc.bc)
 end

Instead of:

dofs(pbc::PinnedDisplacementBoundaryCondition) = dofs(pbc.bc)
Base.values(pbc::PinnedDisplacementBoundaryCondition) = values(pbc.bc)
label(pbc::PinnedDisplacementBoundaryCondition) = label(pbc.bc)

Another option for composition is:


using Lazy

@forward ParentObject.attribute method_1, method_2,... 

Modules to check

mvanzulli commented 1 year ago

Use @eval to define less line numbers of code


# TODO use @eval
"Returns the displacements solution at the `PointEvalHandler` `peh`."
function displacements(st_sol::StatesSolution, peh::PointEvalHandler)

    points_interpolators = interpolator(peh)
    vec_points = points(peh)
    num_points = length(vec_points)

    sol_points = Vector{Vector{Vector{Float64}}}(undef, num_points)

    for index_p in 1:num_points

        interpolator_p = node_to_weights(points_interpolators)[index_p]

        # Compute the first node contribution and the sum up
        node_values = [weight * displacements(st_sol, node) for (node, weight) in pairs(interpolator_p)]
        p_values = reduce(+, node_values)
        sol_points[index_p] = p_values
    end
    return sol_points
end

"Returns the displacements component `i` solution at the `PointEvalHandler` `peh`."
displacements(st_sol::StatesSolution, peh::PointEvalHandler, i::Int) = getindex.(displacements(st_sol, peh), i)

"Returns the internal forces solution  at the `PointEvalHandler` `peh`."
function internal_forces(st_sol::StatesSolution, peh::PointEvalHandler)

    interpolators = interpolator(peh)
    vec_points = points(peh)
    num_points = length(vec_points)

    sol_points = Vector{Vector{Vector{Float64}}}(undef, num_points)

    for index_p in 1:num_points
        interpolator_p = node_to_weights(points_interpolators)[index_p]

        # Compute the first node contribution and the sum up
        node_values = [weight * internal_forces(st_sol, node) for (node, weight) in pairs(node_to_weights(interpolator_p))]
        p_values = reduce(+, node_values)
        sol_points[index_p] = p_values
    end
    return sol_points
end

"Returns the internal ftorce component `i` solution at the `PointEvalHandler` `peh`."
internal_forces(st_sol::StatesSolution, peh::PointEvalHandler, i::Int) = getindex.(internal_forces(st_sol, peh), i)

"Returns the external forces solution  at the `PointEvalHandler` `peh`."
function external_forces(st_sol::StatesSolution, peh::PointEvalHandler)

    interpolators = interpolator(peh)
    vec_points = points(peh)
    num_points = length(vec_points)

    sol_points = Vector{Vector{Vector{Float64}}}(undef, num_points)

    for index_p in 1:num_points
        interpolator_p = node_to_weights(points_interpolators)[index_p]

        # Compute the first node contribution and the sum up
        node_values = [weight * external_forces(st_sol, node) for (node, weight) in pairs(interpolator_p)]
        p_values = reduce(+, node_values)
        sol_points[index_p] = p_values
    end
    return sol_points
end

"Returns the internal force component `i` solution at the `PointEvalHandler` `peh`."
external_forces(st_sol::StatesSolution, peh::PointEvalHandler, i::Int) = getindex.(external_forces(st_sol, peh), i)

"Returns the stresses solution  at the `PointEvalHandler` `peh`."
function stress(st_sol::StatesSolution, peh::PointEvalHandler)

    point_to_element_vec = points_to_element(interpolator(peh))
    vec_points = points(peh)
    num_points = length(vec_points)

    sol_points = Vector{Vector{<:AbstractMatrix{Float64}}}(undef, num_points)

    for index_p in 1:num_points
        element_p = point_to_element_vec[index_p]
        sol_points[index_p] = stress(st_sol, element_p)
    end
    return sol_points
end