Skip to content

Commit

Permalink
Fixing the error computation in the partitioned-heat-equation tutorial (
Browse files Browse the repository at this point in the history
#14)

* working on a fix for the partitioned-heat-equation tutorial

* working on a fix for the partitioned-heat-equation tutorial

* Trying out solutions for L2 error computation from the Discourse

* Error computation in working state

* Formatting

* Calculate relative error instead of absolute error and clean up script

* Formatting

Co-authored-by: Ishaan Desai <ishaandesai@gmail.com>
  • Loading branch information
PhilipHildebrand and IshaanDesai authored Dec 2, 2022
1 parent d7a2491 commit 26678c1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion fenicsxprecice/expression_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class SegregatedRBFInterpolationExpression(CouplingExpression):
"""

def segregated_interpolant_2d(self, coords_x, coords_y, data):
assert(coords_x.shape == coords_y.shape)
assert (coords_x.shape == coords_y.shape)
# create least squares system to approximate a * x ** 2 + b * x + c ~= y

def lstsq_interp(x, y, w): return w[0] * x ** 2 + w[1] * y ** 2 + w[2] * x * y + w[3] * x + w[4] * y + w[5]
Expand Down
22 changes: 12 additions & 10 deletions tutorials/partitioned-heat-conduction/fenicsx/errorcomputation.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from fenics import inner, assemble, dx, project, sqrt
from ufl import dx
from dolfinx.fem import assemble_scalar, form
import numpy as np
from mpi4py import MPI


def compute_errors(u_approx, u_ref, v, total_error_tol=10 ** -4):
# compute pointwise L2 error
error_normalized = (u_ref - u_approx) / u_ref
# project onto function space
error_pointwise = project(abs(error_normalized), v)
# determine L2 norm to estimate total error
error_total = sqrt(assemble(inner(error_pointwise, error_pointwise) * dx))
error_pointwise.rename("error", " ")
def compute_errors(u_approx, u_ref, total_error_tol=10 ** -4):
mesh = u_ref.function_space.mesh

# compute total L2 error between reference and calculated solution
error_pointwise = form(((u_approx - u_ref) / u_ref) ** 2 * dx)
error_total = np.sqrt(mesh.comm.allreduce(assemble_scalar(error_pointwise), MPI.SUM))

assert (error_total < total_error_tol)

return error_total, error_pointwise
# return error_total, error_pointwise
return error_total
24 changes: 12 additions & 12 deletions tutorials/partitioned-heat-conduction/fenicsx/heat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from dolfinx.io import XDMFFile
from ufl import TrialFunction, TestFunction, dx, ds, dot, grad, inner, lhs, rhs, FiniteElement, VectorElement
from fenicsxprecice import Adapter
# from errorcomputation import compute_errors # TODO update do dolfinx
from errorcomputation import compute_errors # TODO update do dolfinx
from my_enums import ProblemType, DomainPart
import argparse
import numpy as np
Expand Down Expand Up @@ -120,7 +120,6 @@ def eval(self, x):
# Define initial value
u_n = Function(V, name="Temperature")
u_n.interpolate(u_D.eval)
# u_n.rename("Temperature", "")

precice, precice_dt, initial_data = None, 0.0, None

Expand Down Expand Up @@ -182,6 +181,7 @@ def eval(self, x):
# reference solution at t=0
u_ref = Function(V, name="reference")
u_ref.interpolate(u_D_function)

'''
# TODO
# mark mesh w.r.t ranks
Expand All @@ -207,8 +207,9 @@ def eval(self, x):
'''

# error_total, error_pointwise = compute_errors(u_n, u_ref, V) # TODO
'''
''
# TODO
'''
error_out << error_pointwise
'''
u_D.t = t + dt.value
Expand Down Expand Up @@ -263,18 +264,17 @@ def eval(self, x):

if precice.is_time_window_complete():
u_ref.interpolate(u_D_function)
# TODO
# error, error_pointwise = compute_errors(u_n, u_ref, V, total_error_tol=error_tol)
# print('n = %d, t = %.2f: L2 error on domain = %.3g' % (n, t, error))
# error, error_pointwise = compute_errors(mesh, u_n, u_ref, total_error_tol=error_tol)
error = compute_errors(u_n, u_ref, total_error_tol=error_tol)
print('n = %d, t = %.2f: L2 error on domain = %.3g' % (n, t, error))
print('output u^%d and u_ref^%d' % (n, n))

xdmf.write_function(u_n, t)
'''
# TODO

# output solution and reference solution at t_n+1
temperature_out << u_n
ref_out << u_ref
error_out << error_pointwise
'''
# temperature_out << u_n
# ref_out << u_ref
# error_out << error_pointwise

# Update Dirichlet BC
u_D.t = t + dt.value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Problem setup for partitioned-heat-conduction/fenics-fenics tutorial
"""
# from dolfinx.fem import Function, VectorFunctionSpace, Expression
from dolfinx.mesh import DiagonalType, create_rectangle
from my_enums import DomainPart
import numpy as np
Expand Down

0 comments on commit 26678c1

Please sign in to comment.