Files
JuliaFEM.jl/src/lagrange.jl
T

81 lines
2.3 KiB
Julia
Raw Normal View History

2015-09-14 23:32:46 +03:00
# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/JuliaFEM.jl/blob/master/LICENSE.md
# Lagrange (Continous Galerkin) finite elements
2015-11-27 10:10:00 +02:00
abstract CG <: AbstractElement
2015-09-14 23:32:46 +03:00
"""
Given polynomial P and coordinates of reference element, calculate
2015-09-29 00:07:56 +03:00
Lagrange basis functions
2015-09-14 23:32:46 +03:00
"""
function calculate_lagrange_basis(P, X)
dim, nbasis = size(X)
A = zeros(nbasis, nbasis)
for i=1:nbasis
A[i,:] = P(X[:, i])
end
invA = inv(A)'
2015-10-27 06:37:58 +02:00
basis(xi) = (invA*P(xi))'
dbasisdxi(xi) = (ForwardDiff.jacobian((xi) -> invA*P(xi), xi, cache=autodiffcache))'
basis, dbasisdxi
2015-09-14 23:32:46 +03:00
end
"""
2015-09-29 00:07:56 +03:00
Create new Lagrange element
2015-09-14 23:32:46 +03:00
2015-09-29 00:07:56 +03:00
Examples
--------
>>> @create_lagrange_element(Seg2, "2 node linear segment", X, P)
"""
macro create_lagrange_element(element_name, element_description, X, P)
2015-09-14 23:32:46 +03:00
eltype = esc(element_name)
quote
2015-11-27 10:10:00 +02:00
global get_basis, get_dbasis
basis, dbasis = calculate_lagrange_basis($P, $X)
abstract $eltype <: CG
function get_basis(::Type{$eltype}, xi::Vector{Float64})
return basis(xi)
2015-09-29 00:07:56 +03:00
end
2015-11-27 10:10:00 +02:00
function get_dbasis(::Type{$eltype}, xi::Vector{Float64})
return dbasis(xi)
end
function $eltype(args...)
return Element{$eltype}(args...)
end
function Base.size(::Type{$eltype})
return Base.size($X)
2015-09-29 00:07:56 +03:00
end
2015-09-14 23:32:46 +03:00
end
end
# 1d Lagrange elements
2015-09-29 00:07:56 +03:00
@create_lagrange_element(Seg2, "2 node linear line element",
[-1.0 1.0], (xi) -> [1.0, xi[1]])
2015-09-14 23:32:46 +03:00
2015-09-29 00:07:56 +03:00
@create_lagrange_element(Seg3, "3 node quadratic line element",
[-1.0 1.0 0.0], (xi) -> [1.0, xi[1], xi[1]^2])
2015-09-14 23:32:46 +03:00
# 2d Lagrange elements
2015-09-29 00:07:56 +03:00
@create_lagrange_element(Tri3, "3 node bilinear triangle element",
2015-09-25 20:41:50 +03:00
[0.0 1.0 0.0
0.0 0.0 1.0],
2015-09-25 20:43:20 +03:00
(xi) -> [1.0, xi[1], xi[2]])
2015-09-25 20:41:50 +03:00
2015-09-29 00:07:56 +03:00
@create_lagrange_element(Quad4, "4 node bilinear quadrangle element",
2015-09-14 23:32:46 +03:00
[-1.0 1.0 1.0 -1.0
-1.0 -1.0 1.0 1.0],
(xi) -> [1.0, xi[1], xi[2], xi[1]*xi[2]])
# 3d Lagrange elements
2015-09-29 00:07:56 +03:00
@create_lagrange_element(Tet10, "10 node quadratic tetrahedron",
2015-09-14 23:32:46 +03:00
[0.0 1.0 0.0 0.0 0.5 0.5 0.0 0.0 0.5 0.0
0.0 0.0 1.0 0.0 0.0 0.5 0.5 0.0 0.0 0.5
0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.5 0.5 0.5],
(xi) -> [ 1.0, xi[1], xi[2], xi[3], xi[1]^2,
xi[2]^2, xi[3]^2, xi[1]*xi[2], xi[2]*xi[3], xi[3]*xi[1]])