示例#1
0
        /// <summary>
        /// Computation of all Damage Extent of a Damage Potential in a given Intensity
        /// </summary>
        /// <param name="mapObj">Damage Potential</param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        public static DamageExtent computeDamageExtent(MappedObject mapObj, Intensity intensity)//, List<MappedObject> clippedObjects = null)
        {
            var _damageExtent = new DamageExtent()
            {
                Intensity    = (Intensity)intensity.Clone(),     //make sure shallow copy is used
                MappedObject = (MappedObject)mapObj.Clone(),     //make sure shallow copy is used
                Clipped      = mapObj.IsClipped,
            };

            if (mapObj.point != null)
            {
                _damageExtent.geometry = mapObj.point;
            }
            if (mapObj.line != null)
            {
                _damageExtent.geometry = mapObj.line;
            }
            if (mapObj.polygon != null)
            {
                _damageExtent.geometry = mapObj.polygon;
            }

            int _intensityDegree = intensity.IntensityDegree; //0=high, 1=med, 2=low

            //Merge Objectparameter with Freefillparamter
            var _mergedObjParam = MappedObjectController.getMergedObjectParameter(mapObj);

            //get Objectparameters for NatHazard (Vulnerability, Mortality, indirect costs)
            int _motherObjectID = _mergedObjParam.MotherObjectparameter != null
                                    ? _mergedObjParam.MotherObjectparameter.ID
                                    : _mergedObjParam.ID;

            ObjectparameterPerProcess _objectParamProcess;

            _objectParamProcess = _mergedObjParam.ProcessParameters
                                  .Where(pp => pp.NatHazard.ID == intensity.NatHazard.ID &&
                                         pp.Objectparameter.ID == _motherObjectID)
                                  .SingleOrDefault();

            if (_objectParamProcess == null)
            {
                _damageExtent.Log += $"ERROR: NO PROCESS PARAMETER, count: {_mergedObjParam.ProcessParameters.Count} \n";
                return(_damageExtent);
            }

            //get pra for intensity
            var _intensityController = new IntensityController();
            PrA _prA = _intensityController.getPrA(intensity);

            if (_prA == null)
            {
                _damageExtent.Log += $"ERROR: NO PrA VALUES FOUND FOR PROCESS {intensity.NatHazard.Name.ToUpper()} \n";
                return(_damageExtent);
            }


            // BUILDINGS and SPECIAL BUILDINGS
            if (_mergedObjParam.ObjectClass.ID <= 2)
            {
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                // PERSON DAMAGE

                double _deaths     = _prA.Value * _mergedObjParam.Personcount * _mergedObjParam.Presence / 24.0;
                string _logDeaths1 = $"Deaths = prA * PersonCount * Presence/24";
                string _logDeaths2 = $"Deaths = {_prA.Value:F3} * {_mergedObjParam.Personcount} * {_mergedObjParam.Presence:F1} / 24";

                double _deathProbability = _mergedObjParam.Personcount > 0 ? 1.0d / _mergedObjParam.Personcount : 0;
                string _logDProb1        = _mergedObjParam.Personcount > 0 ? $"IndividualDeathRisk = 1 / PersonCount" : "ERROR: 1 / PersonCount";
                string _logDProb2        = _mergedObjParam.Personcount > 0 ? $"IndividualDeathRisk =  1 / {_mergedObjParam.Personcount}" : $"ERROR: 1 / {_mergedObjParam.Personcount}";
                if (_mergedObjParam.Personcount < 1)
                {
                    _damageExtent.Log = $"{ResModel.DE_PersonCount} = {_mergedObjParam.Personcount} \n";
                }

                //switching on intensity degree
                switch (_intensityDegree)
                {
                case 0:
                    _deaths     *= _objectParamProcess.MortalityHigh;
                    _logDeaths1 += $" * MortalityHigh";
                    _logDeaths2 += $" * {_objectParamProcess.MortalityHigh:F3}";
                    break;

                case 1:
                    _deaths     *= _objectParamProcess.MortalityMedium;
                    _logDeaths1 += $" * MortalityMedium";
                    _logDeaths2 += $" * {_objectParamProcess.MortalityMedium:F3}";
                    break;

                case 2:
                    _deaths     *= _objectParamProcess.MortalityLow;
                    _logDeaths1 += $" * MortalityLow";
                    _logDeaths2 += $" * {_objectParamProcess.MortalityLow:F3}";
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(_intensityDegree), _intensityDegree, "out of range");
                }

                //looking for floors count, if available for this object type
                if (_mergedObjParam.HasProperties.Where(m => m.Property == nameof(_mergedObjParam.Floors)).Any())
                {
                    _deaths     *= _mergedObjParam.Floors;
                    _logDeaths1 += $" * Floors";
                    _logDeaths2 += $" * {_mergedObjParam.Floors}";

                    if (_mergedObjParam.Floors > 0)
                    {
                        _deathProbability /= _mergedObjParam.Floors;
                        _logDProb1        += $" / Floors";
                        _logDProb2        += $" / {_mergedObjParam.Floors}";
                    }
                    else
                    {
                        _damageExtent.Log += $"{ResModel.DE_Floors} = {_mergedObjParam.Floors} \n";
                    }
                }

                _damageExtent.Deaths    = _deaths;
                _damageExtent.LogDeaths = _logDeaths1 + ";\n" + _logDeaths2;

                _deathProbability *= _deaths;
                _logDProb1        += $" * Deaths";
                _logDProb2        += $" * {_deaths:F6}";
                _damageExtent.DeathProbability    = _deathProbability;
                _damageExtent.LogDeathProbability = _logDProb1 + ";\n" + _logDProb2;

                _damageExtent.PersonDamage     = _deaths * WillingnessToPay;
                _damageExtent.LogPersonDamage  = $"PersonDamage = Deaths * WillingnessToPay;\n";
                _damageExtent.LogPersonDamage += $"PersonDamage = {_deaths:F6} * {WillingnessToPay:C}";


                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                // PROPERTY DAMAGE

                double _propertyDamage     = _prA.Value * _mergedObjParam.Value;
                string _logPropertyDamage1 = $"PropertyDamage = prA * Value";
                string _logPropertyDamage2 = $"PropertyDamage = {_prA.Value:F3} * {_mergedObjParam.Value:C}";

                switch (_mergedObjParam.FeatureType)
                {
                case 0:     //POINT BASED OBJECT(like communication tower)
                    _damageExtent.Piece  = 1;
                    _logPropertyDamage1 += $" * Piece";
                    _logPropertyDamage2 += $" * 1";
                    if (_damageExtent.Clipped)
                    {
                        _damageExtent.Part = 1.0d;
                    }

                    break;

                case 1:     //LINE BASED OBJECT (like Aduccion)
                    _damageExtent.Length = mapObj.line.Length;
                    if (_damageExtent.Clipped)
                    {
                        var rawMapObject = MappedObjectController.ActiveSession.Load <MappedObject>(mapObj.ID);
                        _damageExtent.Part = mapObj.line.Length / rawMapObject.line.Length;
                    }

                    _propertyDamage     *= mapObj.line.Length;
                    _logPropertyDamage1 += $" * Length";
                    _logPropertyDamage2 += $" * {mapObj.line.Length:F3}";
                    break;

                case 2:     //POLYGON BASED OBJECT
                    _damageExtent.Area = mapObj.polygon.Area;
                    if (_damageExtent.Clipped)
                    {
                        var rawMapObject = MappedObjectController.ActiveSession.Load <MappedObject>(mapObj.ID);
                        _damageExtent.Part = mapObj.polygon.Area / rawMapObject.polygon.Area;
                    }

                    _propertyDamage     *= mapObj.polygon.Area;
                    _logPropertyDamage1 += $" * Area";
                    _logPropertyDamage2 += $" * {mapObj.polygon.Area:F3}";
                    break;

                default:
                    _damageExtent.Log += $"ERROR: BUILDING, FEATURETYPE = {_mergedObjParam.ObjectClass.ID}, {_mergedObjParam.FeatureType} \n";
                    return(_damageExtent);
                }

                //looking for floors count, if available for this object type
                if (_mergedObjParam.HasProperties.Where(m => m.Property == nameof(_mergedObjParam.Floors)).Any())
                {
                    _propertyDamage     *= _mergedObjParam.Floors;
                    _logPropertyDamage1 += $" * Floors";
                    _logPropertyDamage2 += $" * {_mergedObjParam.Floors}";
                }

                switch (_intensityDegree)
                {
                case 0:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityHigh;
                    _logPropertyDamage1 += $" * VulnerabilityHigh";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityHigh:F3}";

                    break;

                case 1:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityMedium;
                    _logPropertyDamage1 += $" * VulnerabilityMedium";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityMedium:F3}";
                    break;

                case 2:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityLow;
                    _logPropertyDamage1 += $" * VulnerabilityLow";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityLow:F3}";
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(_intensityDegree), _intensityDegree, "out of range");
                }

                _damageExtent.PropertyDamage    = _propertyDamage;
                _damageExtent.LogPropertyDamage = _logPropertyDamage1 + ";\n" + _logPropertyDamage2;

                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            }
            // INFRASTRUCTURE
            else if (_mergedObjParam.ObjectClass.ID == 3)
            {
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                // PERSON DAMAGE

                // STREETS / BRIDGES
                // LINE OBJECT
                if (_mergedObjParam.FeatureType == 1)
                {
                    if (_mergedObjParam.Personcount == 0)
                    {
                        _damageExtent.Deaths    = 0;
                        _damageExtent.LogDeaths = "no person damage";

                        _damageExtent.DeathProbability    = 0;
                        _damageExtent.LogDeathProbability = "no person damage";

                        _damageExtent.PersonDamage    = 0;
                        _damageExtent.LogPersonDamage = "no person damage";
                    }
                    else
                    {
                        _damageExtent.Length = mapObj.line.Length;

                        double _deaths = _prA.Value * _mergedObjParam.Personcount *
                                         (double)_mergedObjParam.NumberOfVehicles * _damageExtent.Length / (double)_mergedObjParam.Velocity / 24000.0d;

                        string _logDeaths1 = $"Deaths = prA * PersonCount * NumberOfVehicles * Length / Velocity / 24000";
                        string _logDeaths2 = $"Deaths = {_prA.Value:F3} * {_mergedObjParam.Personcount} * {_mergedObjParam.NumberOfVehicles} * {_damageExtent.Length:F3} / {_mergedObjParam.Velocity} / 24000";

                        switch (_intensityDegree)
                        {
                        case 0:
                            _deaths     *= _objectParamProcess.MortalityHigh;
                            _logDeaths1 += $" * MortalityHigh";
                            _logDeaths2 += $" * {_objectParamProcess.MortalityHigh:F3}";
                            break;

                        case 1:
                            _deaths     *= _objectParamProcess.MortalityMedium;
                            _logDeaths1 += $" * MortalityMedium";
                            _logDeaths2 += $" * {_objectParamProcess.MortalityMedium:F3}";
                            break;

                        case 2:
                            _deaths     *= _objectParamProcess.MortalityLow;
                            _logDeaths1 += $" * MortalityLow";
                            _logDeaths2 += $" * {_objectParamProcess.MortalityLow:F3}";
                            break;

                        default:
                            throw new ArgumentOutOfRangeException(nameof(_intensityDegree), _intensityDegree, "out of range");
                        }

                        _damageExtent.Deaths    = _deaths;
                        _damageExtent.LogDeaths = _logDeaths1 + ";\n" + _logDeaths2;

                        int    _passagesSamePerson = 4; //TODO: HARD CODED
                        double _deathProbability   = _deaths * _passagesSamePerson / _mergedObjParam.NumberOfVehicles / _mergedObjParam.Personcount;
                        string _logDProb1          = $"IndivudualDeathRisk = Deaths * PassagesSamePerson / NumberOfVehicles / PersonCount";
                        string _logDProb2          = $"IndivudualDeathRisk = {_deaths:F6} * {_passagesSamePerson} / {_mergedObjParam.NumberOfVehicles} / {_mergedObjParam.Personcount}";
                        _damageExtent.DeathProbability    = _deathProbability;
                        _damageExtent.LogDeathProbability = _logDProb1 + ";\n" + _logDProb2;

                        _damageExtent.PersonDamage     = _deaths * WillingnessToPay;
                        _damageExtent.LogPersonDamage  = $"PersonDamage = Deaths * WillingnessToPay;\n";
                        _damageExtent.LogPersonDamage += $"PersonDamage = {_deaths:F6} * {WillingnessToPay:C}";
                    }
                }
                // TOWERS
                // POINT OBJECT
                else if (_mergedObjParam.FeatureType == 0)
                {
                    _damageExtent.Deaths    = 0;
                    _damageExtent.LogDeaths = "no person damage";

                    _damageExtent.DeathProbability    = 0;
                    _damageExtent.LogDeathProbability = "no person damage";

                    _damageExtent.PersonDamage    = 0;
                    _damageExtent.LogPersonDamage = "no person damage";
                }
                // POLYGON NOT IMPLEMENTED
                else
                {
                    _damageExtent.Log += $"ERROR: Feature type not implemented";
                    return(_damageExtent);
                }


                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                // PROPERTY DAMAGE

                double _propertyDamage     = _prA.Value * _mergedObjParam.Value;
                string _logPropertyDamage1 = $"PropertyDamage = prA * Value";
                string _logPropertyDamage2 = $"PropertyDamage = {_prA.Value:F3} * {_mergedObjParam.Value:C}";

                switch (_mergedObjParam.FeatureType)
                {
                case 0:     //POINT BASED OBJECT (like towers)
                    _damageExtent.Piece  = 1;
                    _logPropertyDamage1 += $" * Piece";
                    _logPropertyDamage2 += $" * 1";
                    break;

                case 1:     //LINE BASED OBJECT (like streets)
                    _damageExtent.Length = mapObj.line.Length;

                    _propertyDamage     *= mapObj.line.Length;
                    _logPropertyDamage1 += $" * Length";
                    _logPropertyDamage2 += $" * {mapObj.line.Length:F3}";
                    break;

                default:
                    _damageExtent.Log += $"ERROR: Infrastructure, FEATURETYPE = {_mergedObjParam.FeatureType} \n";
                    return(_damageExtent);
                }

                switch (_intensityDegree)
                {
                case 0:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityHigh;
                    _logPropertyDamage1 += $" * VulnerabilityHigh";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityHigh:F3}";
                    break;

                case 1:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityMedium;
                    _logPropertyDamage1 += $" * VulnerabilityMedium";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityMedium:F3}";
                    break;

                case 2:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityLow;
                    _logPropertyDamage1 += $" * VulnerabilityLow";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityLow:F3}";
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(_intensityDegree), _intensityDegree, "out of range");
                }

                _damageExtent.PropertyDamage    = _propertyDamage;
                _damageExtent.LogPropertyDamage = _logPropertyDamage1 + ";\n" + _logPropertyDamage2;

                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            }
            // AGRICULTURE
            else if (_mergedObjParam.ObjectClass.ID == 4)
            {
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                // PERSON DAMAGE

                _damageExtent.Deaths    = 0;
                _damageExtent.LogDeaths = "no person damage";

                _damageExtent.DeathProbability    = 0;
                _damageExtent.LogDeathProbability = "no person damage";

                _damageExtent.PersonDamage    = 0;
                _damageExtent.LogPersonDamage = "no person damage";

                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                // PROPERTY DAMAGE

                double _propertyDamage     = _prA.Value * _mergedObjParam.Value; //value per hectare!!!
                string _logPropertyDamage1 = $"PropertyDamage = prA * Value";
                string _logPropertyDamage2 = $"PropertyDamage = {_prA.Value:F3} * {_mergedObjParam.Value:C}";

                switch (_mergedObjParam.FeatureType)
                {
                case 2:                                                  //POLYGON BASED OBJECT
                    _damageExtent.Area   = mapObj.polygon.Area;
                    _propertyDamage     *= _damageExtent.Area / 10000.0; //in hectare!
                    _logPropertyDamage1 += $" * Area / 10000";
                    _logPropertyDamage2 += $" * {(_damageExtent.Area / 10000.0):F3}";
                    break;

                default:
                    _damageExtent.Log += $"ERROR: Agriculture, FEATURETYPE = {_mergedObjParam.FeatureType} \n";
                    return(_damageExtent);
                }

                switch (_intensityDegree)
                {
                case 0:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityHigh;
                    _logPropertyDamage1 += $" * VulnerabilityHigh";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityHigh:F3}";
                    break;

                case 1:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityMedium;
                    _logPropertyDamage1 += $" * VulnerabilityMedium";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityMedium:F3}";
                    break;

                case 2:
                    _propertyDamage     *= _objectParamProcess.VulnerabilityLow;
                    _logPropertyDamage1 += $" * VulnerabilityLow";
                    _logPropertyDamage2 += $" * {_objectParamProcess.VulnerabilityLow:F3}";
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(_intensityDegree), _intensityDegree, "out of range");
                }

                _damageExtent.PropertyDamage    = _propertyDamage;
                _damageExtent.LogPropertyDamage = _logPropertyDamage1 + ";\n" + _logPropertyDamage2;

                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            }

            else
            {
                _damageExtent.Log += $"ERROR: OBJECT CLASS = {_mergedObjParam.ObjectClass.ID} \n";
                return(_damageExtent);
            }

            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            // RESILIENCE FACTOR
            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            if (mapObj.ResilienceValues != null && mapObj.ResilienceValues.Any())
            {
                Tuple <double, string> _resilience = MappedObjectController.computeResilienceFactor(mapObj.ResilienceValues.ToList(), intensity);

                _damageExtent.ResilienceFactor    = _resilience.Item1;
                _damageExtent.LogResilienceFactor = _resilience.Item2;
            }
            else
            {
                _damageExtent.ResilienceFactor    = 0;
                _damageExtent.LogResilienceFactor = "no resilience available";
            }

            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            // INDIRECT DAMAGE
            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            double _indirectDamage     = 0;
            string _logIndirectDamage1 = "";
            string _logIndirectDamage2 = "";

            if (_mergedObjParam.ObjectClass.ID <= 3)    //Value = 1$
            {
                _indirectDamage     = _prA.Value;
                _logIndirectDamage1 = $"IndirectDamage = prA";
                _logIndirectDamage2 = $"IndirectDamage = {_prA.Value:F3}";
            }
            else //Value according to DB
            {
                _indirectDamage     = _prA.Value * _objectParamProcess.Value;
                _logIndirectDamage1 = $"IndirectDamage = prA * Value";
                _logIndirectDamage2 = $"IndirectDamage = {_prA.Value:F3} * {_objectParamProcess.Value:C}";
            }

            //if (_mergedObjParam.ObjectClass.ID != 3) //not available for infrastructure
            {
                switch (_intensityDegree) //0=high, 1=med, 2=low
                {
                case 0:
                    _indirectDamage     *= _objectParamProcess.DurationHigh;
                    _logIndirectDamage1 += $" * DurationHigh";
                    _logIndirectDamage2 += $" * {_objectParamProcess.DurationHigh:F0}";
                    break;

                case 1:
                    _indirectDamage     *= _objectParamProcess.DurationMedium;
                    _logIndirectDamage1 += $" * DurationMedium";
                    _logIndirectDamage2 += $" * {_objectParamProcess.DurationMedium:F0}";
                    break;

                case 2:
                    _indirectDamage     *= _objectParamProcess.DurationLow;
                    _logIndirectDamage1 += $" * DurationLow";
                    _logIndirectDamage2 += $" * {_objectParamProcess.DurationLow:F0}";
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(_intensityDegree), _intensityDegree, "out of range");
                }

                //Building and Special Objects
                if (_mergedObjParam.ObjectClass.ID <= 2)
                {
                    // staff property indicates if indirect damage is available
                    if (_mergedObjParam.HasProperties.Where(m => m.Property == nameof(_mergedObjParam.Staff)).Any())
                    {
                        _indirectDamage     *= (double)_mergedObjParam.Staff;
                        _logIndirectDamage1 += $" * Loss/day";
                        _logIndirectDamage2 += $" * {_mergedObjParam.Staff:F0}";

                        if (_damageExtent.Clipped)
                        {
                            _indirectDamage     *= _damageExtent.Part;
                            _logIndirectDamage1 += $" * PartOfLoss";
                            _logIndirectDamage2 += $" * {_damageExtent.Part:F2}";
                        }

                        if (_mergedObjParam.Staff <= 0)
                        {
                            _damageExtent.Log += $"{ResModel.DE_Staff} = {_mergedObjParam.Staff} \n";
                        }
                    }
                    else // Buildings without indirect damage
                    {
                        _indirectDamage     = 0;
                        _logIndirectDamage1 = $"no indirect damage";
                        _logIndirectDamage2 = "";
                    }
                }
                //Agriculture
                else if (_mergedObjParam.ObjectClass.ID == 4)
                {
                    _indirectDamage     *= _damageExtent.Area / 10000.0; //in hectare!
                    _logIndirectDamage1 += $" * Area / 10000";
                    _logIndirectDamage2 += $" * {_damageExtent.Area / 10000.0:F3}";
                }
                //Infrastructure
                else if (_mergedObjParam.ObjectClass.ID == 3)
                {
                    // duration indicates if indirect damage is available
                    if ((_objectParamProcess.DurationHigh + _objectParamProcess.DurationMedium + _objectParamProcess.DurationLow) > 0)
                    {
                        _indirectDamage     *= (double)_mergedObjParam.Staff;
                        _logIndirectDamage1 += $" * Loss/day";
                        _logIndirectDamage2 += $" * {_mergedObjParam.Staff:F0}";

                        //TODO
                    }
                    else // Infrastructure without indirect damage
                    {
                        _indirectDamage     = 0;
                        _logIndirectDamage1 = $"no indirect damage";
                        _logIndirectDamage2 = "";
                    }
                }
            }
            //else //Infrastructure
            //{
            //    _indirectDamage = 0;
            //    _logIndirectDamage1 = $"no indirect damage";
            //    _logIndirectDamage2 = "";
            //}

            _damageExtent.IndirectDamage    = _indirectDamage;
            _damageExtent.LogIndirectDamage = _logIndirectDamage1 + ";\n" + _logIndirectDamage2;

            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            // if no errors reported
            if (String.IsNullOrWhiteSpace(_damageExtent.Log))
            {
                _damageExtent.Log = "OK \n";
            }

            return(_damageExtent);
        }
        /// <summary>
        /// Delete all damage extents of this project in database and recreate them
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public void createDamageExtent(int projectId)
        {
            Stopwatch _stopWatch = new Stopwatch();

            _stopWatch.Start();

            //DBManager.NewSession(); //bac session

            //ConcurrentBag<DamageExtent> _saveDamageExtents = new ConcurrentBag<DamageExtent>();
            List <DamageExtent> _saveDamageExtents = new List <DamageExtent>();

            var _damageExtentController = new DamageExtentController();

            _damageExtentController.deleteDamageExtentsFromDB(projectId);  //DELETE

            var         _hazards       = ActiveSession.QueryOver <NatHazard>().List <NatHazard>();
            var         _ikClasses     = ActiveSession.QueryOver <IKClasses>().List <IKClasses>();
            List <bool> _beforeActions = new List <bool>()
            {
                true, false
            };

            Logging.warn($"querying: elapsed time = " + _stopWatch.Elapsed.ToString());
            _stopWatch.Restart();

            var _damagePotentialController   = new MappedObjectController();
            var _allAffectedDamagePotentials = _damagePotentialController.getAllDamagePotentials(projectId); //unprocessed Damage Potentials in the project perimeter

            Logging.warn($"getAllDamagePotentials: elapsed time = " + _stopWatch.Elapsed.ToString() + $", count= {_allAffectedDamagePotentials.Count()}");
            _stopWatch.Restart();

            foreach (var hazard in _hazards)
            {
                foreach (var period in _ikClasses)
                {
                    foreach (var beforeAction in _beforeActions)
                    {
                        _stopWatch.Restart();

                        var _controller = new IntensityController();

                        List <Intensity>    _intensities = _controller.getIntensityMap(projectId, hazard.ID, period.ID, beforeAction);
                        List <MappedObject> _allProcessedDamagePotentials = new List <MappedObject>();

                        Logging.warn($"getIntensityMap: elapsed time = " + _stopWatch.Elapsed.ToString());
                        _stopWatch.Restart();

                        if (_intensities == null || _intensities.Count() == 0)
                        {
                            continue;
                        }
                        Stopwatch _damageWatch = new Stopwatch();
                        _damageWatch.Start();

                        // gather all processed DamagePotentials
                        foreach (var intensity in _intensities)
                        {
                            _damageWatch.Restart();

                            IList <MappedObject> dpListWithin = _damagePotentialController.getDamagePotentialsWithin(intensity, _allAffectedDamagePotentials);
                            List <MappedObject>  outlist      = dpListWithin.ToList();

                            Logging.warn($">getDamagePotentialsWithin: elapsed time = " + _damageWatch.Elapsed.ToString());
                            _damageWatch.Restart();

                            //IList<MappedObject> dpListWithin2 = _damagePotentialController.getDamagePotentialsWithin2(intensity, projectId);
                            //List<MappedObject> outlist = dpListWithin2.ToList();
                            //Logging.warn($">getDamagePotentialsWithin2: elapsed time = " + _damageWatch.Elapsed.ToString());
                            //_damageWatch.Restart();

                            IList <MappedObject> dpListCrossing = _damagePotentialController.getDamagePotentialCrossing(intensity, _allAffectedDamagePotentials);
                            outlist.AddRange(dpListCrossing); //Merge Within and Crossing for Intensity

                            Logging.warn($">getDamagePotentialCrossing: elapsed time = " + _damageWatch.Elapsed.ToString());
                            _damageWatch.Restart();

                            //IList<MappedObject> dpListCrossing2 = _damagePotentialController.getDamagePotentialCrossing2(intensity, projectId);
                            //outlist.AddRange(dpListCrossing2); //Merge Within and Crossing for Intensity
                            //Logging.warn($">getDamagePotentialCrossing2: elapsed time = " + _damageWatch.Elapsed.ToString());
                            //_damageWatch.Restart();

                            _allProcessedDamagePotentials.AddRange(outlist); //collect all processed DamagePotentials
                        }
                        _damageWatch.Stop();

                        Logging.warn($"getDamagePotential: elapsed time = " + _stopWatch.Elapsed.ToString());
                        _stopWatch.Restart();

                        //Parallel.ForEach(_intensities,
                        //    (intensity) =>
                        //    {

                        foreach (var intensity in _intensities)
                        {
                            var outlist = _allProcessedDamagePotentials.Where(o => o.Intensity.ID == intensity.ID).ToList();

                            List <DamageExtent> outDamageExtents = new List <DamageExtent>();
                            //ConcurrentBag<DamageExtent> outDamageExtents = new ConcurrentBag<DamageExtent>();

                            //Parallel.ForEach(outlist, (damagePotential) =>
                            //{

                            foreach (MappedObject damagePotential in outlist)
                            {
                                DamageExtent damageExtent = null;

                                damageExtent = DamageExtentController.computeDamageExtent(damagePotential, intensity);

                                if (damageExtent != null)
                                {
                                    outDamageExtents.Add(damageExtent);
                                }
                            }
                            //});

                            _saveDamageExtents.AddRange(outDamageExtents);
                            //_saveDamageExtents.AddRange<DamageExtent>(outDamageExtents);

                            //double _sumPersonDamage = outDamageExtents.Sum(x => x.PersonDamage);
                            //double _sumDeaths = outDamageExtents.Sum(x => x.Deaths);
                            //double _sumProptertyDamage = outDamageExtents.Sum(x => x.PropertyDamage);

                            //Logging.warn($"  #DamageExtent: {outlist.Count}");
                        } //loop over intensities
                          //});

                        Logging.warn($"computeDamageExtent: elapsed time = " + _stopWatch.Elapsed.ToString());
                        _stopWatch.Restart();
                    } //loop over actions
                }     //loop over period
            }         //loop over hazard

            Logging.warn($"inbetween: elapsed time = " + _stopWatch.Elapsed.ToString());
            _stopWatch.Restart();

            //one time saving to db <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            _damageExtentController.saveDamageExtentToDB(_saveDamageExtents.ToList()); //SAVE

            Logging.warn($"saveDamageExtentToDB: elapsed time = " + _stopWatch.Elapsed.ToString());
            _stopWatch.Restart();

            _stopWatch.Stop();


            //Change the project to state "Calculated"
            var _resultController = new ResultController();

            _resultController.setProjectStatus(projectId, 2);
        }