Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect handling of event assignments to compartments #2426

Closed
dweindl opened this issue May 6, 2024 · 0 comments · Fixed by #2428
Closed

Incorrect handling of event assignments to compartments #2426

dweindl opened this issue May 6, 2024 · 0 comments · Fixed by #2428
Assignees
Labels
bug events related to handing discrete events SBML SBML import related

Comments

@dweindl
Copy link
Member

dweindl commented May 6, 2024

I think there is a problem with event assignments to compartments (targeted by rate rules, others aren't supported yet), if

  1. there are multiple event assignments for a single event, and
    2a) the assignment to the compartment is not the last assignment, or
    2b) there are assignments to multiple compartments

In this case, here:

bolus[state_vector.index(species_sym)] = (
species_value * compartment_sym / formula
)

formula has the value of the most recently processed event assignments, independent on whether that was the respective compartment change.

Reproducer for case 2a:

    import amici
    from amici.antimony_import import antimony2amici
    from amici.testing import TemporaryDirectoryWinSafe as TemporaryDirectory
    from numpy.testing import assert_allclose

    ant_model = """
    model test_events_multiple_assignments
        compartment event_target = 1
        event_target' = 0
        species species_in_event_target in event_target = 1
        unrelated = 1
        
        # those two events produce identical results, but they don't
        #  this one fails
        at (time > 10): unrelated = 2, event_target = 10

        #  this one works
        #at (time > 10): event_target = 10, unrelated = 2

        # (note that antimony adds the event assignments to the sbml model 
        # from right to left)
    end
    """
    module_name = "test_multiple_event_assignment_with_compartment"
    with TemporaryDirectory(prefix=module_name, delete=False) as outdir:
        antimony2amici(
            ant_model,
            model_name=module_name,
            output_dir=outdir,
            verbose=True,
        )
        model_module = amici.import_model_module(
            module_name=module_name, module_path=outdir
        )
        amici_model = model_module.getModel()
        assert amici_model.ne == 1
        assert amici_model.ne_solver == 0
        assert amici_model.nx_rdata == 3
        amici_model.setTimepoints(np.linspace(0, 15, 200))
        amici_solver = amici_model.getSolver()
        rdata = amici.runAmiciSimulation(amici_model, amici_solver)
        assert rdata.status == amici.AMICI_SUCCESS
        idx_event_target = amici_model.getStateIds().index("event_target")
        idx_unrelated = amici_model.getStateIds().index("unrelated")
        idx_species_in_event_target = amici_model.getStateIds().index(
            "species_in_event_target"
        )
        assert (rdata.x[rdata.ts < 10, idx_event_target] == 1).all()
        assert (rdata.x[rdata.ts > 10, idx_event_target] == 10).all()
        assert (rdata.x[rdata.ts < 10, idx_unrelated] == 1).all()
        assert (rdata.x[rdata.ts > 10, idx_unrelated] == 2).all()
        assert (rdata.x[rdata.ts < 10, idx_species_in_event_target] == 1).all()
        # fails, it's 0.5, because of division by the new value of `unrelated`
        assert_allclose(rdata.x[rdata.ts > 10, idx_species_in_event_target], 0.1, rtol=0, atol=1e-15)
@dweindl dweindl added bug SBML SBML import related events related to handing discrete events labels May 6, 2024
@dweindl dweindl self-assigned this May 6, 2024
dweindl added a commit to dweindl/AMICI that referenced this issue May 6, 2024
Previously, in some cases (see AMICI-dev#2426), event assignments to compartments
would result in incorrect concentrations because an incorrect post-event
volume was used. This is fixed here.

Closes AMICI-dev#2426.
@dweindl dweindl linked a pull request May 7, 2024 that will close this issue
@dweindl dweindl closed this as completed in 0464b83 May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug events related to handing discrete events SBML SBML import related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant