Skip to content

Commit 0a82215

Browse files
rm Ferrite dependency
1 parent 959f884 commit 0a82215

File tree

3 files changed

+8
-148
lines changed

3 files changed

+8
-148
lines changed

Project.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
2424
LinearSolve = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae"
2525
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2626
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
27-
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
28-
Ferrite = "c061ca5d-56c9-439f-9c0e-210fe06d3992"
29-
FerriteGmsh = "4f95f4f8-b27c-4ae5-9a39-ea55e634e36b"
3027

3128
[targets]
32-
test = ["DelimitedFiles", "FileIO", "IterativeSolvers", "JLD2", "LinearSolve", "Random", "Test","Ferrite","FerriteGmsh","Downloads"]
29+
test = ["DelimitedFiles", "FileIO", "IterativeSolvers", "JLD2", "LinearSolve", "Random", "Test"]

test/lin_elastic_2d.jld2

55.2 KB
Binary file not shown.

test/nns_test.jl

Lines changed: 7 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
using AlgebraicMultigrid
2-
import AlgebraicMultigrid as AMG
3-
using Test
4-
using SparseArrays, LinearAlgebra
5-
using Ferrite, FerriteGmsh, SparseArrays
6-
using Downloads: download
7-
81
## Test QR factorization
92
@testset "fit_candidates unit test cases" begin
103
cases = Vector{Tuple{SparseMatrixCSC{Float64,Int},Matrix{Float64}}}()
@@ -69,138 +62,6 @@ using Downloads: download
6962
end
7063
end
7164

72-
## Test Convergance of AMG for linear elasticity & bending beam
73-
function assemble_external_forces!(f_ext, dh, facetset, facetvalues, prescribed_traction)
74-
# Create a temporary array for the facet's local contributions to the external force vector
75-
fe_ext = zeros(getnbasefunctions(facetvalues))
76-
for facet in FacetIterator(dh, facetset)
77-
# Update the facetvalues to the correct facet number
78-
reinit!(facetvalues, facet)
79-
# Reset the temporary array for the next facet
80-
fill!(fe_ext, 0.0)
81-
# Access the cell's coordinates
82-
cell_coordinates = getcoordinates(facet)
83-
for qp in 1:getnquadpoints(facetvalues)
84-
# Calculate the global coordinate of the quadrature point.
85-
x = spatial_coordinate(facetvalues, qp, cell_coordinates)
86-
tₚ = prescribed_traction(x)
87-
# Get the integration weight for the current quadrature point.
88-
= getdetJdV(facetvalues, qp)
89-
for i in 1:getnbasefunctions(facetvalues)
90-
Nᵢ = shape_value(facetvalues, qp, i)
91-
fe_ext[i] += tₚ Nᵢ *
92-
end
93-
end
94-
# Add the local contributions to the correct indices in the global external force vector
95-
assemble!(f_ext, celldofs(facet), fe_ext)
96-
end
97-
return f_ext
98-
end
99-
100-
function assemble_cell!(ke, cellvalues, C)
101-
for q_point in 1:getnquadpoints(cellvalues)
102-
# Get the integration weight for the quadrature point
103-
= getdetJdV(cellvalues, q_point)
104-
for i in 1:getnbasefunctions(cellvalues)
105-
# Gradient of the test function
106-
∇Nᵢ = shape_gradient(cellvalues, q_point, i)
107-
for j in 1:getnbasefunctions(cellvalues)
108-
# Symmetric gradient of the trial function
109-
∇ˢʸᵐNⱼ = shape_symmetric_gradient(cellvalues, q_point, j)
110-
ke[i, j] += (∇Nᵢ C ∇ˢʸᵐNⱼ) *
111-
end
112-
end
113-
end
114-
return ke
115-
end
116-
117-
function assemble_global!(K, dh, cellvalues, C)
118-
# Allocate the element stiffness matrix
119-
n_basefuncs = getnbasefunctions(cellvalues)
120-
ke = zeros(n_basefuncs, n_basefuncs)
121-
# Create an assembler
122-
assembler = start_assemble(K)
123-
# Loop over all cells
124-
for cell in CellIterator(dh)
125-
# Update the shape function gradients based on the cell coordinates
126-
reinit!(cellvalues, cell)
127-
# Reset the element stiffness matrix
128-
fill!(ke, 0.0)
129-
# Compute element contribution
130-
assemble_cell!(ke, cellvalues, C)
131-
# Assemble ke into K
132-
assemble!(assembler, celldofs(cell), ke)
133-
end
134-
return K
135-
end
136-
137-
function create_nns(dh)
138-
Ndof = ndofs(dh)
139-
grid = dh.grid
140-
B = zeros(Float64, Ndof, 3)
141-
B[1:2:end, 1] .= 1 # x - translation
142-
B[2:2:end, 2] .= 1 # y - translation
143-
144-
# in-plane rotation (x,y) → (-y,x)
145-
coords = reduce(hcat, grid.nodes .|> (n -> n.x |> collect))' # convert nodes to 2d array
146-
y = coords[:, 2]
147-
x = coords[:, 1]
148-
B[1:2:end, 3] .= -y
149-
B[2:2:end, 3] .= x
150-
return B
151-
end
152-
153-
function linear_elasticity_2d()
154-
# Example test: https://ferrite-fem.github.io/Ferrite.jl/stable/tutorials/linear_elasticity/
155-
logo_mesh = "logo.geo"
156-
asset_url = "https://raw.githubusercontent.com/Ferrite-FEM/Ferrite.jl/gh-pages/assets/"
157-
isfile(logo_mesh) || download(string(asset_url, logo_mesh), logo_mesh)
158-
159-
grid = togrid(logo_mesh)
160-
addfacetset!(grid, "top", x -> x[2] 1.0) # facets for which x[2] ≈ 1.0 for all nodes
161-
addfacetset!(grid, "left", x -> abs(x[1]) < 1.0e-6)
162-
addfacetset!(grid, "bottom", x -> abs(x[2]) < 1.0e-6)
163-
164-
dim = 2
165-
order = 1 # linear interpolation
166-
ip = Lagrange{RefTriangle,order}()^dim # vector valued interpolation
167-
168-
qr = QuadratureRule{RefTriangle}(1) # 1 quadrature point
169-
qr_face = FacetQuadratureRule{RefTriangle}(1)
170-
171-
cellvalues = CellValues(qr, ip)
172-
facetvalues = FacetValues(qr_face, ip)
173-
174-
dh = DofHandler(grid)
175-
add!(dh, :u, ip)
176-
close!(dh)
177-
178-
ch = ConstraintHandler(dh)
179-
add!(ch, Dirichlet(:u, getfacetset(grid, "bottom"), (x, t) -> 0.0, 2))
180-
add!(ch, Dirichlet(:u, getfacetset(grid, "left"), (x, t) -> 0.0, 1))
181-
close!(ch)
182-
183-
traction(x) = Vec(0.0, 20.0e3 * x[1])
184-
Emod = 200.0e3 # Young's modulus [MPa]
185-
ν = 0.3 # Poisson's ratio [-]
186-
187-
Gmod = Emod / (2(1 + ν)) # Shear modulus
188-
Kmod = Emod / (3(1 - 2ν)) # Bulk modulus
189-
190-
C = gradient-> 2 * Gmod * dev(ϵ) + 3 * Kmod * vol(ϵ), zero(SymmetricTensor{2,2}))
191-
A = allocate_matrix(dh)
192-
assemble_global!(A, dh, cellvalues, C)
193-
194-
b = zeros(ndofs(dh))
195-
B = create_nns(dh)
196-
assemble_external_forces!(b, dh, getfacetset(grid, "top"), facetvalues, traction)
197-
198-
apply!(A, b, ch)
199-
200-
return A, b, B # return the assembled matrix, force vector, and NNS matrix
201-
end
202-
203-
20465
## Helper functions for cantilever frame beam ##
20566

20667
# Element stiffness matrix
@@ -307,16 +168,18 @@ end
307168

308169
@testset "Mechanics test cases" begin
309170
@testset "Linear elasticity 2D" begin
310-
A, b, B = linear_elasticity_2d()
171+
# load linear elasticity test data
172+
@load "lin_elastic_2d.jld2" A b B
173+
A = SparseMatrixCSC(A.m, A.n, A.colptr, A.rowval, A.nzval)
311174

312175
x_nns, residuals_nns = solve(A, b, SmoothedAggregationAMG(B); log=true, reltol=1e-10)
313176
x_wonns, residuals_wonns = solve(A, b, SmoothedAggregationAMG(); log=true, reltol=1e-10)
314177

315178
ml = smoothed_aggregation(A, B)
316179
@show ml
317180

318-
println("No NNS: final residual at iteration ", length(residuals_wonns), ": ", residuals_nns[end])
319-
println("With NNS: final residual at iteration ", length(residuals_nns), ": ", residuals_wonns[end])
181+
println("No NNS: final residual at iteration ", length(residuals_wonns), ": ", residuals_wonns[end])
182+
println("With NNS: final residual at iteration ", length(residuals_nns), ": ", residuals_nns[end])
320183

321184

322185
#test QR factorization on linear elasticity
@@ -351,8 +214,8 @@ end
351214
x_nns, residuals_nns = solve(A, b, SmoothedAggregationAMG(B); log=true, reltol=1e-10)
352215
x_wonns, residuals_wonns = solve(A, b, SmoothedAggregationAMG(); log=true, reltol=1e-10)
353216

354-
println("No NNS: final residual at iteration ", length(residuals_wonns), ": ", residuals_nns[end])
355-
println("With NNS: final residual at iteration ", length(residuals_nns), ": ", residuals_wonns[end])
217+
println("No NNS: final residual at iteration ", length(residuals_wonns), ": ", residuals_wonns[end])
218+
println("With NNS: final residual at iteration ", length(residuals_nns), ": ", residuals_nns[end])
356219

357220

358221
# test QR factorization on bending beam

0 commit comments

Comments
 (0)