I guess you are referring to this?
In the code below, you are mixing a whole lot of ufl
and numpy
Let us consider a simple MWE, on a unit square:
from IPython import embed
from mpi4py import MPI
from dolfinx import fem, io, nls, log, mesh, plot
import numpy as np
from petsc4py.PETSc import ScalarType
import ufl
from dolfinx.io import gmshio
domain = mesh.create_rectangle(MPI.COMM_WORLD, [[2, 2], [3, 3]], [10, 10])
"""Function Space"""
# Define function space, scalar
U2 = ufl.VectorElement("Lagrange", domain.ufl_cell(), 1) # Displacent
# concentrações + - , electric potential
P1 = ufl.FiniteElement("Lagrange", domain.ufl_cell(), 1)
D0 = ufl.FiniteElement("DG", domain.ufl_cell(), 0)
D1 = ufl.FiniteElement("DG", domain.ufl_cell(), 1)
# DOFs
TH = ufl.MixedElement([U2, P1, P1, P1])
ME = fem.FunctionSpace(domain, TH) # Total space for all DOFs
w = fem.Function(ME)
a = 2.5
cpos0 = 1.1
cNum = 2
def init_omega(x):
condition = x[1] > a
condition_inverse = np.invert(condition)
return condition*np.log(cpos0) + condition_inverse * np.log(cNum)
w.sub(1).interpolate(init_omega)
Here I’ve created a simply numpy function with the same functionality as
where you replace a
with int2-tol
etc.
As long as your example is kept as partial snippets, it is quite hard to give you any further guidance (and it is aso the reason why it has taken so long to write up a reply). I would strongly suggest you try to simplify your code, and have a look at: PETSc NonlinearProblem running slow - #4 by dokken
as it seems rather relevant for the second question.