Files
JuliaFEM.jl/src/lagrange.jl
T

109 lines
2.9 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
"""
2015-11-30 18:49:45 +02:00
function calculate_lagrange_basis_coefficients(P, X)
2015-09-14 23:32:46 +03:00
dim, nbasis = size(X)
A = zeros(nbasis, nbasis)
for i=1:nbasis
A[i,:] = P(X[:, i])
end
2015-11-30 18:49:45 +02:00
# invA = inv(A)'
# basis(xi) = (invA*P(xi))'
# dbasisdxi(xi) = (ForwardDiff.jacobian((xi) -> invA*P(xi), xi, cache=autodiffcache))'
# basis, dbasisdxi
# info(inv(A))
# info(P([0.0, 0.0]))
# invA = inv(A)'
# basis(xi) = invA*P(xi)
# return basis
return inv(A)'
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
2015-11-30 18:49:45 +02:00
#basis, dbasis = calculate_lagrange_basis($P, $X)
C = calculate_lagrange_basis_coefficients($P, $X)
basis(xi) = C*$P(xi)
# dbasis = ForwardDiff.jacobian(basis)
2015-11-27 10:10:00 +02:00
abstract $eltype <: CG
2015-11-30 18:49:45 +02:00
function get_basis(::Type{$eltype}, xi::Vector)
return basis(xi)'
2015-09-29 00:07:56 +03:00
end
2015-11-30 18:49:45 +02:00
#=
2015-11-27 10:10:00 +02:00
function get_dbasis(::Type{$eltype}, xi::Vector{Float64})
2015-11-30 18:49:45 +02:00
return dbasis(xi)'
2015-11-27 10:10:00 +02:00
end
2015-11-30 18:49:45 +02:00
=#
2015-11-27 10:10:00 +02:00
function $eltype(args...)
return Element{$eltype}(args...)
end
2015-11-30 18:49:45 +02:00
2015-11-27 10:10:00 +02:00
function Base.size(::Type{$eltype})
return Base.size($X)
2015-09-29 00:07:56 +03:00
end
2015-11-30 18:49:45 +02:00
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-11-28 14:06:12 +02:00
@create_lagrange_element(Tri6, "6 node quadratic triangle element",
[0.0 1.0 0.0 0.5 0.5 0.0
0.0 0.0 1.0 0.0 0.5 0.5],
(xi) -> [1.0, xi[1], xi[2], xi[1]^2, xi[2]^2, xi[1]*xi[2]])
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-11-28 14:06:12 +02:00
@create_lagrange_element(Tet4, "4 node tetrahedron",
[0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0],
(xi) -> [1.0, xi[1], xi[2], xi[3]])
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]])