def test_2d_block_3(): print('============== test_2d_block_3 ================') x, y = symbols('x y') u = Symbol('u') v = Symbol('v') epsilon = Constant('epsilon') Laplace = lambda v, u: Dot(Grad(v), Grad(u)) Mass = lambda v, u: v * u u1, u2, p = symbols('u1 u2 p') v1, v2, q = symbols('v1 v2 q') a = Lambda((x, y, v1, v2, q, u1, u2, p), Laplace(v1, u1) - dx(v1) * p + Laplace(v2, u2) - dy(v2) * p + q * (dx(u1) + dy(u2)) + epsilon * Mass(q, p)) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V = TensorFemSpace(V1, V2) V = VectorFemSpace(V, V, V) # ... # ... kernel_py = compile_kernel('kernel_block_3', a, V, d_args={'epsilon': 'double'}, backend='python') kernel_f90 = compile_kernel('kernel_block_3', a, V, d_args={'epsilon': 'double'}, backend='fortran') M_py = assemble_matrix(V, kernel_py, args={'epsilon': 1.e-3}) M_f90 = assemble_matrix(V, kernel_f90, args={'epsilon': 1.e-3}) # ... assert_identical_coo(M_py, M_f90) print('')
def test_3d_block_2(): print('============== test_3d_block_2 ================') x, y, z = symbols('x y z') u = IndexedBase('u') v = IndexedBase('v') a = Lambda((x, y, z, v, u), Dot(Curl(u), Curl(v)) + 0.2 * Dot(u, v)) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 p3 = 2 ne1 = 2 ne2 = 2 ne3 = 2 # ... print('> Grid :: [{},{},{}]'.format(ne1, ne2, ne3)) print('> Degree :: [{},{},{}]'.format(p1, p2, p3)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) grid_3 = linspace(0., 1., ne3 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V3 = SplineSpace(p3, grid=grid_3) Vx = TensorFemSpace(V1, V2, V3) Vy = TensorFemSpace(V1, V2, V3) Vz = TensorFemSpace(V1, V2, V3) V = VectorFemSpace(Vx, Vy, Vz) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... symbol_f90 = compile_symbol('symbol_block_2', a, V, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne1 + 1) t2 = linspace(-pi, pi, ne2 + 1) t3 = linspace(-pi, pi, ne3 + 1) x1 = linspace(0., 1., ne1 + 1) x2 = linspace(0., 1., ne2 + 1) x3 = linspace(0., 1., ne3 + 1) e = zeros((3, 3, ne1 + 1, ne2 + 1, ne3 + 1), order='F') symbol_f90(x1, x2, x3, t1, t2, t3, e) # ... print('')
def test_3d_scalar_2(): print('============== test_3d_scalar_2 ================') # ... define the weak formulation x, y, z = symbols('x y z') u = Symbol('u') v = Symbol('v') alpha = Constant('alpha') nu = Constant('nu') a = Lambda((x, v, u), alpha * Dot(Grad(u), Grad(v)) + nu * u * v) # ... # ... create a finite element space p1 = 2 p2 = 2 p3 = 2 ne1 = 2 ne2 = 2 ne3 = 2 # ... print('> Grid :: [{},{},{}]'.format(ne1, ne2, ne3)) print('> Degree :: [{},{},{}]'.format(p1, p2, p3)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) grid_3 = linspace(0., 1., ne3 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V3 = SplineSpace(p3, grid=grid_3) V = TensorFemSpace(V1, V2, V3) # ... # ... kernel_py = compile_kernel('kernel_scalar_2', a, V, d_constants={'nu': 0.1}, d_args={'alpha': 'double'}, backend='python') kernel_f90 = compile_kernel('kernel_scalar_2', a, V, d_constants={'nu': 0.1}, d_args={'alpha': 'double'}, backend='fortran') M_py = assemble_matrix(V, kernel_py, args={'alpha': 2.0}) M_f90 = assemble_matrix(V, kernel_f90, args={'alpha': 2.0}) # ... assert_identical_coo(M_py, M_f90)
def test_3d_scalar_4(): print('============== test_3d_scalar_4 ================') x, y, z = symbols('x y z') u = Symbol('u') v = Symbol('v') a = Lambda( (x, y, z, v, u), dx(dx(u)) * dx(dx(v)) + dy(dy(u)) * dy(dy(v)) + dz(dz(u)) * dz(dz(v))) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 p3 = 2 ne1 = 2 ne2 = 2 ne3 = 2 # ... print('> Grid :: [{},{},{}]'.format(ne1, ne2, ne3)) print('> Degree :: [{},{},{}]'.format(p1, p2, p3)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) grid_3 = linspace(0., 1., ne3 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V3 = SplineSpace(p3, grid=grid_3) V = TensorFemSpace(V1, V2, V3) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... symbol_f90 = compile_symbol('symbol_scalar_4', a, V, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne1 + 1) t2 = linspace(-pi, pi, ne2 + 1) t3 = linspace(-pi, pi, ne3 + 1) x1 = linspace(0., 1., ne1 + 1) x2 = linspace(0., 1., ne2 + 1) x3 = linspace(0., 1., ne3 + 1) e = zeros((ne1 + 1, ne2 + 1, ne3 + 1), order='F') symbol_f90(x1, x2, x3, t1, t2, t3, e) # ... print('')
def test_2d_block_1(): print('============== test_2d_block_1 ================') x, y = symbols('x y') u = IndexedBase('u') v = IndexedBase('v') a = Lambda((x, y, v, u), Rot(u) * Rot(v) + Div(u) * Div(v) + 0.2 * Dot(u, v)) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) Vx = TensorFemSpace(V1, V2) Vy = TensorFemSpace(V1, V2) V = VectorFemSpace(Vx, Vy) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... symbol_f90 = compile_symbol('symbol_block_2', a, V, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne1 + 1) t2 = linspace(-pi, pi, ne2 + 1) x1 = linspace(0., 1., ne1 + 1) x2 = linspace(0., 1., ne2 + 1) e = zeros((2, 2, ne1 + 1, ne2 + 1), order='F') symbol_f90(x1, x2, t1, t2, e) # ... print('')
def test_3d_block_2(): print('============== test_3d_block_2 ================') x, y, z = symbols('x y z') u = IndexedBase('u') v = IndexedBase('v') F = Field('F') a = Lambda((x, y, z, v, u), Dot(Curl(u), Curl(v)) + 0.2 * Dot(u, v) + F * u[0] * v[0]) # ... create a finite element space p1 = 2 p2 = 2 p3 = 2 ne1 = 2 ne2 = 2 ne3 = 2 print('> Grid :: [{},{},{}]'.format(ne1, ne2, ne3)) print('> Degree :: [{},{},{}]'.format(p1, p2, p3)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) grid_3 = linspace(0., 1., ne3 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V3 = SplineSpace(p3, grid=grid_3) W = TensorFemSpace(V1, V2, V3) # ... # ... vector space V = VectorFemSpace(W, W, W) # ... F = Spline(W) F.coeffs._data[:, :, :] = 1. # ... kernel_py = compile_kernel('kernel_block_2', a, V, backend='python') kernel_f90 = compile_kernel('kernel_block_2', a, V, backend='fortran') M_py = assemble_matrix(V, kernel_py, fields={'F': F}) M_f90 = assemble_matrix(V, kernel_f90, fields={'F': F}) # ... assert_identical_coo(M_py, M_f90)
def test_2d_block_2(): print('============== test_2d_block_2 ================') # ... define the weak formulation x, y = symbols('x y') u = IndexedBase('u') v = IndexedBase('v') F = Field('F') a = Lambda( (x, y, v, u), Rot(u) * Rot(v) + Div(u) * Div(v) + 0.2 * Dot(u, v) + F * u[0] * v[0]) # ... # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) W = TensorFemSpace(V1, V2) # ... # ... vector space V = VectorFemSpace(W, W) # ... F = Spline(W) F.coeffs._data[:, :] = 1. # ... kernel_py = compile_kernel('kernel_block_2', a, V, backend='python') kernel_f90 = compile_kernel('kernel_block_2', a, V, backend='fortran') M_py = assemble_matrix(V, kernel_py, fields={'F': F}) M_f90 = assemble_matrix(V, kernel_f90, fields={'F': F}) # ... assert_identical_coo(M_py, M_f90)
def creationSpaces(degree, grid): """ Create the schoenberg spaces of degree @degree (and @degree-1) Parameters ---------- @degree: int degree of the first schoenberg space @grid : list of float or numpy.ndarray breakpoints of the domain Returns ------- V : Class SplineSpace 1D finite element space of schoenberg space of degree @degree W: Class SplineSpace 1D finite element space of schoenberg space of degree (@degree - 1) """ V = SplineSpace(degree, grid=grid) W = SplineSpace(degree - 1, grid=grid) V.init_fem(quad_order=degree + 1) W.init_fem(quad_order=degree + 1) return V, W
def test_3d_scalar_4(): print('============== test_3d_scalar_4 ================') # ... define the weak formulation x, y, z = symbols('x y z') u = Symbol('u') v = Symbol('v') F = Field('F') a = Lambda((x, y, z, v, u), Dot(Grad(F * u), Grad(v)) + u * v) # ... # ... create a finite element space p1 = 2 p2 = 2 p3 = 2 ne1 = 2 ne2 = 2 ne3 = 2 # ... print('> Grid :: [{},{},{}]'.format(ne1, ne2, ne3)) print('> Degree :: [{},{},{}]'.format(p1, p2, p3)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) grid_3 = linspace(0., 1., ne3 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V3 = SplineSpace(p3, grid=grid_3) V = TensorFemSpace(V1, V2, V3) # ... F = Spline(V) F.coeffs._data[:, :, :] = 1. # ... kernel_py = compile_kernel('kernel_scalar_4', a, V, backend='python') kernel_f90 = compile_kernel('kernel_scalar_4', a, V, backend='fortran') M_py = assemble_matrix(V, kernel_py, fields={'F': F}) M_f90 = assemble_matrix(V, kernel_f90, fields={'F': F}) # ... assert_identical_coo(M_py, M_f90)
def test_2d_scalar_5(): print('============== test_2d_scalar_5 ================') x, y = symbols('x y') u = Symbol('u') v = Symbol('v') a = Lambda((x, y, v, u), dx(dx(u)) * dx(dx(v)) + dy(dy(u)) * dy(dy(v))) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V = TensorFemSpace(V1, V2) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... symbol_f90 = compile_symbol('symbol_scalar_5', a, V, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne1 + 1) t2 = linspace(-pi, pi, ne2 + 1) x1 = linspace(0., 1., ne1 + 1) x2 = linspace(0., 1., ne2 + 1) e = zeros((ne1 + 1, ne2 + 1), order='F') symbol_f90(x1, x2, t1, t2, e) # ... print('')
def test_1d_scalar_1(): print('============== test_1d_scalar_1 ================') # ... define the weak formulation x = Symbol('x') u = Symbol('u') v = Symbol('v') a = Lambda((x, v, u), Dot(Grad(u), Grad(v)) + u * v) # ... # ... create a finite element space p = 3 ne = 64 print('> Grid :: {ne}'.format(ne=ne)) print('> Degree :: {p}'.format(p=p)) grid = linspace(0., 1., ne + 1) V = SplineSpace(p, grid=grid) # ... # ... kernel_py = compile_kernel('kernel_scalar_1', a, V, backend='python') kernel_f90 = compile_kernel('kernel_scalar_1', a, V, backend='fortran') M_py = assemble_matrix(V, kernel_py) M_f90 = assemble_matrix(V, kernel_f90) # ... assert_identical_coo(M_py, M_f90)
def test_pdes_2d_2(): print('============ test_pdes_2d_2 =============') # ... abstract model V = H1Space('V', ldim=2) v = TestFunction(V, name='v') u = TestFunction(V, name='u') c = Constant('c', real=True, label='mass stabilization') a = BilinearForm((v, u), dot(grad(v), grad(u)) + c * v * u) # ... # ... discretization # Input data: degree, number of elements p1 = 1 p2 = 1 ne1 = 4 ne2 = 4 # Create uniform grid grid_1 = linspace(0., 1., num=ne1 + 1) grid_2 = linspace(0., 1., num=ne2 + 1) # Create 1D finite element spaces and precompute quadrature data V1 = SplineSpace(p1, grid=grid_1) V1.init_fem() V2 = SplineSpace(p2, grid=grid_2) V2.init_fem() # Create 2D tensor product finite element space V = TensorFemSpace(V1, V2) # ... # ... discretize_symbol(a, [V, V]) # print(mass.symbol.__doc__) # ... # ... n1 = 21 n2 = 21 t1 = linspace(-pi, pi, n1) t2 = linspace(-pi, pi, n2) x1 = linspace(0., 1., n1) x2 = linspace(0., 1., n2) xs = [x1, x2] ts = [t1, t2] e = a.symbol(*xs, *ts, 0.25) print('> c = 0.25 :: ', e.min(), e.max()) e = a.symbol(*xs, *ts, 0.6) print('> c = 0.6 :: ', e.min(), e.max())
def test_3d_scalar_5(): print('============== test_3d_scalar_5 ================') # ... define the weak formulation x, y, z = symbols('x y z') u = Symbol('u') v = Symbol('v') a = Lambda((x, y, z, v, u), dx(dx(u)) * dx(dx(v)) + dy(dy(u)) * dy(dy(v)) + dz(dz(u)) * dz(dz(v)) + Dot(Grad(u), Grad(v)) + u * v) # ... # ... create a finite element space p1 = 2 p2 = 2 p3 = 2 ne1 = 2 ne2 = 2 ne3 = 2 # ... print('> Grid :: [{},{},{}]'.format(ne1, ne2, ne3)) print('> Degree :: [{},{},{}]'.format(p1, p2, p3)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) grid_3 = linspace(0., 1., ne3 + 1) V1 = SplineSpace(p1, grid=grid_1, nderiv=2) V2 = SplineSpace(p2, grid=grid_2, nderiv=2) V3 = SplineSpace(p3, grid=grid_3, nderiv=2) V = TensorFemSpace(V1, V2, V3) # ... # ... kernel_py = compile_kernel('kernel_scalar_5', a, V, backend='python') kernel_f90 = compile_kernel('kernel_scalar_5', a, V, backend='fortran') M_py = assemble_matrix(V, kernel_py) M_f90 = assemble_matrix(V, kernel_f90) # ... assert_identical_coo(M_py, M_f90)
def test_1d_block_1(): print('============== test_1d_block_1 ================') x = Symbol('x') u0, u1 = symbols('u0 u1') v0, v1 = symbols('v0 v1') a = Lambda((x, v0, v1, u0, u1), dx(u0) * dx(v0) + dx(u1) * v0 + u0 * dx(v1) + u1 * v1) print('> input := {0}'.format(a)) # ... create a finite element space p = 3 ne = 64 print('> Grid :: {ne}'.format(ne=ne)) print('> Degree :: {p}'.format(p=p)) grid = linspace(0., 1., ne + 1) V1 = SplineSpace(p, grid=grid) V2 = SplineSpace(p, grid=grid) V = VectorFemSpace(V1, V2) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... symbol_f90 = compile_symbol('symbol_block_1', a, V, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne + 1) x1 = linspace(0., 1., ne + 1) e = zeros((2, 2, ne + 1)) symbol_f90(x1, t1, e) # ... print('')
def test_2d_scalar_6(): print('============== test_2d_scalar_6 ================') # ... define the weak formulation x, y = symbols('x y') u = Symbol('u') v = Symbol('v') a = Lambda((x, y, v, u), dx(dx(u)) * dx(dx(v)) + dy(dy(u)) * dy(dy(v)) + Dot(Grad(u), Grad(v)) + u * v) # ... # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1, nderiv=2) V2 = SplineSpace(p2, grid=grid_2, nderiv=2) V = TensorFemSpace(V1, V2) # ... # ... kernel_py = compile_kernel('kernel_scalar_6', a, V, backend='python') kernel_f90 = compile_kernel('kernel_scalar_6', a, V, backend='fortran') M_py = assemble_matrix(V, kernel_py) M_f90 = assemble_matrix(V, kernel_f90) # ... assert_identical_coo(M_py, M_f90)
def test_pdes_1d_1(): print('============ test_pdes_1d_1 =============') # ... abstract model V = H1Space('V', ldim=1) v = TestFunction(V, name='v') u = TestFunction(V, name='u') a = BilinearForm((v,u), dot(grad(v), grad(u))) # ... # ... discretization # Input data: degree, number of elements p1 = 1 ne1 = 4 # Create uniform grid grid_1 = linspace( 0., 1., num=ne1+1 ) # Create 1D finite element spaces and precompute quadrature data V = SplineSpace( p1, grid=grid_1 ); V.init_fem() # ... # ... discretize_symbol( a, [V,V] ) # print(mass.symbol.__doc__) # ... # ... n1 = 21 t1 = linspace(-pi, pi, n1) x1 = linspace(0.,1., n1) xs = [x1] ts = [t1] e = a.symbol(*xs, *ts) print('> ', e.min(), e.max())
def test_1d_scalar_2(): print('============== test_1d_scalar_2 ================') # ... define the weak formulation x = Symbol('x') u = Symbol('u') v = Symbol('v') alpha = Constant('alpha') nu = Constant('nu') a = Lambda((x, v, u), alpha * Dot(Grad(u), Grad(v)) + nu * u * v) # ... # ... create a finite element space p = 3 ne = 64 print('> Grid :: {ne}'.format(ne=ne)) print('> Degree :: {p}'.format(p=p)) grid = linspace(0., 1., ne + 1) V = SplineSpace(p, grid=grid) # ... # ... kernel_py = compile_kernel('kernel_scalar_2', a, V, d_constants={'nu': 0.1}, d_args={'alpha': 'double'}, backend='python') kernel_f90 = compile_kernel('kernel_scalar_2', a, V, d_constants={'nu': 0.1}, d_args={'alpha': 'double'}, backend='fortran') M_py = assemble_matrix(V, kernel_py, args={'alpha': 2.0}) M_f90 = assemble_matrix(V, kernel_f90, args={'alpha': 2.0}) # ... assert_identical_coo(M_py, M_f90)
def test_1d_scalar_2(): print('============== test_1d_scalar_2 ================') x = Symbol('x') u = Symbol('u') v = Symbol('v') b = Constant('b') a = Lambda((x, v, u), Dot(Grad(b * u), Grad(v)) + u * v) print('> input := {0}'.format(a)) # ... create a finite element space p = 3 ne = 64 print('> Grid :: {ne}'.format(ne=ne)) print('> Degree :: {p}'.format(p=p)) grid = linspace(0., 1., ne + 1) V = SplineSpace(p, grid=grid) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... symbol_f90 = compile_symbol('symbol_scalar_2', a, V, d_constants={'b': 0.1}, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne + 1) x1 = linspace(0., 1., ne + 1) e = zeros(ne + 1) symbol_f90(x1, t1, e) # ... print('')
def test_1d_block_1(): print('============== test_1d_block_1 ================') # ... define the weak formulation x = Symbol('x') u0, u1 = symbols('u0 u1') v0, v1 = symbols('v0 v1') a = Lambda((x, v0, v1, u0, u1), dx(u0) * dx(v0) + dx(u1) * v0 + u0 * dx(v1) + u1 * v1) # ... # ... create a finite element space p = 3 ne = 64 print('> Grid :: {ne}'.format(ne=ne)) print('> Degree :: {p}'.format(p=p)) grid = linspace(0., 1., ne + 1) V = SplineSpace(p, grid=grid) # ... # ... Vector fem space V = VectorFemSpace(V, V) # ... # ... kernel_py = compile_kernel('kernel_block_1', a, V, backend='python') kernel_f90 = compile_kernel('kernel_block_1', backend='fortran') M_py = assemble_matrix(V, kernel_py) M_f90 = assemble_matrix(V, kernel_f90) # ... assert_identical_coo(M_py, M_f90)
def test_2d_block_2(): print('============== test_2d_block_2 ================') x, y = symbols('x y') u = Symbol('u') v = Symbol('v') epsilon = Constant('epsilon') Laplace = lambda v, u: Dot(Grad(v), Grad(u)) Mass = lambda v, u: v * u u1, u2, p = symbols('u1 u2 p') v1, v2, q = symbols('v1 v2 q') a = Lambda((x, y, v1, v2, q, u1, u2, p), Laplace(v1, u1) - dx(v1) * p + Laplace(v2, u2) - dy(v2) * p + q * (dx(u1) + dy(u2)) + epsilon * Mass(q, p)) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V = TensorFemSpace(V1, V2) V = VectorFemSpace(V, V, V) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # TODO not working yet => need complex numbers # # ... # symbol_f90 = compile_symbol('symbol_block_2', a, V, # d_constants={'epsilon': 0.1}, # backend='fortran') # # ... # # # ... example of symbol evaluation # t1 = linspace(-pi,pi, ne1+1) # t2 = linspace(-pi,pi, ne2+1) # x1 = linspace(0.,1., ne1+1) # x2 = linspace(0.,1., ne2+1) # e = zeros((2, 2, ne1+1, ne2+1), order='F') # symbol_f90(x1,x2,t1,t2, e) # # ... print('')
def test_2d_scalar_4(): print('============== test_2d_scalar_4 ================') # ... define the weak formulation x, y = symbols('x y') u = Symbol('u') v = Symbol('v') b0 = Function('b0') b1 = Function('b1') a = Lambda((x, y, v, u), (b0(x, y) * dx(v) + b1(x, y) * dy(v)) * (b0(x, y) * dx(u) + b1(x, y) * dy(u))) # ... # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V = TensorFemSpace(V1, V2) # ... # ... user defined function def b0(x, y): from numpy import sin from scipy import pi r = 1.1659397624413860850012270020670 * (1.0 + 0.1 * sin(2 * pi * y)) return r def b1(x, y): from numpy import sin from scipy import pi r = 1.0 * (1.0 + 0.1 * sin(2 * pi * y)) return r # ... # ... create an interactive pyccel context from pyccel.epyccel import ContextPyccel context = ContextPyccel(name='context_4') context.insert_function(b0, ['double', 'double'], kind='function', results=['double']) context.insert_function(b1, ['double', 'double'], kind='function', results=['double']) context.compile() # ... # ... kernel_py = compile_kernel('kernel_scalar_4', a, V, context=context, verbose=True, backend='python') kernel_f90 = compile_kernel('kernel_scalar_4', a, V, context=context, verbose=True, backend='fortran') # ... # ... M_py = assemble_matrix(V, kernel_py) M_f90 = assemble_matrix(V, kernel_f90) # ... assert_identical_coo(M_py, M_f90)
def test_2d_scalar_3(): print('============== test_2d_scalar_3 ================') # ... define the weak formulation x, y = symbols('x y') u = Symbol('u') v = Symbol('v') b = Function('b') a = Lambda((x, y, v, u), Dot(Grad(u), Grad(v)) + b(x, y) * u * v) # ... # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V = TensorFemSpace(V1, V2) # ... # ... user defined function def b(x, y): r = 1. + x * (1. - x) + y * (1. - y) return r # ... # ... create an interactive pyccel context from pyccel.epyccel import ContextPyccel context = ContextPyccel(name='context_3') context.insert_function(b, ['double', 'double'], kind='function', results=['double']) context.compile() # ... # ... kernel_py = compile_kernel('kernel_scalar_3', a, V, context=context, verbose=True, backend='python') kernel_f90 = compile_kernel('kernel_scalar_3', a, V, context=context, verbose=True, backend='fortran') # ... # ... M_py = assemble_matrix(V, kernel_py) M_f90 = assemble_matrix(V, kernel_f90) # ... assert_identical_coo(M_py, M_f90)
def test_1d_scalar_3(): print('============== test_1d_scalar_3 ================') x = Symbol('x') u = Symbol('u') v = Symbol('v') b = Function('b') a = Lambda((x, v, u), Dot(Grad(u), Grad(v)) + b(x) * u * v) print('> input := {0}'.format(a)) # ... create a finite element space p = 3 ne = 64 print('> Grid :: {ne}'.format(ne=ne)) print('> Degree :: {p}'.format(p=p)) grid = linspace(0., 1., ne + 1) V = SplineSpace(p, grid=grid) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... user defined function def b(s): r = 1. + s * (1. - s) return r # ... # ... create an interactive pyccel context from pyccel.epyccel import ContextPyccel context = ContextPyccel(name='context_scalar_3') context.insert_function(b, ['double'], kind='function', results=['double']) context.compile() # ... # ... symbol_f90 = compile_symbol('symbol_scalar_3', a, V, context=context, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne + 1) x1 = linspace(0., 1., ne + 1) e = zeros(ne + 1) symbol_f90(x1, t1, e) # ... print('')
def test_1d_scalar_3(): print('============== test_1d_scalar_3 ================') x = Symbol('x') u = Symbol('u') v = Symbol('v') b = Function('b') a = Lambda((x, v, u), Dot(Grad(u), Grad(v)) + b(x) * u * v) print('> input := {0}'.format(a)) # ... create a finite element space p = 3 ne = 64 print('> Grid :: {ne}'.format(ne=ne)) print('> Degree :: {p}'.format(p=p)) grid = linspace(0., 1., ne + 1) V = SplineSpace(p, grid=grid) # ... # ... user defined function def b(s): r = 1. + s * (1. - s) return r # ... # ... create an interactive pyccel context from pyccel.epyccel import ContextPyccel context = ContextPyccel(name='context_3') context.insert_function(b, ['double'], kind='function', results=['double']) context.compile() # ... # ... kernel_py = compile_kernel('kernel_scalar_3', a, V, context=context, verbose=True, backend='python') kernel_f90 = compile_kernel('kernel_scalar_3', a, V, context=context, verbose=True, backend='fortran') # ... # ... M_py = assemble_matrix(V, kernel_py) M_f90 = assemble_matrix(V, kernel_f90) # ... assert_identical_coo(M_py, M_f90) print('')
# ... numbers of elements and degres p1 = 2 p2 = 2 ne1 = 32 ne2 = 32 # ... comm = MPI.COMM_WORLD rank = comm.Get_rank() if rank == 0: print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V = TensorFemSpace(V1, V2, comm=comm) # ... wt = MPI.Wtime() assembly(V, kernel) wt = MPI.Wtime() - wt print('rank: ', rank, '> Elapsed time: {}'.format(wt)) # ...
sec = lambda x: np.sin(2 * np.pi * x) * (2 * np.pi)**2 analytic = lambda x: np.sin(2 * np.pi * x) # define dirichlet boundary conditions dirichletleft = 0. dirichletright = 0. # SplineSpace p = 3 ne = 60 grid = np.linspace(0., 1., ne + 1) # Main part print("Programm running ... ") V = SplineSpace(p, grid=grid) V.init_fem() # building stiffness matrix in a sparse matrix stiff = StiffnessMatrix(V) stiff = stiff.todense() rhs = assemblyRhs(V, sec) # homogenous dirichlet boundary conditions stiff = stiff[1:-1, 1:-1] rhs = rhs[1:-1] u = np.linalg.solve(stiff, rhs) u = list(u) u.insert(0, dirichletleft)
def test_3d_block_4(): print('============== test_3d_block_4 ================') """Alfven operator.""" x, y, z = symbols('x y z') u = IndexedBase('u') v = IndexedBase('v') bx = Constant('bx') by = Constant('by') bz = Constant('bz') b = Tuple(bx, by, bz) c0 = Constant('c0') c1 = Constant('c1') c2 = Constant('c2') a = Lambda((x, y, z, v, u), (c0 * Dot(u, v) + c1 * Div(u) * Div(v) + c2 * Dot(Curl(Cross(b, u)), Curl(Cross(b, v))))) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 p3 = 2 ne1 = 2 ne2 = 2 ne3 = 2 # ... print('> Grid :: [{},{},{}]'.format(ne1, ne2, ne3)) print('> Degree :: [{},{},{}]'.format(p1, p2, p3)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) grid_3 = linspace(0., 1., ne3 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V3 = SplineSpace(p3, grid=grid_3) Vx = TensorFemSpace(V1, V2, V3) Vy = TensorFemSpace(V1, V2, V3) Vz = TensorFemSpace(V1, V2, V3) V = VectorFemSpace(Vx, Vy, Vz) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... symbol_f90 = compile_symbol('symbol_block_4', a, V, d_constants={ 'bx': 0.1, 'by': 1., 'bz': 0.2, 'c0': 0.1, 'c1': 1., 'c2': 1. }, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne1 + 1) t2 = linspace(-pi, pi, ne2 + 1) t3 = linspace(-pi, pi, ne3 + 1) x1 = linspace(0., 1., ne1 + 1) x2 = linspace(0., 1., ne2 + 1) x3 = linspace(0., 1., ne3 + 1) e = zeros((3, 3, ne1 + 1, ne2 + 1, ne3 + 1), order='F') symbol_f90(x1, x2, x3, t1, t2, t3, e) # ... print('')
def test_2d_scalar_3(): print('============== test_2d_scalar_3 ================') x, y = symbols('x y') u = Symbol('u') v = Symbol('v') b = Function('b') a = Lambda((x, y, v, u), Dot(Grad(u), Grad(v)) + b(x, y) * u * v) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V = TensorFemSpace(V1, V2) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... user defined function def b(x, y): r = 1. + x * y return r # ... # ... create an interactive pyccel context from pyccel.epyccel import ContextPyccel context = ContextPyccel(name='context_scalar_3') context.insert_function(b, ['double', 'double'], kind='function', results=['double']) context.compile() # ... # ... symbol_f90 = compile_symbol('symbol_scalar_3', a, V, context=context, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne1 + 1) t2 = linspace(-pi, pi, ne2 + 1) x1 = linspace(0., 1., ne1 + 1) x2 = linspace(0., 1., ne2 + 1) e = zeros((ne1 + 1, ne2 + 1), order='F') symbol_f90(x1, x2, t1, t2, e) # ... print('')
def test_2d_scalar_2(): print('============== test_2d_scalar_2 ================') x, y = symbols('x y') u = Symbol('u') v = Symbol('v') c = Constant('c') b0 = Constant('b0') b1 = Constant('b1') b = Tuple(b0, b1) a = Lambda((x, y, v, u), c * u * v + Dot(b, Grad(v)) * u + Dot(b, Grad(u)) * v) print('> input := {0}'.format(a)) # ... create a finite element space p1 = 2 p2 = 2 ne1 = 8 ne2 = 8 print('> Grid :: [{ne1},{ne2}]'.format(ne1=ne1, ne2=ne2)) print('> Degree :: [{p1},{p2}]'.format(p1=p1, p2=p2)) grid_1 = linspace(0., 1., ne1 + 1) grid_2 = linspace(0., 1., ne2 + 1) V1 = SplineSpace(p1, grid=grid_1) V2 = SplineSpace(p2, grid=grid_2) V = TensorFemSpace(V1, V2) # ... # ... create a glt symbol from a string without evaluation expr = glt_symbol(a, space=V) print('> glt symbol := {0}'.format(expr)) # ... # ... symbol_f90 = compile_symbol('symbol_scalar_2', a, V, d_constants={ 'b0': 0.1, 'b1': 1., 'c': 0.2 }, backend='fortran') # ... # ... example of symbol evaluation t1 = linspace(-pi, pi, ne1 + 1) t2 = linspace(-pi, pi, ne2 + 1) x1 = linspace(0., 1., ne1 + 1) x2 = linspace(0., 1., ne2 + 1) e = zeros((ne1 + 1, ne2 + 1), order='F') symbol_f90(x1, x2, t1, t2, e) # ... print('')
def creation_spaces_2d(p1, p2, ne1, ne2): """ Creation of the different spaces used in our solver: Require: -------- @p1 and @p2 are degrees of direction 1 and 2 @ne1 and @ne2 are number of elements in direction 1 and 2 Returns: -------- @V TensorFem which represents H^1 space @W TensorFem which represents L^2 space @Wdiv1 and @Wdiv2 represent (H¹, div) space """ grid_1 = np.linspace( 0., 1., num=ne1+1 ) grid_2 = np.linspace( 0., 1., num=ne2+1 ) # Create 1D finite element spaces and precompute quadrature data V1 = SplineSpace( p1, grid=grid_1); V1.init_fem(quad_order=p1+1) W1 = SplineSpace(p1-1, grid=grid_1); W1.init_fem(quad_order=p1+1) V2 = SplineSpace( p2, grid=grid_2 ); V2.init_fem(quad_order=p2+1) W2 = SplineSpace(p2-1, grid=grid_2); W2.init_fem(quad_order=p2+1) # Create 2D tensor product finite element space V = TensorFemSpace(V1, V2 ) W = TensorFemSpace(W1, W2) Wdiv1 = TensorFemSpace(V1, W2) Wdiv2 = TensorFemSpace(W1, V2) return V, Wdiv1, Wdiv2, W