using System; using System.IO; using Deltares.Mathematics; // ======================================================================================================================= // Class name: TDGlobals // // Description: Class that handles probabilistic calculations for all programmes. // // Date ID Modification // 2003-06-03 Best Created // 2004-03-23 Best Append Extra information // 2004-10-14 Sel Added MeasurementData and IsForm // 2004-10-15 Sel Implemented TPredictionEvent // 2004-10-19 Sel Implemented TFetchVariableInstantsEvent // 2004-10-21 Best Uncertaintybandcalculation added // 2004-10-22 Sel Implemented PredictionCovariance; // Replaced Band by Bound; removed TFetchVariableInstantsEvent. // 2004-11-10 Best Zvalues replaced by boundvalues // 2004-11-11 Best property stdReading added // 2004-12-24 Tuk Added prediction progress property // 2005-01-05 Tuk Coupled progress event for Monte Carlo // 2005-02-21 Best DAta for testdump added // 2005-02-23 Best ParameterCovarianceUpdate added // 2008-10-13 Best If no convergence use the values from the smaller Z found // ======================================================================================================================= namespace Deltares.Stability.Calculation.Inner { public class EDProbCalculationError : Exception { //@ Constructor auto-generated public EDProbCalculationError(String message) : base(message) {} //@ Constructor auto-generated public EDProbCalculationError(String message, Exception innerException) : base(message, innerException) {} } // end EDProbCalculationError public class TProbCalculation { public TZfunctiePointerType ZFunction = null; public TZfunctieBoundPointerType ZFunctionBound = null; private bool FIsDars = false; private bool FIsFOSM = false; private bool FIsForm = false; private bool FIsMonteCarlo = false; private bool FIsTestDump = false; private double[] FMeasurementTimes; private TPredictionEvent[] FOnPrediction; private TProbProgessEvent FOnProbProgress = null; private TMatrix FParameterCovarianceUpdate = null; private double FStdReading = 0; private string FTestFileName = String.Empty; public TProbCalculation() : base() { // Initialize FParameterCovarianceUpdate = null; } public bool IsTestDump { get { return FIsTestDump; } set { FIsTestDump = value; } } public string TestFileName { get { return FTestFileName; } set { FTestFileName = value; } } public double[] MeasurementTimes { get { return FMeasurementTimes; } set { FMeasurementTimes = value; } } public TMatrix ParameterCovarianceUpdate { get { return FParameterCovarianceUpdate; } set { FParameterCovarianceUpdate = value; } } public double StdReading { get { return FStdReading; } set { FStdReading = value; } } public bool IsMonteCarlo { get { return FIsMonteCarlo; } set { FIsMonteCarlo = value; } } public bool IsForm { get { return FIsForm; } set { FIsForm = value; } } public bool IsFOSM { get { return FIsFOSM; } set { FIsFOSM = value; } } public bool IsDars { get { return FIsDars; } set { FIsDars = value; } } public TPredictionEvent[] OnPrediction { get { return FOnPrediction; } set { FOnPrediction = value; } } public TProbProgessEvent OnProbProgress { get { return FOnProbProgress; } set { FOnProbProgress = value; } } // ======================================================================================================================= // // Date ID Modification // 2004-01-07 Best Created // 2005-01-05 Tuk Coupled progress event // ======================================================================================================================= public void StartCalculation(int NTot, int[] Verdeling, double[] Ex, double[] Sx, ref double[][] Vp, ref double[][] Covariantie, int NumCor, double[][] Eivec, int NumEivec, ref double StuurPar, ref double Beta, ref double[] x, ref double[] u, ref double[] zes, ref int ierror, ref int izs) { // LMonteCarlo: TDMonteCarlo; TDForm4 LTheForm; // LProbab: TDProbab; zonder probabdll uitzetten string LStr; try { // ... a monte carlo calculation ... // (* if (FIsMonteCarlo) then // begin // LMonteCarlo := TDMonteCarlo.Create; // try // LMonteCarlo.OnProgress := FOnProbProgress; // LMonteCarlo.NumberOfDrawings := Round(Stuurpar); // LMonteCarlo.ZFunctie := ZFunction; // LMonteCarlo.Calculate(NTot, Verdeling, Ex, Sx, Vp, Covariantie, // NumCor, Eivec, NumEivec, StuurPar, Beta, x, u, zes, ierror, Izs, lfs); // finally // LMonteCarlo.Free; // end; // end; // *) // ... a Form calculation ... if (FIsForm) { LTheForm = new TDForm4(); LTheForm.GG = ZFunction; // LTheForm.GiveProgress := ProgresFunction; try { LTheForm.FORM4(NTot, Verdeling, Ex, Sx, ref Vp, ref Covariantie, NumCor, Eivec, NumEivec, ref StuurPar, ref Beta, ref x, ref u, ref zes, ref ierror, ref izs); } catch (Exception E) { LStr = E.Message; throw E; } if ((ierror == 300000)) { // No convergence found // Use values of minimal z found Beta = LTheForm.MinBeta; x = (double[]) LTheForm.MinEX.Clone(); u = (double[]) LTheForm.MinU.Clone(); } } } catch (Exception E) { LStr = E.Message; throw E; } // ... A Dars calculation ... // (* // {Dars is not working poperly. If this is fixed a dll must be made from dars} // if (FIsDars) then // begin // LProbab := TDProbab.Create; // try // GDProbab.ZFunction := ZFunction; // GDProbab.ProbMethod := DS; // GDProbab.MaxIter := 30; // GDProbab.MaxFit := 3; // GDProbab.Maxsamp := 100; // GDProbab.Variat := 0.3; // GDProbab.Lambda := 3.0; // GDProbab.LSConv := 0.01; // // {verplicht geeft parameters } // LProbab.CallProbab(NTot, Verdeling, Ex, Sx, VP[3]); // finally // LProbab.Free; // end; // end; // *) } // ======================================================================================================================= // // Date ID Modification // 2004-01-07 Best Created // 2005-01-05 Tuk Coupled progress event // ======================================================================================================================= public void StartUncertaintyBoundCalculation(int NTot, int[] Verdeling, double[] Ex, double[] Sx, ref double[] Vp, ref double[] Covariantie, int NumCor, double[] Eivec, int NumEivec, ref double StuurPar, ref double Beta, ref double[] x, ref double[] u, ref TBoundCalculationRec[] ABoundValues, ref TExceedingRec[] AExceeding, ref TExceedingRec[] AExceedingBound, ref int ierror, ref int Izs, ref Stream lfs) { // LMonteCarlo: TDMonteCarlo; // LFOSM: TDFOSM; // ... a monte carlo calculation ... // (* if (FIsMonteCarlo) then // begin // LMonteCarlo := TDMonteCarlo.Create; // try // LMonteCarlo.OnProgress := FOnProbProgress; // LMonteCarlo.NumberOfDrawings := Round(Stuurpar); // LMonteCarlo.ZFunctionBound := ZFunctionBound; // LMonteCarlo.CalculateCombined(NTot, Verdeling, Ex, Sx, Vp, Covariantie, // NumCor, Eivec, NumEivec, stuurpar, Beta, X, U, ABoundValues, AExceeding, // AExceedingBound, ierror, Izs, lfs); // // finally // LMonteCarlo.Free; // end; // end; // *) // ... a FOSM calculation ... // (* if (FIsFOSM) then // begin // LFOSM := TDFOSM.Create; // LFOSM.MeasurementTimes := FMeasurementTimes; // try // LFOSM.OnPrediction := FOnPrediction; // LFOSM.OnPredictionProgress := FOnProbProgress; // // LFOSM.CalculateBound(NTot, Verdeling, Ex, Sx, Vp, Covariantie, // NumCor, Eivec, NumEivec, StuurPar, Beta, x, u, ABoundValues, ierror, Izs, lfs); // if Assigned(LFosm.ParameterCovarianceUpdate) then // begin // // if (FIsTestDump) then // LFosm.ParameterCovarianceUpdate.WriteToFile(LOutFile, 12, 5); // // FParameterCovarianceUpdate := LFosm.ParameterCovarianceUpdate.Duplicate; // end; // finally // LFOSM.Free; // end; // end; // *) } // ======================================================================================================================= // Date ID Modification // 2004-12-08 Best Created // ======================================================================================================================= public string GetFormError(int IError) { string result = ""; bool LErrorFound = false; LErrorFound = false; if ((IError == 1)) { result = Strings.sAllPartZero[Strings.GLI]; LErrorFound = true; } if ((IError == 111000)) { result = Strings.sUnKownDistribution[Strings.GLI]; LErrorFound = true; } if ((IError == 112000)) { result = Strings.sNegStdev[Strings.GLI]; LErrorFound = true; } if ((IError == 113000)) { result = Strings.sNegStdev[Strings.GLI]; LErrorFound = true; } if ((IError == 121000) || (IError == 122000) || (IError == 123000) || (IError == 124000)) { result = Strings.sMeanOutsideDef[Strings.GLI]; LErrorFound = true; } if ((IError == 131000)) { result = Strings.sAllDistDeterministic[Strings.GLI]; LErrorFound = true; } if ((IError == 143000)) { result = Strings.sMAtrixtoosmall[Strings.GLI]; LErrorFound = true; } if ((IError == 145000)) { result = Strings.sCovMAtrixUndefined[Strings.GLI]; LErrorFound = true; } if ((IError == 300000)) { result = Strings.sNoConvergenceReached[Strings.GLI]; LErrorFound = true; } if (!LErrorFound) { result = Strings.sUnKownFormError[Strings.GLI]; } return result; } } // end TProbCalculation public delegate void TProbProgessEvent(Object Sender, double AProgress); }