UFL conditions cannot be evaluated as bool in a Python context

Hello everybody,
I have a problem with the imposition of conditions when I activate the dolfin module. Specifically, in my script I have such conditions:

if (t1<(Day_after_surgery*86400)): 
        k_c=0
        k_r=0
    elif (t1>(Day_after_surgery*86400) and t1<((Day_after_surgery+42)*86400)): 
        k_r=0.0648/86400
        k_c=0.00735/86400

from which I get this output:

UFL conditions cannot be evaluated as bool in a Python context.
Traceback (most recent call last):
  File "/Users/lorenzomarta/Desktop/Script simulazioni/Test10/GBM_Besta_10.py", line 341, in <module>
    if (t1<(Day_after_surgery*86400)): #pre-inizio terapia
  File "/Applications/anaconda3/envs/fenicsproject/lib/python3.10/site-packages/ufl/conditional.py", line 46, in __bool__
    error("UFL conditions cannot be evaluated as bool in a Python context.")
  File "/Applications/anaconda3/envs/fenicsproject/lib/python3.10/site-packages/ufl/log.py", line 172, in error
    raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: UFL conditions cannot be evaluated as bool in a Python context.

How could I solve?
Thanks in advance

Without a minimal example reproducing the error it is hard to give any guidance as to how to solve your problem.

In my script I have inserted the following libraries:

from dolfin import *
import numpy as np
import sys

I used the following compiler options:

parameters["form_compiler"]["cpp_optimize"] = True
parameters["form_compiler"]["representation"] = "uflacs"
parameters["form_compiler"]["quadrature_degree"] = 4

After that I inserted the mesh, defined the variables, the function space and the function class:

# Finite Element function spaces
V = FiniteElement("Lagrange", mesh.ufl_cell(), 1) 
ME = FunctionSpace(mesh, V)

# FE-space to determine the initial condition of phi
element1 = MixedElement([V,V,V])
ME1 = FunctionSpace(mesh, element1)

# Trial functions at time n+1
solution1 = Function(ME1)
phi, mu, n = split(solution1)

# Trial functions at time n
solution1_old = Function(ME1)
phi_old, mu_old, n_old = split(solution1_old) 

# Trial functions for derivatives
d_solution = TrialFunction(ME1)
d_phi, d_mu, d_n = split(d_solution)

# Test functions
varphi, vv, q = TestFunctions(ME1)

# ---------------------- Cahn-Hilliard Class ----------------------------------------------- #

# Interface-class for the Newton solver
class CahnHilliardEquation(NonlinearProblem):
    #def __init__(self, a, L, bc):
    def __init__(self, a, L):
        NonlinearProblem.__init__(self)
        self.L = L
        self.a = a
        #self.bc = bc
        self.reset_sparsity = True
    def F(self, b, x):
        assemble(self.L, tensor=b)
        #self.bc.apply(b, x)
    def J(self, A, x):
        assemble(self.a, tensor=A)
        #self.bc.apply(A)
        self.reset_sparsity = False

At this point the initial condition has been defined and a time cycle has been started to update the solution. the conditions on the parameters k_c, k_s indicated above are found inside the loop.

In the code you have supplied, you have not defined
the variables in

or

which is the cause of your error message, it is still impossible to give you guidance.

Day_after_surgery is defined as follows:

Day_after_surgery = int(input('How many days after the surgery did the treatment start? '))
T_tot = 131 + Day_after_surgery

while t1 is defined as t1 = 0.0 before the start of the cycle and updated at each step with the addition of dt1. Specifically, we have that:

deltaT = (T_tot/301)*86400
dt1 = Constant(deltaT)

I made a mistake, the constraint on t1 is as follows:

if (t1<(Day_after_surgery*86400)): 
        k_c=0
        k_r=0
    elif (t1>(Day_after_surgery*86400) and t1<((Day_after_surgery+42)*86400)): 
        k_r=0.0648/86400
        k_c=0.00735/86400

Are you assiging t1 as
t1 += dt1?
You should convert use deltaT to update t and not dt1.

It would seem to work, thanks !!!

However I have another problem now, modifying inside the “for loop” my FO function passing by:

   F0 = phi * varphi * dx - phi_old * varphi * dx \
         + deltaT * (1/M_0) *  inner(mat_T * grad(mu), grad(varphi)) * dx \
         - deltaT * nu * (n - delta) * (1/2) * (1 + phi_old) * varphi * dx \

to

   F0 = phi * varphi * dx - phi_old * varphi * dx \
         + deltaT * (1/M_0) *  inner(mat_T * grad(mu), grad(varphi)) * dx \
         - deltaT * nu * (n - delta) * (1/2) * (1 + phi_old) * varphi * dx \
         + deltaT * (k_r + k_c) * phi_old * varphi * dx \

where phi is the function with respect to which the weak formulation is being solved, varphi the respective test function, deltaT the time step, k_s, k_c, nu, n and M_0 are constants, while mat_T a matrix.
I get the following output:

 File "/Users/lorenzomarta/Desktop/Script simulazioni/Test9/GBM_Besta_9.py", line 369, in <module>
    + dt1 * (k_r + k_c) * phi_old * varphi * dx \
  File "/Applications/anaconda3/envs/fenicsproject/lib/python3.10/site-packages/ufl/measure.py", line 441, in __rmul__
    error("This integral is missing an integration domain.")
  File "/Applications/anaconda3/envs/fenicsproject/lib/python3.10/site-packages/ufl/log.py", line 172, in error
    raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: This integral is missing an integration domain.

Since the differential term for integration is dx, I don’t understand what’s wrong, as before the last addition everything works fine.

You keep on just adding snippets of code that cannot reproduce the error.
This makes it very hard for anyone to give any guidance.
What I would suggest is overload dx with dx = Measure("dx", domain=mesh).