I have defined a type called CylindricalStress
for bundling the results of Lamé’s Equations which I understand is good practice:
struct CylindricalStress
r::Number # Radial Stress
θ::Number # Tangential (Hoop) Stress
z::Number # Longitudinal Stress
end
"""
lame(r, pᵢ, pₒ=0)
Compute theoretical stress distribution from Lamé's thick wall cylinder equations.
# Arguments
- `r `: vector of points to compute stress over; must span from inside boundary to outside boundary
- `pᵢ`: pressure on inside boundary
- `pₒ`: pressure on outside boundary (pₒ=0 if argument is omitted)
"""
function lame(r::AbstractVector, pᵢ, pₒ = 0)
(rᵢ, rₒ) = extrema(r)
term1 = (rᵢ^2 * pᵢ - rₒ^2 * pₒ) / (rₒ^2 - rᵢ^2)
term2 = rᵢ^2 * rₒ^2 * (pᵢ - pₒ) / (rₒ^2 - rᵢ^2) ./ r.^2
σr = term1 .- term2
σθ = term1 .+ term2
σz = fill(term1, length(r))
σ = CylindricalStress.(σr, σθ, σz)
return σ
end
julia> σ = lame([5,10,15], 100)
3-element Vector{CylindricalStress}:
CylindricalStress(-100.0, 125.0, 12.5)
CylindricalStress(-15.625, 40.625, 12.5)
CylindricalStress(0.0, 25.0, 12.5)
1. How can I define my type as a subtype of something else so I don’t have to extend every function manually?
I just want this to act like any three-component vector for sum(σ)
, mean(σ)
, 10.*σ
, norm.(σ)
, etc.
2. What types should I enforce in the struct
and in the function
?
I have heard you need AbstractVector
in order to accept a slice, and that you can’t restrict to Number
if you want to accept Uniful.jl types. I would like to give the user some indication of what to input, especially scalar vs vector. (I debugged some errors already related to this.) How can I provide wide compatibility, but also helpful documentation and error checking on input/output types?
3. How can I keep my type as a “private implementation detail”?
I have heard the stance on this forum that custom types should not be publicly available. Why is that and how would that be implemented in this case?