Boolean expression in a while loop with theano tensor

Hi,
I am new to pymc3 and I am trying to use pymc3 for a model that I have written using NumPy arrays. This model has ODE that I solve numerically using finite-difference. I start with a guess value and use a while loop to solve iteratively until the error is below the given convergence criteria. This is the code snippet of the definition from the model where I am using the while loop.

    def compute_pipe(self, q_liq_in, t_inlet, p_out, t_out):
        """
        Computes the pressure drop, void fractions, holdups and phase velocities of the pipe
        at given inlet and outlet conditions.

        Parameters
        ----------
        q_liq_in : volumetric flowrates of liquid in m3/s or Sm3/s depending on specified boundary conditions
        t_inlet : inlet temperature in the pipe, Celsius
        p_out : outlet pressure from the pipe, Pa
        t_out : outlet temperature from the pipe, Celsius
        """
        # initialize pressure and temperature distribution
        p_guess, t_lin = self.initialize(t_inlet, p_out, t_out)
        # solve the system until it converges
        criterion = 1e-10  # convergence criterion
        max_error = 1e5
        iteration = 0
        while max_error > criterion:
            # Compute mass balance and get phase fractions and velocities
            (void_fractions,
             holdups,
             mixture_velocities,
             gas_velocities,
             liquid_velocities,
             liq_visc) = self.compute_mass_balance(p_guess, t_lin, t_inlet, q_liq_in)

            gas_superficial = np.array(np.array(gas_velocities) * np.array(void_fractions)).tolist()
            
            liquid_superficial = np.array(np.array(liquid_velocities) * np.array(holdups)).tolist()
            
            # Compute momentum balance and get pressure drops in each control volume
            pressure_drops = self.compute_momentum_balance(pressures=p_guess, temperatures=t_lin,
                                                           void_fractions=void_fractions,
                                                           mix_velocities=mixture_velocities,
                                                           gas_superficial=gas_superficial,
                                                           liquid_superficial=liquid_superficial)
            
            new_pressure_vector = self.solve_pressure_system(pressure_drops, p_out=p_out)

            press_diff = new_pressure_vector - np.array(p_guess).reshape(-1, 1)
            relative_error = abs(press_diff) / np.array(p_guess).reshape(-1, 1)
            p_guess = np.array(p_guess).reshape(-1, 1) + 0.5 * press_diff 
            p_guess = p_guess.ravel().tolist()
            # Compute maximum error to compare with the criterion
            max_error = np.sum(relative_error)
            iteration = iteration + 1
        print("Solution has converged after {} iterations".format(iteration))
        return new_pressure_vector

Now, when I use this with the pymc3 I keep getting “TypeError: Variables do not support boolean operations.”
I guess this is because <=,==,> operations are not supported by theano.tensor. How can I modify this operation to make it work in pymc3?

You’ll need to use the conditionals made available in theano.tensor.

1 Like