Skip to content

Commit

Permalink
Merge pull request #2237 from Open-Systems-Pharmacology/default_outpu…
Browse files Browse the repository at this point in the history
…ts_population_simulations

Default outputs population simulations
  • Loading branch information
abdelr authored Jun 2, 2022
2 parents 64cc180 + b2ef39e commit e1e5c3e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 27 deletions.
21 changes: 2 additions & 19 deletions src/PKSim.Core/Services/SimulationPersistableUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace PKSim.Core.Services
{
public interface ISimulationPersistableUpdater : OSPSuite.Core.Domain.Services.ISimulationPersistableUpdater
{
void UpdatePersistableFromSettings(IndividualSimulation individualSimulation);
void UpdatePersistableFromSettings(PopulationSimulation populationSimulation);
void UpdatePersistableFromSettings(Simulation populationSimulation);
void ResetPersistable(Simulation simulation);
}

Expand All @@ -18,7 +17,7 @@ public SimulationPersistableUpdater(IEntitiesInContainerRetriever quantitiesRetr
{
}

public void UpdatePersistableFromSettings(IndividualSimulation individualSimulation)
public void UpdatePersistableFromSettings(Simulation individualSimulation)
{
UpdateSimulationPersistable(individualSimulation);

Expand All @@ -27,11 +26,6 @@ public void UpdatePersistableFromSettings(IndividualSimulation individualSimulat
individualSimulation.Compounds.Each(compound => addRequiredOutputForSimulation(organism, compound));
}

public void UpdatePersistableFromSettings(PopulationSimulation populationSimulation)
{
UpdateSimulationPersistable(populationSimulation);
}

private void addRequiredOutputForSimulation(IContainer organism, Compound compound)
{
//make sure venous blood plasma is always selected so that PK can be calculated as well
Expand Down Expand Up @@ -68,11 +62,6 @@ private void setUrineFecesAndBileAmountToPersitable(Simulation simulation)
setUrineFecesAndBilePersitable(simulation, setMoleculeAmountToPersistableIn);
}

private void setUrineFecesAndBileConcentrationToNonPersitable(Simulation simulation)
{
setUrineFecesAndBilePersitable(simulation, setConcentrationObserversToNonPersistableIn);
}

private void setUrineFecesAndBilePersitable(Simulation simulation, Action<IContainer> updatePersitableInContainerAction)
{
var organism = simulation.Model.Root.Container(Constants.ORGANISM);
Expand All @@ -95,12 +84,6 @@ private void setMoleculeAmountToPersistableIn(IContainer container)
SetPersistable(container.GetAllChildren<IMoleculeAmount>(), true);
}

private void setConcentrationObserversToNonPersistableIn(IContainer container)
{
if (container == null) return;
SetPersistable(container.GetAllChildren<IObserver>(x => x.NameIsOneOf(CoreConstants.Observer.CONCENTRATION_IN_CONTAINER)), false);
}

private void setApplicationObserversNonPersistable(Simulation simulation)
{
//Set all observers defined in application to persistable false
Expand Down
8 changes: 4 additions & 4 deletions src/PKSim.Core/Services/SimulationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public Task RunSimulation(Simulation simulation, SimulationRunOptions simulation
switch (simulation)
{
case IndividualSimulation individualSimulation:
return runSimulation<IndividualSimulation, SimulationRunResults>(individualSimulation, _simulationPersistableUpdater.UpdatePersistableFromSettings);
return runSimulation<IndividualSimulation, SimulationRunResults>(individualSimulation);

case PopulationSimulation populationSimulation:
return runSimulation<PopulationSimulation, PopulationRunResults>(populationSimulation, _simulationPersistableUpdater.UpdatePersistableFromSettings);
return runSimulation<PopulationSimulation, PopulationRunResults>(populationSimulation);
}

return _simulationDidNotRun;
Expand All @@ -61,15 +61,15 @@ public void StopSimulation()
_simulationEngine = null;
}

private async Task runSimulation<TSimulation, TResult>(TSimulation simulation, Action<TSimulation> updatePersistableFromSettings) where TSimulation : Simulation
private async Task runSimulation<TSimulation, TResult>(TSimulation simulation) where TSimulation : Simulation
{
var simulationEngine = _simulationEngineFactory.Create<TSimulation, TResult>();
_simulationEngine = simulationEngine;

if (_simulationRunOptions.RunForAllOutputs)
_simulationPersistableUpdater.ResetPersistable(simulation);
else
updatePersistableFromSettings(simulation);
_simulationPersistableUpdater.UpdatePersistableFromSettings(simulation);

updateSolverSettings(simulation);
await simulationEngine.RunAsync(simulation, _simulationRunOptions);
Expand Down
75 changes: 71 additions & 4 deletions tests/PKSim.Tests/Core/SimulationPersistableUpdaterSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override void Context()

_individualSimulation = new IndividualSimulation();
_individualSimulation.SimulationSettings = new SimulationSettings();
_individualSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) {BuildingBlock = compound});
_individualSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) { BuildingBlock = compound });
_individualSimulation.Model = new OSPSuite.Core.Domain.Model();
_individualSimulation.Model.Root = new Container();
_individualSimulation.Model.Root.Add(organsim);
Expand Down Expand Up @@ -87,6 +87,73 @@ public void should_always_select_the_peripheral_venous_blood_plasna()
}
}

public class When_updating_the_simulation_settings_for_a_population_simulation : concern_for_SimulationPersistableUpdater
{
private PopulationSimulation _populationSimulation;
private Observer _venousBloodPlasma;
private Observer _fabsObserver;
private Observer _peripheralVenousBloodObserver;

protected override void Context()
{
base.Context();
var organsim = new Container().WithName(Constants.ORGANISM);
var venousBlood = new Container().WithName(CoreConstants.Organ.VENOUS_BLOOD).WithParentContainer(organsim);
var peripheralVenousBlood = new Container().WithName(CoreConstants.Organ.PERIPHERAL_VENOUS_BLOOD).WithParentContainer(organsim);
var lumen = new Container().WithName(CoreConstants.Organ.LUMEN).WithParentContainer(organsim);
var plasma = new Container().WithName(CoreConstants.Compartment.PLASMA);
var moleculeVenousBlood = new Container().WithName("DRUG")
.WithParentContainer(plasma.WithParentContainer(venousBlood));

var moleculePeripheral = new Container().WithName("DRUG")
.WithParentContainer(peripheralVenousBlood);

_peripheralVenousBloodObserver = new Observer().WithName(CoreConstants.Observer.PLASMA_PERIPHERAL_VENOUS_BLOOD);
moleculePeripheral.Add(_peripheralVenousBloodObserver);

_venousBloodPlasma = new Observer().WithName(CoreConstants.Observer.CONCENTRATION_IN_CONTAINER);
moleculeVenousBlood.Add(_venousBloodPlasma);

var moleculeLumen = new Container().WithName("DRUG")
.WithParentContainer(lumen);

_fabsObserver = new Observer().WithName(CoreConstants.Observer.FABS_ORAL);
moleculeLumen.Add(_fabsObserver);

var compound = new Compound().WithName("DRUG");

_populationSimulation = new PopulationSimulation();
_populationSimulation.SimulationSettings = new SimulationSettings();
_populationSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) { BuildingBlock = compound });
_populationSimulation.Model = new OSPSuite.Core.Domain.Model();
_populationSimulation.Model.Root = new Container();
_populationSimulation.Model.Root.Add(organsim);
}

protected override void Because()
{
sut.UpdatePersistableFromSettings(_populationSimulation);
}

[Observation]
public void should_always_select_the_venous_blood_plasma_observer()
{
_venousBloodPlasma.Persistable.ShouldBeTrue();
}

[Observation]
public void should_always_select_the_lumen_fabs_observer()
{
_fabsObserver.Persistable.ShouldBeTrue();
}

[Observation]
public void should_always_select_the_peripheral_venous_blood_plasna()
{
_peripheralVenousBloodObserver.Persistable.ShouldBeTrue();
}
}

public class When_resetting_the_simulation_settings_for_a_given_simulation : concern_for_SimulationPersistableUpdater
{
private Observer _urineConcentrationObserver;
Expand Down Expand Up @@ -114,7 +181,7 @@ protected override void Context()
.WithParentContainer(feces);
_moleculeFeces.Persistable = false;

_moleculeGallBladder = new MoleculeAmount().WithName("DRUG")
_moleculeGallBladder = new MoleculeAmount().WithName("DRUG")
.WithParentContainer(gallBladder);
_moleculeGallBladder.Persistable = false;

Expand All @@ -130,8 +197,8 @@ protected override void Context()
var compound = new Compound().WithName("DRUG");

_individualSimulation = new IndividualSimulation();
_individualSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) {BuildingBlock = compound});
_individualSimulation.Model = new OSPSuite.Core.Domain.Model {Root = new Container {organsim}};
_individualSimulation.AddUsedBuildingBlock(new UsedBuildingBlock("Id", PKSimBuildingBlockType.Compound) { BuildingBlock = compound });
_individualSimulation.Model = new OSPSuite.Core.Domain.Model { Root = new Container { organsim } };
}

protected override void Because()
Expand Down

0 comments on commit e1e5c3e

Please sign in to comment.