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

add trivial.jl solving the trivial equation f = u #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/trivial.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# In this tutorial we will learn
#
# - How to solve the trivial equation `u = f`
# - How to visualize the solution in pure julia
#
# We want to solve `u = sin`. This equation is trivial, but it showcases how
# the finite element machinery works. The first step is to rephrase it as
# a variational problem:
# ```math
# \int u \cdot v dx = \int sin \cdot v dx
# ```
# for all test functions `v`.
using Gridap
model = CartesianDiscreteModel((0, 2π), 10) # partition the interval (0, 2π) into 10 cells
f(pt) = sin(pt[1])
V0 = TestFESpace(
reffe=:Lagrangian, order=1, valuetype=Float64,
conformity=:H1, model=model)
U = TrialFESpace(V0)
trian = Triangulation(model)
degree = 2
quad = CellQuadrature(trian, degree)
A(u, v) = u ⊙ v
b(v) = v*f
t_Ω = AffineFETerm(A,b,trian,quad)
op = AffineFEOperator(U,V0,t_Ω)
u = solve(op)

# Now that we have a solution, we want to know if it is any good.
# So lets visualize it.
using Plots
xs = map(get_cell_coordinates(trian)) do cell
left, right = cell
1/2*(left[1] + right[1])
end # physical coordinges of the cell centers
q = fill([VectorValue((1/2,))], length(xs)) # reference coordinates of each cell center.
ys = only.(evaluate(u,q)) # solution values at the cell centers
plot(xs, ys, label="solution")
plot!(sin, 0:0.01:2pi, label="truth")