Skip to content

Commit

Permalink
Use SemiNorm (#325)
Browse files Browse the repository at this point in the history
Use H^1 and H^2 `SemiNorm` from SymPDE. Previously the class `Norm`
was used for all seminorms, which was not mathematically correct.

---------

Co-authored-by: Yaman Güçlü <[email protected]>
  • Loading branch information
saidctb and yguclu authored Aug 31, 2023
1 parent a623184 commit 02d1bf7
Show file tree
Hide file tree
Showing 26 changed files with 108 additions and 110 deletions.
25 changes: 12 additions & 13 deletions psydac/api/ast/tests/poisson.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
# -*- coding: UTF-8 -*-

import os

from sympy import symbols
from sympy import sin,pi
from sympy import sin, pi

from sympde.calculus import grad, dot

from sympde.topology import ScalarFunctionSpace
from sympde.topology import elements_of,LogicalExpr
from sympde.topology import elements_of, LogicalExpr
from sympde.topology import Square
from sympde.topology import IdentityMapping,Mapping ,PolarMapping
from sympde.topology import Mapping, IdentityMapping, PolarMapping
from sympde.expr import integral
from sympde.expr import LinearForm
from sympde.expr import BilinearForm
from sympde.expr import Norm
from sympde.expr import Norm, SemiNorm
from sympde.expr.evaluation import TerminalExpr

from psydac.api.ast.fem import AST
from psydac.api.ast.parser import parse
from psydac.api.discretization import discretize
from psydac.api.printing.pycode import pycode

import os
# ...
try:
mesh_dir = os.environ['PSYDAC_MESH_DIR']
Expand All @@ -32,6 +30,7 @@
mesh_dir = os.path.join(base_dir, 'mesh')
filename = os.path.join(mesh_dir, 'identity_2d.h5')


def test_codegen():
domain = Square()
M = Mapping('M',2)
Expand All @@ -51,22 +50,22 @@ def test_codegen():
Vh = discretize(V, domain_h)

error = u - sin(pi*x)*sin(pi*y)
l2norm = LogicalExpr(M, Norm(error, domain, kind='l2'))
h1norm = LogicalExpr(M, Norm(error, domain, kind='h1'))
l2norm = LogicalExpr(M, Norm(error, domain, kind='l2'))
h1norm = LogicalExpr(M, SemiNorm(error, domain, kind='h1'))

ast_b = AST(b, TerminalExpr(b)[0],[Vh, Vh])
ast_b = parse(ast_b.expr, settings={'dim':2,'nderiv':1,'mapping':M,'target':domain.logical_domain})
ast_b = parse(ast_b.expr, settings={'dim':2, 'nderiv':1, 'mapping':M, 'target':domain.logical_domain})
print(pycode(ast_b))

print('==============================================================================================================')
ast_l = AST(l, TerminalExpr(l)[0], Vh)
ast_l = parse(ast_l.expr, settings={'dim':2,'nderiv':1,'mapping':M,'target':domain.logical_domain})
ast_l = parse(ast_l.expr, settings={'dim':2, 'nderiv':1, 'mapping':M, 'target':domain.logical_domain})
print(pycode(ast_l))


print('==============================================================================================================')
ast_l2norm = AST(l2norm, TerminalExpr(l2norm)[0], Vh)
ast_l2norm = parse(ast_l2norm.expr, settings={'dim':2,'nderiv':1,'mapping':M, 'target':domain.logical_domain})
ast_l2norm = parse(ast_l2norm.expr, settings={'dim':2, 'nderiv':1, 'mapping':M, 'target':domain.logical_domain})
print(pycode(ast_l2norm))
test_codegen()

test_codegen()
26 changes: 12 additions & 14 deletions psydac/api/ast/tests/system_1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: UTF-8 -*-
import os

from sympy import pi, sin, Tuple, Matrix

Expand All @@ -10,8 +11,7 @@
from sympde.expr import integral
from sympde.expr import LinearForm
from sympde.expr import BilinearForm
from sympde.expr import Norm

from sympde.expr import Norm, SemiNorm
from sympde.expr.evaluation import TerminalExpr

from psydac.api.ast.fem import AST
Expand All @@ -20,7 +20,6 @@
from psydac.api.printing.pycode import pycode


import os
# ...
try:
mesh_dir = os.environ['PSYDAC_MESH_DIR']
Expand All @@ -39,7 +38,7 @@ def test_codegen():

x,y = domain.coordinates

f = Tuple(2*pi**2*sin(pi*x)*sin(pi*y),
f = Tuple(2*pi**2*sin(pi*x)*sin(pi*y),
2*pi**2*sin(pi*x)*sin(pi*y))

Fe = Tuple(sin(pi*x)*sin(pi*y), sin(pi*x)*sin(pi*y))
Expand All @@ -50,12 +49,12 @@ def test_codegen():

int_0 = lambda expr: integral(domain , expr)

b = BilinearForm((u,v), int_0(inner(grad(v), grad(u))))
b = BilinearForm((u, v), int_0(inner(grad(v), grad(u))))
l = LinearForm(v, int_0(dot(f, v)))

error = Matrix([F[0]-Fe[0], F[1]-Fe[1]])
l2norm_F = Norm(error, domain, kind='l2')
h1norm_F = Norm(error, domain, kind='h1')
l2norm_F = Norm(error, domain, kind='l2')
h1norm_F = SemiNorm(error, domain, kind='h1')

# Create computational domain from topological domain
domain_h = discretize(domain, filename=filename)
Expand All @@ -64,17 +63,16 @@ def test_codegen():
Vh = discretize(V, domain_h)

print('============================================BilinearForm=========================================')
ast_b = AST(b, TerminalExpr(b)[0], [Vh, Vh])
stmt_b = parse(ast_b.expr, settings={'dim':2,'nderiv':1, 'mapping':Vh.symbolic_mapping})
ast_b = AST(b, TerminalExpr(b)[0], [Vh, Vh])
stmt_b = parse(ast_b.expr, settings={'dim':2, 'nderiv':1, 'mapping':Vh.symbolic_mapping})
print(pycode(stmt_b))

print('============================================LinearForm===========================================')
ast_l = AST(l, TerminalExpr(l)[0], Vh)
stmt_l = parse(ast_l.expr, settings={'dim':2,'nderiv':1, 'mapping':Vh.symbolic_mapping})
ast_l = AST(l, TerminalExpr(l)[0], Vh)
stmt_l = parse(ast_l.expr, settings={'dim':2, 'nderiv':1, 'mapping':Vh.symbolic_mapping})
print(pycode(stmt_l))

print('============================================Norm===========================================')
print('============================================SemiNorm===========================================')
ast_norm = AST(h1norm_F, TerminalExpr(h1norm_F)[0], Vh)
stmt_n = parse(ast_norm.expr, settings={'dim':2,'nderiv':1, 'mapping':Vh.symbolic_mapping})
stmt_n = parse(ast_norm.expr, settings={'dim':2, 'nderiv':1, 'mapping':Vh.symbolic_mapping})
print(pycode(stmt_n))

10 changes: 5 additions & 5 deletions psydac/api/discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# TODO: - init_fem is called whenever we call discretize. we should check that
# nderiv has not been changed. shall we add nquads too?
import os

from sympy import Expr as sym_Expr
import numpy as np

Expand All @@ -11,7 +12,7 @@
from sympde.expr import LinearForm as sym_LinearForm
from sympde.expr import Functional as sym_Functional
from sympde.expr import Equation as sym_Equation
from sympde.expr import Norm as sym_Norm
from sympde.expr import Norm as sym_Norm, SemiNorm as sym_SemiNorm
from sympde.expr import TerminalExpr

from sympde.topology import BasicFunctionSpace
Expand Down Expand Up @@ -39,9 +40,8 @@
from psydac.fem.vector import ProductFemSpace, VectorFemSpace
from psydac.cad.geometry import Geometry
from psydac.mapping.discrete import NurbsMapping

from psydac.linalg.stencil import StencilVectorSpace
from psydac.linalg.block import BlockVectorSpace
from psydac.linalg.stencil import StencilVectorSpace
from psydac.linalg.block import BlockVectorSpace

__all__ = ('discretize', 'discretize_derham', 'reduce_space_degrees', 'discretize_space', 'discretize_domain')

Expand Down Expand Up @@ -445,7 +445,7 @@ def discretize(a, *args, **kwargs):
kwargs['symbolic_mapping'] = mapping

if isinstance(a, sym_BasicForm):
if isinstance(a, sym_Norm):
if isinstance(a, (sym_Norm, sym_SemiNorm)):
kernel_expr = TerminalExpr(a, domain)
if not mapping is None:
kernel_expr = tuple(LogicalExpr(i, domain) for i in kernel_expr)
Expand Down
5 changes: 3 additions & 2 deletions psydac/api/fem.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sympde.expr import LinearForm as sym_LinearForm
from sympde.expr import Functional as sym_Functional
from sympde.expr import Norm as sym_Norm
from sympde.expr import SemiNorm as sym_SemiNorm
from sympde.topology import Boundary, Interface
from sympde.topology import VectorFunctionSpace
from sympde.topology import ProductSpace
Expand Down Expand Up @@ -1525,7 +1526,7 @@ def assemble(self, **kwargs):
Example
--------------
n = Norm(1.0j*v, domain, kind='l2')
n = SemiNorm(1.0j*v, domain, kind='l2')
nh = discretize(n, domain_h, Vh , **kwargs)
fh = FemField(Vh)
fh.coeffs[:] = 1
Expand Down Expand Up @@ -1555,7 +1556,7 @@ def assemble(self, **kwargs):
args += (v, )

v = self._func(*args)
if isinstance(self.expr, sym_Norm):
if isinstance(self.expr, (sym_Norm, sym_SemiNorm)):
if not( self.comm is None ):
v = self.comm.allreduce(sendobj=v)

Expand Down
8 changes: 4 additions & 4 deletions psydac/api/tests/test_2d_biharmonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sympde.topology import Square
from sympde.topology import Union
from sympde.expr import BilinearForm, LinearForm, integral
from sympde.expr import Norm
from sympde.expr import Norm, SemiNorm
from sympde.expr import find, EssentialBC

from psydac.api.discretization import discretize
Expand Down Expand Up @@ -74,9 +74,9 @@ def run_biharmonic_2d_dir(solution, f, dir_zero_boundary, ncells, degree, backen

# Error norms
error = u - solution
l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
h2norm = Norm(error, domain, kind='h2')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')
h2norm = SemiNorm(error, domain, kind='h2')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down
16 changes: 8 additions & 8 deletions psydac/api/tests/test_2d_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sympde.topology import Domain, Square
from sympde.topology import IdentityMapping, AffineMapping, PolarMapping
from sympde.expr import BilinearForm, LinearForm, integral
from sympde.expr import Norm
from sympde.expr import Norm, SemiNorm
from sympde.expr import find, EssentialBC

from psydac.api.discretization import discretize
Expand Down Expand Up @@ -87,9 +87,9 @@ def run_biharmonic_2d_dir(solution, f, dir_zero_boundary, ncells=None, degree=No

# Error norms
error = u - solution
l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
h2norm = Norm(error, domain, kind='h2')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')
h2norm = SemiNorm(error, domain, kind='h2')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down Expand Up @@ -170,8 +170,8 @@ def run_poisson_2d(solution, f, domain, ncells=None, degree=None, filename=None,

equation = find(u, forall=v, lhs=1j*a(u,v), rhs=1j*l(v), bc=bc)

l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down Expand Up @@ -223,8 +223,8 @@ def run_helmholtz_2d(solution, kappa, e_w_0, dx_e_w_0, domain, ncells=None, degr

equation = find(u, forall=v, lhs=a(u,v), rhs=l(v))

l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down
6 changes: 3 additions & 3 deletions psydac/api/tests/test_2d_laplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sympde.topology import Square
from sympde.topology import Union
from sympde.expr import BilinearForm, LinearForm, integral
from sympde.expr import Norm
from sympde.expr import Norm, SemiNorm
from sympde.expr import find, EssentialBC

from psydac.api.discretization import discretize
Expand Down Expand Up @@ -78,8 +78,8 @@ def run_laplace_2d(solution, f, dir_zero_boundary, dir_nonzero_boundary,

# Error norms
error = u - solution
l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down
8 changes: 4 additions & 4 deletions psydac/api/tests/test_2d_mapping_biharmonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from sympde.topology import Domain,Square
from sympde.topology import Union
from sympde.expr import BilinearForm, LinearForm, integral
from sympde.expr import Norm
from sympde.expr import Norm, SemiNorm
from sympde.expr import find, EssentialBC

from psydac.api.discretization import discretize
Expand Down Expand Up @@ -100,9 +100,9 @@ def run_biharmonic_2d_dir(filename, solution, f, dir_zero_boundary,

# Error norms
error = u - solution
l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
h2norm = Norm(error, domain, kind='h2')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')
h2norm = SemiNorm(error, domain, kind='h2')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down
6 changes: 3 additions & 3 deletions psydac/api/tests/test_2d_mapping_laplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from sympde.topology import Domain,Square
from sympde.topology import Union
from sympde.expr import BilinearForm, LinearForm, integral
from sympde.expr import Norm
from sympde.expr import Norm, SemiNorm
from sympde.expr import find, EssentialBC

from psydac.api.discretization import discretize
Expand Down Expand Up @@ -101,8 +101,8 @@ def run_laplace_2d(filename, solution, f, dir_zero_boundary,

# Error norms
error = u - solution
l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down
6 changes: 3 additions & 3 deletions psydac/api/tests/test_2d_mapping_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from sympde.topology import Domain
from sympde.topology import Union
from sympde.expr import BilinearForm, LinearForm, integral
from sympde.expr import Norm
from sympde.expr import Norm, SemiNorm
from sympde.expr import find, EssentialBC

from psydac.api.discretization import discretize
Expand Down Expand Up @@ -106,8 +106,8 @@ def run_poisson_2d(filename, solution, f, dir_zero_boundary,

# Error norms
error = u - solution
l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down
6 changes: 3 additions & 3 deletions psydac/api/tests/test_2d_multipatch_mapping_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sympde.topology import IdentityMapping, PolarMapping, AffineMapping
from sympde.expr.expr import LinearForm, BilinearForm
from sympde.expr.expr import integral
from sympde.expr.expr import Norm
from sympde.expr.expr import Norm, SemiNorm
from sympde.expr.equation import find, EssentialBC

from psydac.api.discretization import discretize
Expand Down Expand Up @@ -76,8 +76,8 @@ def run_poisson_2d(solution, f, domain, ncells=None, degree=None, filename=None,

equation = find(u, forall=v, lhs=a(u,v), rhs=l(v))

l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down
6 changes: 3 additions & 3 deletions psydac/api/tests/test_2d_multipatch_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sympde.topology import IdentityMapping, AffineMapping
from sympde.expr.expr import LinearForm, BilinearForm
from sympde.expr.expr import integral
from sympde.expr.expr import Norm
from sympde.expr.expr import Norm, SemiNorm
from sympde.expr.equation import find, EssentialBC

from psydac.api.discretization import discretize
Expand Down Expand Up @@ -53,8 +53,8 @@ def run_poisson_2d(solution, f, domain, ncells, degree):

equation = find(u, forall=v, lhs=a(u,v), rhs=l(v), bc=bc)

l2norm = Norm(error, domain, kind='l2')
h1norm = Norm(error, domain, kind='h1')
l2norm = Norm(error, domain, kind='l2')
h1norm = SemiNorm(error, domain, kind='h1')

#+++++++++++++++++++++++++++++++
# 2. Discretization
Expand Down
Loading

0 comments on commit 02d1bf7

Please sign in to comment.