pmgbergen / porepy

Python Simulation Tool for Fractured and Deformable Porous Media

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect behavior of `previous_iteration` and `previous_timestep`

Yuriyzabegaev opened this issue · comments

I stumbled upon the problem of creating the ad variable both on the previous time step and the previous iteration. The code that does not work (imagine we are building a model where the accumulation term is evaluated on the previous time step):

import porepy as pp
from porepy.models.fluid_mass_balance import SinglePhaseFlow 
class SinglePhaseModelBreakdown(SinglePhaseFlow):

    def fluid_mass(self, subdomains: list[pp.Grid]) -> pp.ad.Operator:
        pressure_prev_it = self.pressure(subdomains).previous_iteration()
        exp = pp.ad.Function(pp.ad.exp, "density_exponential")
        rho_0 = pp.ad.Scalar(self.fluid.density())
        density_prev_it = rho_0 * exp(self.fluid_compressibility(subdomains) * pressure_prev_it)

        mass_density = density_prev_it * self.porosity(subdomains)
        mass = self.volume_integral(mass_density, subdomains, dim=1)
        mass.set_name("fluid_mass")
        return mass
    

model = SinglePhaseModelBreakdown()
pp.run_stationary_model(model, {})

The result is:

KeyError: Variable pressure with id 5 on grid 0
Degrees of freedom: cells (1), faces (0), nodes (0)
Evaluated at the previous iteration.

I understand that these functions are not meant to be used together, and the previous Newton iteration for the previous time step is not defined. However, I faced this problem when I was making a custom stabilization term for preconditioning, and I had to use previous_iteration in the accumulation term. I only needed the Jacobian of that operator, so I assumed that the whole term related to the previous time step will cut down. Instead, it raised this error and took some time to figure out what causes it in the ad implementation. Thus, I propose to raise an error here, to show more clearly, that this is not the intended use. If you agree, I can make this change.

You are right that what you did is not the intended solution, but surely a more transparent error message is useful. The solution you suggest is fair, so if you are willing to do a PR, please go ahead.