/// <summary>Phes the units set.</summary> /// <param name="ToUnits">To units.</param> public void PHUnitsSet(PHUnitsEnum ToUnits) { if (ToUnits != PHUnits) { // convert the numbers if (ToUnits == PHUnitsEnum.Water) { // pH in water = (pH in CaCl X 1.1045) - 0.1375 PH = MathUtilities.Subtract_Value(MathUtilities.Multiply_Value(PH, 1.1045), 0.1375); } else { // pH in CaCl = (pH in water + 0.1375) / 1.1045 PH = MathUtilities.Divide_Value(MathUtilities.AddValue(PH, 0.1375), 1.1045); } PHUnits = ToUnits; } }
public void TestSaturatedFlow() { SoilModel soil = new SoilModel(); APSIM.Shared.Soils.Soil soilProperties = Setup(); APSIMReadySoil.Create(soilProperties); SaturatedFlowModel saturatedFlow = new SaturatedFlowModel(); SetLink(soil, "properties", soilProperties); SetLink(saturatedFlow, "soil", soil); saturatedFlow.SWCON = new double[] { 0.3, 0.3, 0.3, 0.3, 0.3, 0.3 }; // Profile at DUL. soil.Water = MathUtilities.Multiply(soilProperties.Water.DUL, soilProperties.Water.Thickness); double[] flux = saturatedFlow.Values; Assert.IsTrue(MathUtilities.AreEqual(flux, new double[] { 0, 0, 0, 0, 0, 0 })); // Profile at SAT. soil.Water = MathUtilities.Multiply(soilProperties.Water.SAT, soilProperties.Water.Thickness); flux = saturatedFlow.Values; Assert.IsTrue(MathUtilities.AreEqual(flux, new double[] { 1.05, 2.85, 4.64999, 6.45, 8.25, 10.05 })); // Use the KS method soilProperties.Water.KS = new double[] { 1000, 300, 20, 100, 100, 100 }; flux = saturatedFlow.Values; Assert.IsTrue(MathUtilities.AreEqual(flux, new double[] { 1.05, 1.8000, 1.8000, 1.8000, 1.8000, 1.8000 })); Assert.AreEqual(saturatedFlow.backedUpSurface, 0); // Use the KS method, water above SAT. soilProperties.Water.KS = new double[] { 1000, 300, 20, 100, 100, 100 }; MathUtilities.AddValue(soil.Water, 10); // add 5 mm of water into each layer. flux = saturatedFlow.Values; Assert.IsTrue(MathUtilities.AreEqual(flux, new double[] { 1.05, 1.8000, 1.8000, 1.8000, 1.8000, 1.8000 })); }
/// <summary>Evaluates the specified sym1.</summary> /// <param name="sym1">The sym1.</param> /// <param name="opr">The opr.</param> /// <param name="sym2">The sym2.</param> /// <returns></returns> protected Symbol Evaluate(Symbol sym1, Symbol opr, Symbol sym2) { Symbol result; result.m_name = sym1.m_name + opr.m_name + sym2.m_name; result.m_type = ExpressionType.Result; result.m_value = 0; result.m_values = null; switch (opr.m_name) { case "^": if (sym1.m_values != null) { result.m_values = new double[sym1.m_values.Length]; for (int i = 0; i < sym1.m_values.Length; i++) { result.m_values[i] = Math.Pow(sym1.m_values[i], sym2.m_value); } } else { result.m_value = System.Math.Pow(sym1.m_value, sym2.m_value); } break; case "/": { if (sym1.m_values != null && sym2.m_values != null) { result.m_values = MathUtilities.Divide(sym1.m_values, sym2.m_values); } else if (sym1.m_values != null) { result.m_values = MathUtilities.Divide_Value(sym1.m_values, sym2.m_value); } else if (sym2.m_values != null) { result.m_values = new double[sym2.m_values.Length]; for (int i = 0; i < result.m_values.Length; i++) { result.m_values[i] = MathUtilities.Divide(sym1.m_value, sym2.m_values[i], 0); } } else { if (sym2.m_value > 0) { result.m_value = sym1.m_value / sym2.m_value; } else { result.m_name = "Divide by Zero."; result.m_type = ExpressionType.Error; } } break; } case "*": if (sym1.m_values != null && sym2.m_values != null) { result.m_values = MathUtilities.Multiply(sym1.m_values, sym2.m_values); } else if (sym1.m_values != null) { result.m_values = MathUtilities.Multiply_Value(sym1.m_values, sym2.m_value); } else if (sym2.m_values != null) { result.m_values = MathUtilities.Multiply_Value(sym2.m_values, sym1.m_value); } else { result.m_value = sym1.m_value * sym2.m_value; } break; case "%": result.m_value = sym1.m_value % sym2.m_value; break; case "+": if (sym1.m_values != null && sym2.m_values != null) { result.m_values = MathUtilities.Add(sym1.m_values, sym2.m_values); } else if (sym1.m_values != null) { result.m_values = MathUtilities.AddValue(sym1.m_values, sym2.m_value); } else if (sym2.m_values != null) { result.m_values = MathUtilities.AddValue(sym2.m_values, sym1.m_value); } else { result.m_value = sym1.m_value + sym2.m_value; } break; case "-": if (sym1.m_values != null && sym2.m_values != null) { result.m_values = MathUtilities.Subtract(sym1.m_values, sym2.m_values); } else if (sym1.m_values != null) { result.m_values = MathUtilities.Subtract_Value(sym1.m_values, sym2.m_value); } else if (sym2.m_values != null) { result.m_values = new double[sym2.m_values.Length]; for (int i = 0; i < result.m_values.Length; i++) { result.m_values[i] = sym1.m_value - sym2.m_values[i]; } } else { result.m_value = sym1.m_value - sym2.m_value; } break; default: result.m_type = ExpressionType.Error; result.m_name = "Undefine operator: " + opr.m_name + "."; break; } return(result); }