Skip to content

Commit

Permalink
Fixes #2260 Formulas are not applied correctly (#2262)
Browse files Browse the repository at this point in the history
* Fixes #2260 Formulas are not applied correctly
Fixes #2261 Cannot create simulation when a module overwrites a value of a parameter with a formula

* PR feedback
  • Loading branch information
rwmcintosh committed Jun 14, 2024
1 parent 7e8d6df commit 6f347e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/OSPSuite.Core/Domain/Services/QuantityValuesUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OSPSuite.Core.Domain.Builder;
using OSPSuite.Core.Domain.Formulas;
using OSPSuite.Core.Domain.Mappers;
using OSPSuite.Core.Extensions;
using OSPSuite.Utility.Extensions;

namespace OSPSuite.Core.Domain.Services
Expand Down Expand Up @@ -126,7 +127,7 @@ private IParameter getParameter(ModelConfiguration modelConfiguration, PathAndVa
}

//If the value is defined, this will be used instead of the formula (even if set previously)
if (pathAndValueEntity.Value != null)
if (pathAndValueEntity.Value.IsValid())
{
var parameterValue = pathAndValueEntity.Value.Value;
if (parameter.Formula is ConstantFormula constantFormula)
Expand Down
4 changes: 4 additions & 0 deletions src/OSPSuite.Core/Extensions/DoubleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,9 @@ public static bool EqualsByTolerance(this double[] value, double[] equalValue)
return EqualsByTolerance(value, equalValue, 1e-10);
}

public static bool IsValid(this double? value)
{
return value.HasValue && value.Value.IsValid();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,29 @@ public void should_have_created_a_new_distributed_parameter_defined_in_the_indiv
}
}

internal class When_a_parameter_value_is_defined_with_formula_and_nan_value : concern_for_ModelConstructor
{
private ParameterValue _parameterValue;

protected override void Context()
{
base.Context();
var simulationBuilder = new SimulationBuilder(_simulationConfiguration);

_parameterValue = simulationBuilder.ParameterValues.First(x => x.Name.Equals("FormulaParameterOverwritten"));
_parameterValue.Value = double.NaN;
_parameterValue.Formula = new ExplicitFormula("1");
}

[Observation]
public void should_not_overwrite_the_formula_with_NaN_fixed_value()
{
var targetParameter = _parameterValue.Path.TryResolve<IParameter>(_result.Model.Root);
targetParameter.IsFixedValue.ShouldBeFalse();
targetParameter.Value.ShouldNotBeEqualTo(double.NaN);
}
}

internal class When_a_initial_condition_is_defined_for_logical_container : concern_for_ModelConstructor
{
protected override void Context()
Expand Down

0 comments on commit 6f347e7

Please sign in to comment.