Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP extend Lagrange elements #482

Closed
wants to merge 0 commits into from
Closed

Conversation

edljk
Copy link
Contributor

@edljk edljk commented Sep 19, 2022

  • the file extend_interpolations.jl contains the original version which does not exactly follow Ferrite ordering. The correspondence between reference_coordinates / values can be checked using the test_ip function:
using Ferrite
include("test/test_assembleP3.jl")
ip = Lagrange{2, RefTetrahedron, 3}()
test_ip(ip)
Test Summary:                                    | Pass  Total
test compatibility reference_coordinates / value |  100    100
Test.DefaultTestSet("test compatibility reference_coordinates / value", Any[], 100, false, false)
  • the file test/test_assembleP3.jl also contains an eigenvalue test test_H1Pk which shows that the P3 implementation is not working
julia> test_H1Pk(degfem = 1)
real.(λ) = [-1.1449292170806827e-13, 2.4696521376578597, 2.4696521503592974, 4.948302034886464, 9.90560868039773, 9.905687511076572, 12.394925748673597, 12.427609847958852, 19.95440714026327, 22.38929528455335]
nbdof = 961
error = 0.09060434659715355

julia> test_H1Pk(degfem = 2)
real.(λ) = [-1.6866544239827613e-13, 2.4674015089022836, 2.4674015091765296, 4.9348079086679375, 9.869630512359082, 9.869630512498036, 12.337058505254959, 12.33710273868119, 19.739571819121473, 22.20690628895592]
nbdof = 3721
error = 9.723731949229375e-5

julia> test_H1Pk(degfem = 3)
real.(λ) = [245.3630632864696, 254.07915393517663, 264.6618932884518, 267.5507276351468, 279.10444193168826, 288.83161121361366, 296.2788154801049, 307.1738922427109, 309.6855966007029, 322.50938299450695]
nbdof = 6481
error = 294.8368867413492
Test Failed at test/test_assembleP3.jl:83
  Expression: error < 0.1
   Evaluated: 294.8368867413492 < 0.1
ERROR: There was an error during testing
  • a last file extend_interpolations_Ferriteorder.jl is a tentative to reorder the basis to match Ferrite requirements. At this point it didn't solve the raised by test_H1Pk

@fredrikekre
Copy link
Member

fredrikekre commented Sep 22, 2022

Thanks for the PR!

I think you have just missed the "cell dofs", i.e. dofs that are internal to the cell. (Edit: In practice this meant that you only assembled the first 9 rows/colums of the local matrix even though the full 10x10 matrix was computed correctly. This should have errored IMO, so I opened a PR to check that the size of the matrix matches the number of dofs: #483) Your test function passes with Lagrange{2,RefTetrahedron,3}() with this modification:

diff --git a/src/extend_interpolations_Ferriteorder.jl b/src/extend_interpolations_Ferriteorder.jl
index 4682c693..781c60db 100644
--- a/src/extend_interpolations_Ferriteorder.jl
+++ b/src/extend_interpolations_Ferriteorder.jl
@@ -3,8 +3,9 @@ function Ferrite.getnbasefunctions(::Lagrange{2,RefTetrahedron,order}) where ord
 end

 Ferrite.nvertexdofs(::Lagrange{2,RefTetrahedron,order}) where order = 1
-Ferrite.nedgedofs(::Lagrange{2,RefTetrahedron,order}) where order = order - 1
+# Ferrite.nedgedofs(::Lagrange{2,RefTetrahedron,order}) where order = order - 1
 Ferrite.nfacedofs(::Lagrange{2,RefTetrahedron,order}) where order = order - 1
+Ferrite.ncelldofs(::Lagrange{2,RefTetrahedron,3}) = 1

 # permutation to switch to Ferrite ordering
 permdof2D, permdof2Dinv = Dict{Int, Vector{Int}}(), Dict{Int, Vector{Int}}()

Result:

julia> test_H1Pk(degfem=3)
real.(λ) = [-7.083382139302583e-14, 2.467401100301959, 2.4674011003021055, 4.934802201505752, 9.869604408700198, 9.869604408705166, 12.337005525905012, 12.33700553941171, 19.739209047924067, 22.20661009725714]
nbdof = 8281
error = 3.8050011141876894e-8

@edljk
Copy link
Contributor Author

edljk commented Sep 23, 2022

Nice catch, thank you!

I updated extend_interpolation.jl with the ordering required by Ferrite. The file extend_interpolations_Ferriteorder.jl is not needed anymore. It seems to be ok up to order 5. I am not completely satisfied by the reordering step. The code could be improved but I do not know exactly what would be the most efficient way.

@edljk
Copy link
Contributor Author

edljk commented Sep 23, 2022

.. I just read in MixDofHandler.jl

@assert nfacedofs == 1 "Currently only supports interpolations with nfacedofs = 1"

Is this limitation also for H^1 problems?

@@ -0,0 +1,83 @@
function Ferrite.getnbasefunctions(::Lagrange{2,RefTetrahedron,order}) where order
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this file isn't used anymore, can you remove it?


#-------------------------------------------------------------------------------
# tentative to implement Lagrange element of order k ≥ 3
include("extend_interpolations.jl")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you just add the content of this file directly after the current Lagrange{2,RefTetrahedron} in this file instead.

return
end
#-------------------------------------------------------------------------------
function test_ip(ip)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we have a test function like this in the test_interpolations.jl file, perhaps just test your new interpolations in that file?

@edljk
Copy link
Contributor Author

edljk commented Sep 23, 2022

I made some tests using another library to compare. I think the assembling part for order 4 and 5 may be incorrect. Perhaps due to ncelldofs > 1?

@fredrikekre
Copy link
Member

What did you compare? I don't see anything wrong with this PR now, and dof distribution looks correct.

@codecov-commenter
Copy link

Codecov Report

Base: 91.91% // Head: 90.75% // Decreases project coverage by -1.16% ⚠️

Coverage data is based on head (0a73680) compared to base (bc8a836).
Patch coverage: 6.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #482      +/-   ##
==========================================
- Coverage   91.91%   90.75%   -1.17%     
==========================================
  Files          22       22              
  Lines        3649     3699      +50     
==========================================
+ Hits         3354     3357       +3     
- Misses        295      342      +47     
Impacted Files Coverage Δ
src/interpolations.jl 76.90% <6.00%> (-11.63%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@edljk
Copy link
Contributor Author

edljk commented Oct 6, 2022

I used Getfem++ to assemble mass and stiffness matrices to solve the eigenvalue test problem we already discussed.
With a small mesh I obtain exactly the same error using Ferrite or Getfem++ for P3 elements. For P4 and P5, the error is small BUT different. Moreover the error is greater than the one with P3 using Ferrite with this peace of code..

@fredrikekre
Copy link
Member

Do you integrate it exactly ? The mass matrix with polynomial degree 4 would require Gauss integration of order 9, but we currently only have up to 8:

At least when I run with order 8 for the quadrature I get:

julia> test_H1Pk(degfem = 1)
error = 0.09060434660651495

julia> test_H1Pk(degfem = 2)
error = 9.723732195787704e-5

julia> test_H1Pk(degfem = 3)
error = 3.8048966644055326e-8

julia> test_H1Pk(degfem = 4)
error = 9.816147894525784e-12

for example. degfem = 5 gives a higher error again, but that would require integration of order 13 then.

@edljk
Copy link
Contributor Author

edljk commented Oct 6, 2022

Still the same mistake!
Yes you are right. When I use Getfem++ I chose an exact quadrature order..which may explain the difference

@edljk
Copy link
Contributor Author

edljk commented Oct 6, 2022

I think order 8 is exact for degree 4. Order 10 would be required to be exact for order 5.
It could be the occasion to extend quadrature rules up to order at least 10. Will have a look.

@fredrikekre
Copy link
Member

I think order 8 is exact for degree 4.

IIRC, order n integrates polynomials of order 2n-1 exactly. A mass matrix with 4th order polynomials would be order 16, and thus require order 9 to be exact, right?

@edljk
Copy link
Contributor Author

edljk commented Oct 6, 2022

You are right, I was thinking to the stiffness matrix

@edljk
Copy link
Contributor Author

edljk commented Oct 9, 2022

I tried to follow the style of gaussquad_tri_table.jl to extend Gauss quadrature formula up to order 20 using the python package quadpy.
Does it look reasonable?
More generally, it could perhaps be an easier way to extend quadrature rules in Ferrite.

using PyCall, Printf

quadpy = pyimport("quadpy")

for order in 1:20
    strelseif = order == 1 ? "if" : "elseif"
    qpy = if order < 10 
        quadpy.t2.schemes["dunavant_0$(order)"]()
    else
        quadpy.t2.schemes["dunavant_$(order)"]()
    end
    nw = length(qpy.weights)
    for k = 1:nw 
        if k == 1
            println("$(strelseif) n == $(order)")
            print("xw=[")
        end
        for l = 1:2
            @printf("%1.16f ", qpy.points[l, k])
        end
        @printf("%1.16f / 2.0", qpy.weights[k])
        k == nw ? println("];") : println("")
    end
end

which produces
...
elseif n == 8
xw=[0.3333333333333333 0.3333333333333333 0.1443156076777871 / 2.0
0.4592925882927232 0.4592925882927232 0.0950916342672846 / 2.0
0.1705693077517603 0.1705693077517603 0.1032173705347181 / 2.0
0.0505472283170311 0.0505472283170311 0.0324584976231981 / 2.0
0.4592925882927232 0.0814148234145536 0.0950916342672846 / 2.0
0.1705693077517603 0.6588613844964795 0.1032173705347181 / 2.0
0.0505472283170311 0.8989055433659379 0.0324584976231981 / 2.0
0.0814148234145536 0.4592925882927232 0.0950916342672846 / 2.0
0.6588613844964795 0.1705693077517603 0.1032173705347181 / 2.0
0.8989055433659379 0.0505472283170311 0.0324584976231981 / 2.0
0.0083947774099577 0.2631128296346381 0.0272303141744350 / 2.0
0.7284923929554042 0.0083947774099577 0.0272303141744350 / 2.0
0.2631128296346381 0.7284923929554042 0.0272303141744350 / 2.0
0.2631128296346381 0.0083947774099577 0.0272303141744350 / 2.0
0.7284923929554042 0.2631128296346381 0.0272303141744350 / 2.0
0.0083947774099577 0.7284923929554042 0.0272303141744350 / 2.0];
elseif n == 9
xw=[0.3333333333333333 0.3333333333333333 0.0971357962827987 / 2.0
0.4896825191987376 0.4896825191987376 0.0313347002271392 / 2.0
0.4370895914929365 0.4370895914929365 0.0778275410047742 / 2.0
0.1882035356190327 0.1882035356190327 0.0796477389272102 / 2.0
0.0447295133944527 0.0447295133944527 0.0255776756586980 / 2.0
0.4896825191987376 0.0206349616025249 0.0313347002271392 / 2.0
0.4370895914929365 0.1258208170141270 0.0778275410047742 / 2.0
0.1882035356190327 0.6235929287619346 0.0796477389272102 / 2.0
0.0447295133944527 0.9105409732110945 0.0255776756586980 / 2.0
0.0206349616025249 0.4896825191987376 0.0313347002271392 / 2.0
0.1258208170141270 0.4370895914929365 0.0778275410047742 / 2.0
0.6235929287619346 0.1882035356190327 0.0796477389272102 / 2.0
0.9105409732110945 0.0447295133944527 0.0255776756586980 / 2.0
0.0368384120547363 0.2219629891607658 0.0432835393772893 / 2.0
0.7411985987844980 0.0368384120547363 0.0432835393772893 / 2.0
0.2219629891607658 0.7411985987844980 0.0432835393772893 / 2.0
0.2219629891607658 0.0368384120547363 0.0432835393772893 / 2.0
0.7411985987844980 0.2219629891607658 0.0432835393772893 / 2.0
0.0368384120547363 0.7411985987844980 0.0432835393772893 / 2.0];
elseif n == 10
xw=[0.3333333333333333 0.3333333333333333 0.0908179903827535 / 2.0
0.4855776333836573 0.4855776333836573 0.0367259577564668 / 2.0
0.1094815754850370 0.1094815754850370 0.0453210594355280 / 2.0
0.4855776333836573 0.0288447332326853 0.0367259577564668 / 2.0
0.1094815754850370 0.7810368490299259 0.0453210594355280 / 2.0
0.0288447332326853 0.4855776333836573 0.0367259577564668 / 2.0
0.7810368490299259 0.1094815754850370 0.0453210594355280 / 2.0
0.1417072194148800 0.3079398387641210 0.0727579168454201 / 2.0
0.0250035347626864 0.2466725606399027 0.0283272425310575 / 2.0
0.0095408154002995 0.0668032510122003 0.0094216669637328 / 2.0
0.5503529418209990 0.1417072194148800 0.0727579168454201 / 2.0
0.7283239045974108 0.0250035347626864 0.0283272425310575 / 2.0
0.9236559335875002 0.0095408154002995 0.0094216669637328 / 2.0
0.3079398387641210 0.5503529418209990 0.0727579168454201 / 2.0
0.2466725606399027 0.7283239045974108 0.0283272425310575 / 2.0
0.0668032510122003 0.9236559335875002 0.0094216669637328 / 2.0
0.3079398387641210 0.1417072194148800 0.0727579168454201 / 2.0
0.2466725606399027 0.0250035347626864 0.0283272425310575 / 2.0
0.0668032510122003 0.0095408154002995 0.0094216669637328 / 2.0
0.5503529418209990 0.3079398387641210 0.0727579168454201 / 2.0
0.7283239045974108 0.2466725606399027 0.0283272425310575 / 2.0
0.9236559335875002 0.0668032510122003 0.0094216669637328 / 2.0
0.1417072194148800 0.5503529418209990 0.0727579168454201 / 2.0
0.0250035347626864 0.7283239045974108 0.0283272425310575 / 2.0
0.0095408154002995 0.9236559335875002 0.0094216669637328 / 2.0];
elseif n == 11
xw=[0.5346110482707582 0.5346110482707582 0.0009270063289607 / 2.0
0.3989693029658552 0.3989693029658552 0.0771495349148131 / 2.0
0.2033099004312824 0.2033099004312824 0.0593229773807741 / 2.0
0.1193509122825813 0.1193509122825813 0.0361845405034180 / 2.0
0.0323649481112759 0.0323649481112759 0.0136597310026779 / 2.0
0.5346110482707582 -0.0692220965415165 0.0009270063289607 / 2.0
0.3989693029658552 0.2020613940682896 0.0771495349148131 / 2.0
0.2033099004312824 0.5933801991374351 0.0593229773807741 / 2.0
0.1193509122825813 0.7612981754348375 0.0361845405034180 / 2.0
0.0323649481112759 0.9352701037774482 0.0136597310026779 / 2.0
-0.0692220965415165 0.5346110482707582 0.0009270063289607 / 2.0
0.2020613940682896 0.3989693029658552 0.0771495349148131 / 2.0
0.5933801991374351 0.2033099004312824 0.0593229773807741 / 2.0
0.7612981754348375 0.1193509122825813 0.0361845405034180 / 2.0
0.9352701037774482 0.0323649481112759 0.0136597310026779 / 2.0
0.0501781383104947 0.3566206482612926 0.0523371119622041 / 2.0
0.0210220165361663 0.1714889803040416 0.0207076596391407 / 2.0
0.5932012134282127 0.0501781383104947 0.0523371119622041 / 2.0
0.8074890031597921 0.0210220165361663 0.0207076596391407 / 2.0
0.3566206482612926 0.5932012134282127 0.0523371119622041 / 2.0
0.1714889803040416 0.8074890031597921 0.0207076596391407 / 2.0
0.3566206482612926 0.0501781383104947 0.0523371119622041 / 2.0
0.1714889803040416 0.0210220165361663 0.0207076596391407 / 2.0
0.5932012134282127 0.3566206482612926 0.0523371119622041 / 2.0
0.8074890031597921 0.1714889803040416 0.0207076596391407 / 2.0
0.0501781383104947 0.5932012134282127 0.0523371119622041 / 2.0
0.0210220165361663 0.8074890031597921 0.0207076596391407 / 2.0];
...

@fredrikekre
Copy link
Member

fredrikekre commented Oct 9, 2022

Good idea, perhaps that can be added in a separate PR. (I could not find the license for quadpy, but perhaps it doesn't matter when just using the output?) I will make some updates to this PR and then we can merge it I think.
Edit: found this: sigma-py/quadpy#453 ...
Edit 2: This release: https://zenodo.org/record/5218525 is GPL, which output we can use according to https://www.gnu.org/licenses/gpl-faq.en.html#WhatCaseIsOutputGPL

@fredrikekre
Copy link
Member

Hmm, not sure why GitHub closed the PR when I pushed some tests... I can't reopen it either.

@termi-official
Copy link
Member

I think this is because you cannot push to remote master branches due to ownership conflicts.

@fredrikekre
Copy link
Member

Merged in #512 🎉 This is great to have, thanks for the nice contribution @edljk and sorry for the somewhat slow review process. If you want to follow up by extending the quadrature rules that would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants