@dokken I want to solve one term by Poisson equation with Neumann and periodic boundary conditions are given. My code is not working for the vel function that mentioned in the variational problem of first code.
Here is the minimal example:
from __future__ import print_function
from fenics import *
from dolfin import *
# Create mesh and define function space
nx = ny = 256
mesh = RectangleMesh( Point(0, 0), Point(5120, 6400), nx, ny)
# Define function space for velocity
W = VectorFunctionSpace(mesh, 'P', 2)
#Define function space for system of equations
P1 = FiniteElement('P', triangle, 1)
element = MixedElement( [P1, P1] )
V = FunctionSpace ( mesh, element)
# Define test functions
v_1, v_2= TestFunctions(V)
# Define functions for velocity and PDE
w = Function(W)
u = Function(V)
u_n = Function(V)
# Split system functions to access components
u_1, u_2 = split(u)
u_n1, u_n2 = split(u_n)
#Define source terms
f_1 = Constant (0.14)
f_2 = Constant (0.0)
#Define function
def K(u_1, u_2):
return( (L*(P(u_1)-P_c)+L*(Q(u_1)-Q_c)+(F_s-F_c))/M_s(u_2))
# Define variational problem
F = - M1 * dot(grad( vel(u_1, u_2)), grad( u_1))v_1 dx
- M2 * dot(grad( vel(u_1, u_2)), grad( u_2))v_2 dx
My code is not working for the function vel(u_1, u_2). In this function, I want to calculate
-\Delta u = - div (K(u_1, u_2) on [0,5120] \times [o,6400]
\frac{\partial u}{\partial n} = 0 on y =0 and y=6400
u(0,y)=u(5120, y).
The code for vel(u_1,u_2) is :
def vel( w1, w2):
class PeriodicBoundary(SubDomain):
def inside(self, x, on_boundary):
return bool( x[0] <DOLFIN_EPS or x[0] > -DOLFIN_EPS and on_boundary)
def map(self, x, y):
y[0] = x[0] - 5120.0
y[1] = x[1]
V1 = FunctionSpace(mesh, "CG", 1, constrained_domain=PeriodicBoundary() )
dof_coordinates = V1.tabulate_dof_coordinates()
# Define variational problem
u1 = TrialFunction(V1)
v1 = TestFunction(V1)
f = - div ( K(w1, w2))
a = dot(grad(u1), grad(v1))*dx
L = f*v1*dx
# Compute solution
u1 = Function(V1)
solve(a == L, u1 )
return(u1)
The following error occurred:
File "AN.py", line 124, in vel
solve(a == L, u1 )
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/fem/solving.py", line 220, in solve
_solve_varproblem(*args, **kwargs)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/fem/solving.py", line 241, in _solve_varproblem
problem = LinearVariationalProblem(eq.lhs, eq.rhs, u, bcs,
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/fem/problem.py", line 55, in __init__
L = Form(L, form_compiler_parameters=form_compiler_parameters)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/fem/form.py", line 43, in __init__
ufc_form = ffc_jit(form, form_compiler_parameters=form_compiler_parameters,
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/jit/jit.py", line 47, in mpi_jit
return local_jit(*args, **kwargs)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/jit/jit.py", line 97, in ffc_jit
return ffc.jit(ufl_form, parameters=p)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ffc/jitcompiler.py", line 217, in jit
module = jit_build(ufl_object, module_name, parameters)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ffc/jitcompiler.py", line 130, in jit_build
module, signature = dijitso.jit(jitable=ufl_object,
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dijitso/jit.py", line 165, in jit
header, source, dependencies = generate(jitable, name, signature, params["generator"])
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ffc/jitcompiler.py", line 65, in jit_generate
code_h, code_c, dependent_ufl_objects = compile_object(ufl_object,
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ffc/compiler.py", line 142, in compile_form
return compile_ufl_objects(forms, "form", object_names,
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ffc/compiler.py", line 185, in compile_ufl_objects
analysis = analyze_ufl_objects(ufl_objects, kind, parameters)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ffc/analysis.py", line 89, in analyze_ufl_objects
form_datas = tuple(_analyze_form(form, parameters)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ffc/analysis.py", line 89, in <genexpr>
form_datas = tuple(_analyze_form(form, parameters)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ffc/analysis.py", line 169, in _analyze_form
form_data = compute_form_data(form,
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algorithms/compute_form_data.py", line 264, in compute_form_data
form = apply_algebra_lowering(form)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algorithms/apply_algebra_lowering.py", line 186, in apply_algebra_lowering
return map_integrand_dags(LowerCompoundAlgebra(), expr)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algorithms/map_integrands.py", line 57, in map_integrand_dags
return map_integrands(lambda expr: map_expr_dag(function, expr, compress),
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algorithms/map_integrands.py", line 38, in map_integrands
mapped_integrals = [map_integrands(function, itg, only_integral_type)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algorithms/map_integrands.py", line 38, in <listcomp>
mapped_integrals = [map_integrands(function, itg, only_integral_type)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algorithms/map_integrands.py", line 46, in map_integrands
return itg.reconstruct(function(itg.integrand()))
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algorithms/map_integrands.py", line 57, in <lambda>
return map_integrands(lambda expr: map_expr_dag(function, expr, compress),
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/corealg/map_dag.py", line 37, in map_expr_dag
result, = map_expr_dags(function, [expression], compress=compress)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/corealg/map_dag.py", line 86, in map_expr_dags
r = handlers[v._ufl_typecode_](v, *[vcache[u] for u in v.ufl_operands])
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algorithms/apply_algebra_lowering.py", line 152, in div
return a[..., i].dx(i)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/exproperators.py", line 449, in _getitem
all_indices, slice_indices, repeated_indices = create_slice_indices(component, shape, self.ufl_free_indices)
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/index_combination_utils.py", line 181, in create_slice_indices
error("Component and shape length don't match.")
File "/Users/kapilchawla/opt/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/log.py", line 172, in error
raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: Component and shape length don't match.
I am first time dealing with Neumann and periodic boundary condition. Can you suggest the possible correction in the code?