Exemple #1
0
        public ImagePsd(string p_FileFullPath)
        {
            if (!File.Exists(p_FileFullPath))
            {
                return;
            }
            FileStream _PSD = File.Open(p_FileFullPath, FileMode.Open);

            byte[] _HeadByte = new byte[26];
            _PSD.Read(_HeadByte, 0, 26);
            m_Head       = new PSDHEAD(_HeadByte);
            m_ColorModel = new ColorModel(_PSD);

            long _ReadCount = _PSD.Position;

            while (true)
            {
                BIM _Bim = new BIM(_PSD);
                if (!_Bim.Read || _PSD.Position - _ReadCount >= m_ColorModel.BIMSize)
                {
                    break;
                }
                m_8BIMList.Add(_Bim);
            }
            m_LayerMaskInfo = new LayerMaskInfo(_PSD);
            m_ImageData     = new ImageData(_PSD, m_Head);
            if (m_Head.ColorMode == 2)
            {
                m_ImageData.PSDImage.Palette = m_ColorModel.ColorData;
            }
            _PSD.Close();
        }
Exemple #2
0
        public BIM.BusinessEntities.VerificadorPLD ServicioVerificadorPLD(BIM.BusinessEntities.BitacoraPLD parametros)
        {
            BIM.BusinessEntities.VerificadorPLD result = null;
            try
            {
                result = (new VerificadorPLDLogic()).ServicioVerificadorPLD(parametros);
            }
            catch (Exception ex)
            {
#if(DEBUG)

                if (Directory.Exists(RepositorioTmpLocal))
                    Directory.CreateDirectory(RepositorioTmpLocal);

                string msg = DateTime.Today.ToShortDateString() + " - " + DateTime.Today.ToShortTimeString() + " Mensaje error: " + ex.Message;

                File.AppendAllText(RepositorioTmpLocal + ArchivoError, msg);                
              
#else
                    EventLogManager.LogErrorEntry("Error en VerificadorPLDServices.ServicioVerificadorPLD: " + ex.Message);
                    //TODO: Codificar envío de notificación de error al EventLog
#endif
            }
            return result;
        }
Exemple #3
0
        public static BIM bimLiftOver(Chain chain,
                                      BIM input)
        {
            string chr = input.Chromosome;
            int    pos = input.Position;

            if (pos > 0)
            {
                foreach (ChainRecord chainRecord in chain.chainRecords)
                {
                    if (chainRecord.tName == ("chr" + chr) &&
                        chainRecord.tStart <= pos &&
                        chainRecord.tEnd >= pos)
                    {
                        int tStart = chainRecord.tStart, qStart = chainRecord.qStart;
                        for (int idx = 0; idx < chainRecord.chainGaps.Count; ++idx)
                        {
                            var chainGap = chainRecord.chainGaps[idx];
                            if (pos < tStart + chainGap.size)
                            {
                                input.Position = pos + (qStart - tStart);
                                break;
                            }
                            tStart += chainGap.size + chainGap.dt;
                            qStart += chainGap.size + chainGap.dq;
                        }
                        break;
                    }
                }
            }
            return(input);
        }
Exemple #4
0
        public BIM.BusinessEntities.VerificadorPLD ServicioVerificadorPLD(BIM.BusinessEntities.BitacoraPLD parametros)
        {
            BIM.BusinessEntities.VerificadorPLD result = null;
            try
            {
                result = (new VerificadorPLDLogic()).ServicioVerificadorPLD(parametros);
            }
            catch (Exception ex)
            {
#if(DEBUG)


                const string fic = @"C:\inetpub\wwwroot\PLD\Prueba.txt";
                string texto = ex.Message;

                System.IO.StreamWriter sw = new System.IO.StreamWriter(fic);
                sw.WriteLine(texto);
                sw.Close();

                
                //Console.WriteLine("Error en VerificadorPLDServices.ServicioVerificadorPLD: " + ex.Message);

                //if (!EventLog.SourceExists(cs))
                //    EventLog.CreateEventSource(cs, "Application");

                //EventLog.WriteEntry(cs, ex.Message, EventLogEntryType.Error);
#else
                    EventLogManager.LogErrorEntry("Error en VerificadorPLDServices.ServicioVerificadorPLD: " + ex.Message);
                    //TODO: Codificar envío de notificación de error al EventLog
#endif
            }
            return result;
        }
Exemple #5
0
        public static BigDec FromString(string v)
        {
            if (v == null)
            {
                throw new FormatException();
            }

            BIM integral = BIM.Zero;
            BIM fraction = BIM.Zero;
            int exponent = 0;

            int len = v.Length;

            int i = v.IndexOf('e');

            if (i >= 0)
            {
                if (i + 1 == v.Length)
                {
                    throw new FormatException();
                }
                exponent = Int32.Parse(v.Substring(i + 1, len - i - 1));
                len      = i;
            }

            int fractionLen = 0;

            i = v.IndexOf('.');
            if (i >= 0)
            {
                if (i + 1 == v.Length)
                {
                    throw new FormatException();
                }
                fractionLen = len - i - 1;
                fraction    = BIM.Parse(v.Substring(i + 1, fractionLen));
                len         = i;
            }

            integral = BIM.Parse(v.Substring(0, len));

            if (!fraction.IsZero)
            {
                while (fractionLen > 0)
                {
                    integral    = integral * ten;
                    exponent    = exponent - 1;
                    fractionLen = fractionLen - 1;
                }
            }

            if (integral.Sign == -1)
            {
                return(new BigDec(integral - fraction, exponent));
            }
            else
            {
                return(new BigDec(integral + fraction, exponent));
            }
        }
Exemple #6
0
        // ``floor`` rounds towards negative infinity (like SMT-LIBv2's to_int).
        /// <summary>
        /// NOTE:  This may give wrong results, it hasn't been tested extensively
        /// If you're getting weird bugs, you may want to check this function out...
        /// Computes the floor and ceiling of this BigFloat. Note the choice of rounding towards negative
        /// infinity rather than zero for floor is because SMT-LIBv2's to_int function floors this way.
        /// </summary>
        /// <param name="floor">The Floor (rounded towards negative infinity)</param>
        /// <param name="ceiling">Ceiling (rounded towards positive infinity)</param>
        public void FloorCeiling(out BIM floor, out BIM ceiling)
        {
            BIM two = new BIM(2);

            BIM sig = Significand + BIM.Pow(two, SignificandSize); //Add hidden bit
            BIM exp = Exponent - BIM.Pow(two, ExponentSize - 1) + 1;

            while (exp > BIM.Zero)
            {
                exp--;
                sig = sig << 1;
            }

            sig = sig >> SignificandSize;

            if (isNeg)
            {
                ceiling = -sig + 1;
                floor   = -sig;
            }
            else
            {
                ceiling = sig + 1;
                floor   = sig;
            }
        }
Exemple #7
0
        public override string /*!*/ ToString()
        {
            Contract.Ensures(Contract.Result <string>() != null);
            if (value == "")
            {
                byte[]        sigBytes = significand.ToByteArray();
                StringBuilder binSig   = new StringBuilder();

                if (exponent == 0)
                {
                    binSig.Append('0');
                }
                else
                {
                    binSig.Append('1'); //hidden bit
                }

                for (int i = significandSize - 2; i >= 0; --i) //little endian
                {
                    if (i / 8 < sigBytes.Length)
                    {
                        binSig.Append((char)('0' + ((sigBytes[i / 8] >> (i % 8)) & 1)));
                    }
                    else
                    {
                        binSig.Append('0');
                    }
                }

                BIM bias = two_n(exponentSize - 1) - 1;
                if (exponent == 0)
                {
                    --bias;
                }

                int moveDec  = ((int)((exponent - bias) % 4) + 4) % 4;
                BIM finalExp = (exponent - bias - moveDec) / 4;

                string leftBinSig  = binSig.ToString().Substring(0, moveDec + 1);
                string rightBinSig = binSig.ToString().Substring(moveDec + 1);

                leftBinSig  = new string('0', 4 - leftBinSig.Length % 4) + leftBinSig;
                rightBinSig = rightBinSig + new string('0', 4 - rightBinSig.Length % 4);

                StringBuilder leftHexSig  = new StringBuilder();
                StringBuilder rightHexSig = new StringBuilder();

                for (int i = 0; i < leftBinSig.Length / 4; ++i)
                {
                    leftHexSig.AppendFormat("{0:X}", Convert.ToByte(leftBinSig.Substring(i * 4, 4), 2));
                }
                for (int i = 0; i < rightBinSig.Length / 4; ++i)
                {
                    rightHexSig.AppendFormat("{0:X}", Convert.ToByte(rightBinSig.Substring(i * 4, 4), 2));
                }

                return(String.Format("{0}0x{1}.{2}e{3}f{4}e{5}", isNeg ? "-" : "", leftHexSig, rightHexSig, finalExp, significandSize, exponentSize));
            }
            return(String.Format("0{0}{1}e{2}", value, significandSize, exponentSize));
        }
Exemple #8
0
        public static BigFloat operator +(BigFloat x, BigFloat y)
        {
            //TODO: Modify for correct fp functionality
            Contract.Requires(x.ExponentSize == y.ExponentSize);
            Contract.Requires(x.SignificandSize == y.SignificandSize);
            BIM m1 = x.significand;
            BIM e1 = x.Exponent;
            BIM m2 = y.significand;
            BIM e2 = y.Exponent;

            m1 = m1 + two_n(x.significandSize + 1); //Add implicit bit
            m2 = m2 + two_n(y.significandSize + 1);
            if (e2 > e1)
            {
                m1 = y.significand;
                e1 = y.Exponent;
                m2 = x.significand;
                e2 = x.Exponent;
            }

            while (e2 < e1)
            {
                m2 = m2 / two;
                e2 = e2 + one;
            }

            return(new BigFloat(false, e1, m1 + m2, x.significandSize, x.ExponentSize));
        }
Exemple #9
0
 public static BigNum FromString(string v)
 {
     try {
         return(new BigNum(BIM.Parse(v)));
     } catch (System.ArgumentException) {
         throw new FormatException();
     }
 }
Exemple #10
0
 public BigFloat(bool isNeg, BIM significand, BIM exponent, int significandSize, int exponentSize)
 {
     this.exponentSize    = exponentSize;
     this.exponent        = exponent;
     this.significand     = significand;
     this.significandSize = significandSize + 1;
     this.isNeg           = isNeg;
     this.value           = "";
 }
Exemple #11
0
 public BigFloat(bool isSignBitSet, BIM significand, BIM exponent, int significandSize, int exponentSize)
 {
     this.exponentSize    = exponentSize;
     this.exponent        = exponent;
     this.significand     = significand;
     this.significandSize = significandSize;
     this.isSignBitSet    = isSignBitSet;
     this.value           = "";
 }
Exemple #12
0
        private BIM maxsignificand()
        {
            BIM result = one;

            for (int i = 0; i < significandSize; i++)
            {
                result = result * two;
            }
            return(result - one);
        }
Exemple #13
0
        private static BIM two_n(int n)
        {
            BIM toReturn = one;

            for (int i = 0; i < n; i++)
            {
                toReturn = toReturn * two;
            }
            return(toReturn);
        }
Exemple #14
0
        ////////////////////////////////////////////////////////////////////////////
        // Conversion operations

        /// <summary>
        /// NOTE: THIS METHOD MAY NOT WORK AS EXPECTED!!!
        /// Converts the given decimal value (provided as a string) to the nearest floating point approximation
        /// the returned fp assumes the given significand and exponent size
        /// </summary>
        /// <param name="value"></param>
        /// <param name="significandSize"></param>
        /// <param name="exponentSize"></param>
        /// <returns></returns>
        public static BigFloat Round(String value, int exponentSize, int significandSize)
        {
            int i = value.IndexOf('.');

            if (i == -1)
            {
                return(Round(BIM.Parse(value), BIM.Zero, exponentSize, significandSize));
            }
            return(Round(i == 0 ? BIM.Zero : BIM.Parse(value.Substring(0, i)), BIM.Parse(value.Substring(i + 1, value.Length - i - 1)), exponentSize, significandSize));
        }
Exemple #15
0
 public BigFloat(String value, int significandSize, int exponentSize)
 {
     this.exponentSize    = exponentSize;
     this.significandSize = significandSize;
     this.exponent        = BIM.Zero;
     this.significand     = BIM.Zero;
     this.value           = value;
     if (value.Equals("nan"))
     {
         this.value = "NaN";
     }
     this.isNeg = value[0] == '-';
 }
Exemple #16
0
        public static BigFloat FromString(String s)
        {
            /*
             * String must be either of the format *e*f*e*
             * or of the special value formats: 0NaN*e* 0nan*e* 0+oo*e* 0-oo*e*
             * Where * indicates an integer value (digit)
             */
            BIM  sig, exp;
            int  sigSize, expSize;
            bool isNeg;

            if (s.IndexOf('f') == -1)
            {
                String val = s;
                sigSize = int.Parse(s.Substring(4, s.IndexOf('e') - 4));
                expSize = int.Parse(s.Substring(s.IndexOf('e') + 1));
                if (sigSize <= 0 || expSize <= 0)
                {
                    throw new FormatException("Significand and Exponent sizes must be greater than 0");
                }
                return(new BigFloat(val, sigSize, expSize));
            }

            sig     = BIM.Parse(s.Substring(0, s.IndexOf('e')));
            exp     = BIM.Parse(s.Substring(s.IndexOf('e') + 1, s.IndexOf('f') - s.IndexOf('e') - 1));
            sigSize = int.Parse(s.Substring(s.IndexOf('f') + 1, s.IndexOf('e', s.IndexOf('e') + 1) - s.IndexOf('f') - 1));
            expSize = int.Parse(s.Substring(s.IndexOf('e', s.IndexOf('e') + 1) + 1));
            isNeg   = s[0] == '-'; //We do this so that -0 is parsed correctly

            if (sigSize <= 0 || expSize <= 0)
            {
                throw new FormatException("Significand and Exponent sizes must be greater than 0");
            }

            sigSize = sigSize - 1;  //Get rid of sign bit
            sig     = BIM.Abs(sig); //sig must be positive
            //Uncomment if you want to shift the exponent for the user (i.e. 0e-1f24e8 --> 0e126f24e8)
            //exp = exp + BIM.Pow(new BIM(2), expSize-1) - BIM.One;

            if (exp < 0 || exp >= BIM.Pow(new BIM(2), expSize))
            {
                throw new FormatException("The given exponent " + exp + " cannot fit in the bit size " + expSize);
            }

            if (sig >= BIM.Pow(new BIM(2), sigSize))
            {
                throw new FormatException("The given significand " + sig + " cannot fit in the bit size " + (sigSize + 1));
            }

            return(new BigFloat(isNeg, sig, exp, sigSize, expSize));
        }
Exemple #17
0
 internal BigDec(BIM mantissa, int exponent)
 {
     if (mantissa.IsZero)
     {
         this.mantissa = mantissa;
         this.exponent = 0;
     }
     else
     {
         while (mantissa % ten == BIM.Zero)
         {
             mantissa = mantissa / ten;
             exponent = exponent + 1;
         }
         this.mantissa = mantissa;
         this.exponent = exponent;
     }
 }
Exemple #18
0
        public String ToDecimalString(int maxDigits)
        {
            string s      = this.mantissa.ToString();
            int    digits = (this.mantissa >= 0) ? s.Length : s.Length - 1;
            BIM    max    = BIM.Pow(10, maxDigits);
            BIM    min    = -max;

            if (this.exponent >= 0)
            {
                if (maxDigits < digits || maxDigits - digits < this.exponent)
                {
                    return(String.Format("{0}.0", (this.mantissa >= 0) ? max.ToString() : min.ToString()));
                }
                else
                {
                    return(String.Format("{0}{1}.0", s, new string('0', this.exponent)));
                }
            }
            else
            {
                int exp = -this.exponent;

                if (exp < digits)
                {
                    int intDigits = digits - exp;
                    if (maxDigits < intDigits)
                    {
                        return(String.Format("{0}.0", (this.mantissa >= 0) ? max.ToString() : min.ToString()));
                    }
                    else
                    {
                        int fracDigits = Math.Min(maxDigits, digits - intDigits);
                        return(String.Format("{0}.{1}", s.Substring(0, intDigits), s.Substring(intDigits, fracDigits)));
                    }
                }
                else
                {
                    int fracDigits = Math.Min(maxDigits, digits);
                    return(String.Format("0.{0}{1}", new string('0', exp - fracDigits), s.Substring(0, fracDigits)));
                }
            }
        }
Exemple #19
0
        ////////////////////////////////////////////////////////////////////////////
        // Conversion operations

        // ``floor`` rounds towards negative infinity (like SMT-LIBv2's to_int).
        /// <summary>
        /// Computes the floor and ceiling of this BigDec. Note the choice of rounding towards negative
        /// infinity rather than zero for floor is because SMT-LIBv2's to_int function floors this way.
        /// </summary>
        /// <param name="floor">The Floor (rounded towards negative infinity)</param>
        /// <param name="ceiling">Ceiling (rounded towards positive infinity)</param>
        public void FloorCeiling(out BIM floor, out BIM ceiling)
        {
            BIM n = this.mantissa;
            int e = this.exponent;

            if (n.IsZero)
            {
                floor = ceiling = n;
            }
            else if (0 <= e)
            {
                // it's an integer
                for (; 0 < e; e--)
                {
                    n = n * ten;
                }

                floor = ceiling = n;
            }
            else
            {
                // it's a non-zero integer, so the ceiling is one more than the floor
                for (; e < 0 && !n.IsZero; e++)
                {
                    n = n / ten; // Division rounds towards negative infinity
                }

                if (this.mantissa >= 0)
                {
                    floor   = n;
                    ceiling = n + 1;
                }
                else
                {
                    ceiling = n;
                    floor   = n - 1;
                }
            }

            Debug.Assert(floor <= ceiling, "Invariant was not maintained");
        }
Exemple #20
0
        public static BigDec operator +(BigDec x, BigDec y)
        {
            BIM m1 = x.mantissa;
            int e1 = x.exponent;
            BIM m2 = y.mantissa;
            int e2 = y.exponent;

            if (e2 < e1)
            {
                m1 = y.mantissa;
                e1 = y.exponent;
                m2 = x.mantissa;
                e2 = x.exponent;
            }

            while (e2 > e1)
            {
                m2 = m2 * ten;
                e2 = e2 - 1;
            }

            return(new BigDec(m1 + m2, e1));
        }
Exemple #21
0
        public BIM Floor(BIM?minimum, BIM?maximum)
        {
            BIM n = this.mantissa;

            if (this.exponent >= 0)
            {
                int e = this.exponent;
                while (e > 0 && (minimum == null || minimum <= n) && (maximum == null || n <= maximum))
                {
                    n = n * ten;
                    e = e - 1;
                }
            }
            else
            {
                int e = -this.exponent;
                while (e > 0 && !n.IsZero)
                {
                    n = n / ten;
                    e = e - 1;
                }
            }

            if (minimum != null && n < minimum)
            {
                return((BIM)minimum);
            }
            else if (maximum != null && maximum < n)
            {
                return((BIM)maximum);
            }
            else
            {
                return(n);
            }
        }
Exemple #22
0
        /// <summary>
        /// Momentum equations for the u wind component - algebraic mixing lenght model
        /// </summary>
        /// <param name="IS">ADI direction for x cells</param>
        /// <param name="JS">ADI direction for y cells</param>
        /// <param name="Cmueh">Constant</param>
        /// <param name="VISHMIN">Minimum horizontal turbulent exchange coefficients </param>
        /// <param name="AREAxy">Area of a flow field cell</param>
        /// <param name="UG">Geostrophic wind</param>
        /// <param name="building_Z0">Roughness of buildings</param>
        /// <param name="relax">Relaxation factor</param>
        public static void Calculate(int IS, int JS, float Cmueh, float VISHMIN, float AREAxy, Single UG, float relax)
        {
            float          DXK = Program.DXK; float DYK = Program.DYK;
            Vector <float> DXK_V     = new Vector <float>(DXK);
            Vector <float> DYK_V     = new Vector <float>(DYK);
            Vector <float> VISHMIN_V = new Vector <float>(VISHMIN);
            Vector <float> AREAxy_V  = new Vector <float>(AREAxy);
            Vector <float> UG_V      = new Vector <float>(UG);

            Parallel.For(3, Program.NII, Program.pOptions, i1 =>
            {
                Span <float> PIMU   = stackalloc float[Program.KADVMAX + 1];
                Span <float> QIMU   = stackalloc float[Program.KADVMAX + 1];
                Span <float> Mask   = stackalloc float[SIMD];
                Span <float> Mask2  = stackalloc float[SIMD];
                Span <float> AIM_A  = stackalloc float[SIMD];
                Span <float> BIM_A  = stackalloc float[SIMD];
                Span <float> CIM_A  = stackalloc float[SIMD];
                Span <float> DIMU_A = stackalloc float[SIMD];

                Vector <float> DVDXN, DVDXS, DUDXE, DUDXW, DWDXT, DWDXB;
                Vector <float> DE, DW, DS, DN, DT, DB;
                Vector <float> FE, FW, FS, FN, FT, FB;
                Vector <float> PE, PW, PS, PN, PT, PB;
                Vector <float> BIM, CIM, AE1, AW1, AS1, AN1, AIM;
                Vector <float> DIM_U, windhilf;
                Vector <float> DDPX;

                Vector <float> UKSim_LV;
                Vector <float> VKSjm_LV, WKSim_LV, WKSimM_LV, WKSkM_LV;

                Vector <float> VKSimjm_LV, VKSimjp_LV, VKSjp_LV, UKip_LV, VKSim_LV;

                Vector <float> WKS_V, VKS_V, UKS_V, DZK_V;
                Vector <float> DXKDZK, DYKDZK, AP0_V, intern, intern2;

                Vector <float> VIS, zs, mixL;
                Vector <float> UK_LV, UKim_LV;
                Vector <float> HOKART_KKART_V;

                Vector <float> Mask_V = Vector <float> .Zero;
                Vector <float> DUDZ, DVDZ;

                int i = i1;
                if (IS == -1)
                {
                    i = Program.NII - i1 + 1;
                }

                for (int j1 = 2; j1 <= Program.NJJ - 1; ++j1)
                {
                    int j = j1;
                    if (JS == -1)
                    {
                        j = Program.NJJ - j1 + 1;
                    }

                    if (Program.ADVDOM[i][j] == 1)
                    {
                        //inside a prognostic sub d omain
                        int jM1 = j - 1;
                        int jP1 = j + 1;
                        int iM1 = i - 1;
                        int iP1 = i + 1;

                        Single[] UK_L       = Program.UK[i][j];
                        Single[] UKS_L      = Program.UKS[i][j];
                        Single[] VKS_L      = Program.VKS[i][j];
                        Single[] WKS_L      = Program.WKS[i][j];
                        Single[] DPMNEW_L   = Program.DPMNEW[i][j];
                        Single[] UKim_L     = Program.UK[iM1][j];
                        Single[] UKip_L     = Program.UK[iP1][j];
                        Single[] UKjm_L     = Program.UK[i][jM1];
                        Single[] UKjp_L     = Program.UK[i][jP1];
                        Single[] UKSim_L    = Program.UKS[iM1][j];
                        Single[] VKSim_L    = Program.VKS[iM1][j];
                        Single[] VKSimjm_L  = Program.VKS[iM1][jM1];
                        Single[] VKSjm_L    = Program.VKS[i][jM1];
                        Single[] VKSimjp_L  = Program.VKS[iM1][jP1];
                        Single[] VKSjp_L    = Program.VKS[i][jP1];
                        Single[] WKSim_L    = Program.WKS[iM1][j];
                        Single[] DPMNEWim_L = Program.DPMNEW[iM1][j];

                        Single[] VKSipjp_L = Program.VKS[iP1][jP1];
                        Single[] VKSipjm_L = Program.VKS[iP1][jM1];
                        Single[] WKSip_L   = Program.WK[iP1][j];
                        Single[] VK_L      = Program.VK[i][j];

                        int Vert_Index_LL               = Program.VerticalIndex[i][j];
                        float Ustern_terrain_helpterm   = Program.UsternTerrainHelpterm[i][j];
                        float Ustern_obstacles_helpterm = Program.UsternObstaclesHelpterm[i][j];
                        bool ADVDOM_JM = (Program.ADVDOM[i][jM1] < 1) || (j == 2);
                        bool ADVDOM_JP = (Program.ADVDOM[i][jP1] < 1) || (j == Program.NJJ - 1);
                        bool ADVDOM_IM = (Program.ADVDOM[iM1][j] < 1) || (i == 2);
                        bool ADVDOM_IP = (Program.ADVDOM[iP1][j] < 1) || (i == Program.NII - 1);

                        float CUTK_L   = Program.CUTK[i][j];
                        Single[] VEG_L = Program.VEG[i][j];
                        Single COV_L   = Program.COV[i][j];

                        float Z0 = Program.Z0;
                        if (Program.AdaptiveRoughnessMax < 0.01)
                        {
                            //Use GRAMM Roughness with terrain or Roughness from point 1,1 without terrain
                            int IUstern = 1;
                            int JUstern = 1;
                            if ((Program.Topo == 1) && (Program.LandUseAvailable == true))
                            {
                                double x    = i * Program.DXK + Program.GralWest;
                                double y    = j * Program.DYK + Program.GralSouth;
                                double xsi1 = x - Program.GrammWest;
                                double eta1 = y - Program.GrammSouth;
                                IUstern     = Math.Clamp((int)(xsi1 / Program.DDX[1]) + 1, 1, Program.NX);
                                JUstern     = Math.Clamp((int)(eta1 / Program.DDY[1]) + 1, 1, Program.NY);
                            }
                            Z0 = Program.Z0Gramm[IUstern][JUstern];
                        }
                        else
                        {
                            Z0 = Program.Z0Gral[i][j];
                        }

                        int KKART_LL   = Program.KKART[i][j];
                        HOKART_KKART_V = new Vector <float>(Program.HOKART[KKART_LL]);

                        int KSTART = 1;
                        if (CUTK_L == 0) // building heigth == 0 m
                        {
                            KSTART = KKART_LL + 1;
                        }

                        int KKART_LL_P1 = KKART_LL + 1;

                        int KEND   = Vert_Index_LL + 1; // simpify for-loop
                        bool end   = false;             // flag for the last loop
                        int k_real = 0;

                        for (int k = KSTART; k < KEND; k += SIMD)
                        {
                            // to compute the top levels - reduce k, remember k_real and set end to true
                            k_real = k;
                            if (k > KEND - SIMD)
                            {
                                k   = KEND - SIMD;
                                end = true;
                            }
                            int kM1 = k - 1;
                            int kP1 = k + 1;

                            WKS_V = new Vector <float>(WKS_L, k);
                            VKS_V = new Vector <float>(VKS_L, k);
                            UKS_V = new Vector <float>(UKS_L, k);

                            DZK_V = new Vector <float>(Program.DZK, k);

                            DXKDZK = DXK_V * DZK_V;
                            DYKDZK = DYK_V * DZK_V;

                            //turbulence modelling
                            zs = new Vector <float>(Program.HOKART, k) - HOKART_KKART_V - DZK_V * Vect_05;

                            VIS = Vector <float> .Zero;

                            // Create Mask if KKART_LL is between k and k + SIMD
                            bool mask_v_enable = false;
                            if (KKART_LL_P1 >= k && KKART_LL_P1 < (k + SIMD))
                            {
                                for (int ii = 0; ii < Mask.Length; ++ii)
                                {
                                    if ((k + ii) > KKART_LL_P1)
                                    {
                                        Mask[ii] = 1;
                                    }
                                    else
                                    {
                                        Mask[ii] = 0;
                                    }
                                }
                                Mask_V        = new Vector <float>(Mask);
                                mask_v_enable = true;
                            }

                            if ((k + SIMD) > KKART_LL_P1)
                            {
                                //k-eps model
                                //float VIS = (float)Math.Sqrt(TURB_L[k]) * zs * Cmueh;

                                //mixing-length model
                                mixL = Vector.Min(Vect_0071 * zs, Vect_100);

                                //adapted mixing-leng th within vegetation layers
                                if (COV_L > 0)
                                {
                                    mixL *= (Vect_1 - Vect_099 * new Vector <float>(COV_L));
                                    //mixL = (float)Math.Min(0.071 * Math.Max(1 - Math.Min(2 * VEG_L[kM1], 1), 0.05) * zs, 100);
                                }

                                intern = (new Vector <float>(UK_L, kP1) - new Vector <float>(UK_L, kM1)) / (Vect_2 * DZK_V);
                                DUDZ   = intern * intern;

                                intern = (new Vector <float>(VK_L, kP1) - new Vector <float>(VK_L, kM1)) / (Vect_2 * DZK_V);
                                DVDZ   = intern * intern;

                                if (mask_v_enable)
                                {
                                    VIS = Vector.Min(mixL * mixL * Vector.SquareRoot(Vect_05 * (DUDZ + DVDZ)), Vect_15) * Mask_V;
                                }
                                else
                                {
                                    VIS = Vector.Min(mixL * mixL * Vector.SquareRoot(Vect_05 * (DUDZ + DVDZ)), Vect_15);
                                }
                            }

                            DE = Vector.Max(VIS, VISHMIN_V) * DYKDZK / DXK_V;
                            DW = DE;
                            DS = Vector.Max(VIS, VISHMIN_V) * DXKDZK / DYK_V;
                            DN = DS;
                            DT = Vector.Max(VIS, VISHMIN_V) * AREAxy_V / DZK_V;
                            DB = Vector <float> .Zero;
                            if (mask_v_enable)
                            {
                                DB = DT * Mask_V;
                            }
                            else if (k > KKART_LL_P1) // otherwise k < KKART_LL_P1 and DB=0!
                            {
                                DB = DT;
                            }

                            //BOUNDARY CONDITIONS FOR DIFFUSION TERMS
                            if (ADVDOM_JM)
                            {
                                DS = Vector <float> .Zero;
                            }

                            if (ADVDOM_JP)
                            {
                                DN = Vector <float> .Zero;
                            }

                            if (ADVDOM_IM)
                            {
                                DW = Vector <float> .Zero;
                            }

                            if (ADVDOM_IP)
                            {
                                DE = Vector <float> .Zero;
                            }

                            //ADVECTION TERMS
                            VKSimjp_LV = new Vector <float>(VKSimjp_L, k);
                            VKSjp_LV   = new Vector <float>(VKSjp_L, k);
                            VKSim_LV   = new Vector <float>(VKSim_L, k);
                            VKSimjm_LV = new Vector <float>(VKSimjm_L, k);
                            VKSjm_LV   = new Vector <float>(VKSjm_L, k);
                            WKSim_LV   = new Vector <float>(WKSim_L, k);
                            UKSim_LV   = new Vector <float>(UKSim_L, k);

                            FE = UKS_V * DYKDZK;
                            FW = UKSim_LV * DYKDZK;
                            FS = Vect_025 * (VKSim_LV + VKS_V + VKSimjm_LV + VKSjm_LV) * DXKDZK;
                            FN = Vect_025 * (VKSim_LV + VKS_V + VKSimjp_LV + VKSjp_LV) * DXKDZK;
                            FT = Vect_025 * (WKSim_LV + WKS_V + new Vector <float>(WKSim_L, kP1) + new Vector <float>(WKS_L, kP1)) * AREAxy_V;
                            FB = Vector <float> .Zero;

                            WKSkM_LV  = new Vector <float>(WKS_L, kM1);
                            WKSimM_LV = new Vector <float>(WKSim_L, kM1);

                            if (mask_v_enable)
                            {
                                FB = Vect_025 * (WKSim_LV + WKS_V + WKSimM_LV + WKSkM_LV) * AREAxy_V * Mask_V;
                            }
                            else if (k > KKART_LL_P1) // otherwise k < KKART_LL_P1 and FB=0!
                            {
                                FB = Vect_025 * (WKSim_LV + WKS_V + WKSimM_LV + WKSkM_LV) * AREAxy_V;
                            }

                            //PECLET NUMBERS
                            DE = Vector.Max(DE, Vect_0001);
                            DB = Vector.Max(DB, Vect_0001);
                            DW = Vector.Max(DW, Vect_0001);
                            DS = Vector.Max(DS, Vect_0001);
                            DN = Vector.Max(DN, Vect_0001);
                            DT = Vector.Max(DT, Vect_0001);

                            PE = Vector.Abs(FE / DE);
                            PB = Vector.Abs(FB / DB);
                            PW = Vector.Abs(FW / DW);
                            PS = Vector.Abs(FS / DS);
                            PN = Vector.Abs(FN / DN);
                            PT = Vector.Abs(FT / DT);

                            //POWER LAW ADVECTION SCHEME
                            intern = Vect_1 - Vect_01 * PT;
                            BIM    = DT * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(-FT, Vect_0);

                            intern = Vect_1 - Vect_01 * PB;
                            CIM    = DB * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(FB, Vect_0);

                            intern = Vect_1 - Vect_01 * PE;
                            AE1    = DE * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(-FE, Vect_0);

                            intern = Vect_1 - Vect_01 * PW;
                            AW1    = DW * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(FW, Vect_0);

                            intern = Vect_1 - Vect_01 * PS;
                            AS1    = DS * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(FS, Vect_0);

                            intern = Vect_1 - Vect_01 * PN;
                            AN1    = DN * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(-FN, Vect_0);

                            //UPWIND SCHEME

                            /*
                             *  double BIM = DT + Program.fl_max(-FT, 0);
                             *  double CIM = DB + Program.fl_max(FB, 0);
                             *  double AE1 = DE + Program.fl_max(-FE, 0);
                             *  double AW1 = DW + Program.fl_max(FW, 0);
                             *  double AS1 = DS + Program.fl_max(FS, 0);
                             *  double AN1 = DN + Program.fl_max(-FN, 0);
                             */

                            AP0_V = new Vector <float>(PrognosticFlowfield.AP0, k);

                            AIM = BIM + CIM + AW1 + AS1 + AE1 + AN1 + AP0_V;

                            //SOURCE TERMS
                            intern2 = Vect_05 * (VKSim_LV + VKS_V);
                            intern  = Vect_05 * (UKSim_LV + UKS_V);

                            windhilf = Vector.Max(Vector.SquareRoot(intern * intern + intern2 * intern2), Vect_001);

                            DDPX = new Vector <float>(DPMNEWim_L, k) - new Vector <float>(DPMNEW_L, k);

                            UKim_LV  = new Vector <float>(UKim_L, k);
                            UKip_LV  = new Vector <float>(UKip_L, k);
                            UKSim_LV = new Vector <float>(UKSim_L, k);
                            UK_LV    = new Vector <float>(UK_L, k);

                            DIM_U = AW1 * UKim_LV +
                                    AS1 * new Vector <float>(UKjm_L, k) +
                                    AE1 * UKip_LV +
                                    AN1 * new Vector <float>(UKjp_L, k) +
                                    AP0_V * Vect_05 * (UKSim_LV + UKS_V) +
                                    DDPX * DYKDZK +
                                    (Program.CorolisParam * (UG_V - UK_LV) -
                                     new Vector <float>(VEG_L, kM1) * UK_LV * windhilf) * AREAxy_V * DZK_V;


                            //BOUNDARY CONDITION AT SURFACES (OBSTACLES AND TERRAIN)
                            if ((k <= KKART_LL_P1) && ((k + SIMD) > KKART_LL_P1))
                            {
                                Mask2.Clear();
                                //Array.Clear(Mask2, 0, Mask2.Length);
                                int ii    = KKART_LL_P1 - k; // 0 to SIMD - 1
                                Mask2[ii] = 1;               // Set the mask2 at SIMD position k == KKART_LL_P1 to 1

                                intern   = Vect_05 * (UKSim_LV + UKS_V);
                                intern2  = Vect_05 * (VKSim_LV + VKS_V);
                                windhilf = Vector.Max(Vector.SquareRoot(intern * intern + intern2 * intern2), Vect_01);

                                if (CUTK_L < 1) // building heigth < 1 m
                                {
                                    // above terrain
                                    float Ustern_Buildings = Ustern_terrain_helpterm * windhilf[ii];
                                    if (Z0 >= DZK_V[ii] * 0.1F)
                                    {
                                        int ind          = k + ii + 1;
                                        Ustern_Buildings = Ustern_terrain_helpterm * MathF.Sqrt(Program.Pow2(0.5F * ((UKSim_L[ind]) + UKS_L[ind])) + Program.Pow2(0.5F * ((VKSim_L[ind]) + VKS_L[ind])));
                                    }

                                    Vector <float> Ustern_Buildings_V = new Vector <float>(Ustern_Buildings * Ustern_Buildings);
                                    DIM_U -= UK_LV / windhilf * Ustern_Buildings_V * AREAxy_V * new Vector <float>(Mask2);
                                }
                                else
                                {
                                    //above a building
                                    float Ustern_Buildings            = Ustern_obstacles_helpterm * windhilf[ii];
                                    Vector <float> Ustern_Buildings_V = new Vector <float>(Ustern_Buildings * Ustern_Buildings);
                                    DIM_U -= UK_LV / windhilf * Ustern_Buildings_V * AREAxy_V * new Vector <float>(Mask2);
                                }
                            }

                            //additional terms of the eddy-viscosity model
                            if (k > KKART_LL_P1)
                            {
                                DUDXE = (UKip_LV - UK_LV) * DYKDZK;
                                DUDXW = (UK_LV - UKim_LV) * DYKDZK;
                                DVDXN = (Vect_05 * (new Vector <float>(VKSipjp_L, k) + VKSjp_LV) - Vect_05 * (VKSjp_LV + VKSimjp_LV)) * DXKDZK;
                                DVDXS = (Vect_05 * (new Vector <float>(VKSipjm_L, k) + VKSjm_LV) - Vect_05 * (VKSjm_LV + VKSimjm_LV)) * DXKDZK;
                                DWDXT = (Vect_05 * (new Vector <float>(WKSip_L, k) + WKS_V) - Vect_05 * (WKS_V + WKSim_LV)) * AREAxy_V;
                                DWDXB = (Vect_05 * (new Vector <float>(WKSip_L, kM1) + WKSkM_LV) - Vect_05 * (WKSkM_LV + WKSimM_LV)) * AREAxy_V;

                                if (mask_v_enable) // compute ADD_DIFF and add to DIMW
                                {
                                    DIM_U += VIS / DXK_V * (DUDXE - DUDXW + DVDXN - DVDXS + DWDXT - DWDXB) * Mask_V;
                                }
                                else
                                {
                                    DIM_U += VIS / DXK_V * (DUDXE - DUDXW + DVDXN - DVDXS + DWDXT - DWDXB);
                                }
                            }

                            //RECURRENCE FORMULA
                            int kint_s = 0;
                            if (end) // restore original indices
                            {
                                kint_s  = k_real - k;
                                k_real -= kint_s;
                            }

                            AIM.CopyTo(AIM_A);
                            BIM.CopyTo(BIM_A);
                            CIM.CopyTo(CIM_A);
                            DIM_U.CopyTo(DIMU_A);

                            for (int kint = kint_s; kint < AIM_A.Length; ++kint) // loop over all SIMD values
                            {
                                int index_i = k_real + kint;

                                if (index_i < KEND)
                                {
                                    if (index_i > KKART_LL_P1)
                                    {
                                        float TERMP   = 1 / (AIM_A[kint] - CIM_A[kint] * PIMU[index_i - 1]);
                                        PIMU[index_i] = BIM_A[kint] * TERMP;
                                        QIMU[index_i] = (DIMU_A[kint] + CIM_A[kint] * QIMU[index_i - 1]) * TERMP;
                                    }
                                    else
                                    {
                                        float TERMP   = 1 / AIM_A[kint];
                                        PIMU[index_i] = BIM_A[kint] * TERMP;
                                        QIMU[index_i] = DIMU_A[kint] * TERMP;
                                    }
                                }
                            }

                            if (end)
                            {
                                k = KEND + 1;
                            }
                        } // loop over all k

                        //OBTAIN NEW U-COMPONENTS
                        int KKARTm_LL = Math.Max(KKART_LL, Program.KKART[i - 1][j]);
                        lock (UK_L)
                        {
                            for (int k = Vert_Index_LL; k >= KSTART; --k)
                            {
                                if (KKARTm_LL < k)
                                {
                                    UK_L[k] += relax * (PIMU[k] * UK_L[k + 1] + QIMU[k] - UK_L[k]);
                                }
                            }
                        }
                    }
                }
            });
        }
Exemple #23
0
 public static BigDec FromBigInt(BIM v)
 {
     return(new BigDec(v, 0));
 }
Exemple #24
0
 public BigFloat(String value, int significandSize, int exponentSize) {
   this.exponentSize = exponentSize;
   this.significandSize = significandSize;
   this.exponent = BIM.Zero;
   this.significand = BIM.Zero;
   this.value = value;
   if (value.Equals("nan"))
     this.value = "NaN";
   this.isNeg = value[0] == '-';
 }
Exemple #25
0
 public static BigFloat FromBigInt(BIM v, int significandSize, int exponentSize)
 {
     return(new BigFloat(v.ToString(), significandSize, exponentSize));
 }
Exemple #26
0
        /// <summary>
        /// Momentum equations for the w wind component - algebraic mixing lenght model
        /// </summary>
        public static void Calculate(int IS, int JS, float Cmueh, float VISHMIN, float AREAxy, float building_Z0, float relax)
        {
            Vector <float> DXK_V     = new Vector <float>(Program.DXK);
            Vector <float> DYK_V     = new Vector <float>(Program.DYK);
            Vector <float> VISHMIN_V = new Vector <float>(VISHMIN);
            Vector <float> AREAxy_V  = new Vector <float>(AREAxy);

            Parallel.For(2, Program.NII, Program.pOptions, i1 =>
            {
                int KKART_LL, Vert_Index_LL;
                Span <float> PIMW   = stackalloc float [Program.KADVMAX + 1];
                Span <float> QIMW   = stackalloc float [Program.KADVMAX + 1];
                Span <float> Mask   = stackalloc float [SIMD];
                Span <float> AIM_A  = stackalloc float [SIMD];
                Span <float> BIM_A  = stackalloc float [SIMD];
                Span <float> CIM_A  = stackalloc float [SIMD];
                Span <float> DIMW_A = stackalloc float [SIMD];

                Vector <float> DUDZE, DUDZW, DVDZN, DVDZS, DWDZT, DWDZB;
                Vector <float> DE, DW, DS, DN, DT, DB;
                Vector <float> FE, FW, FS, FN, FT, FB;
                Vector <float> PE, PW, PS, PN, PT, PB;
                Vector <float> BIM, CIM, AE1, AW1, AS1, AN1, AIM;
                Vector <float> DIMW, windhilf;
                Vector <float> DDPZ;

                Vector <float> UKSim_LV, UKSimM_LV;
                Vector <float> VKSjm_LV, VKSjmM_LV;

                Vector <float> WKS_V, VKS_V, UKS_V, DZK_V;
                Vector <float> DXKDZK, DYKDZK, AP0_V, intern, intern2;

                Vector <float> VIS, zs, mixL;
                Vector <float> HOKART_KKART;

                Vector <float> Mask_V = Vector <float> .Zero;
                Vector <float> DUDZ, DVDZ;

                Vector <float> UKS_M_V, VKS_M_V, WK_LV;

                float Ustern_terrain_helpterm, Ustern_obstacles_helpterm;

                int i = i1;
                if (IS == -1)
                {
                    i = Program.NII - i1 + 1;
                }

                for (int j1 = 2; j1 < Program.NJJ; ++j1)
                {
                    int j = j1;
                    if (JS == -1)
                    {
                        j = Program.NJJ - j1 + 1;
                    }

                    if (Program.ADVDOM[i][j] == 1)
                    {
                        int jM1 = j - 1;
                        int jP1 = j + 1;
                        int iM1 = i - 1;
                        int iP1 = i + 1;

                        Single[] WK_L     = Program.WK[i][j];
                        Single[] WKim_L   = Program.WK[iM1][j];
                        Single[] WKip_L   = Program.WK[iP1][j];
                        Single[] WKjm_L   = Program.WK[i][jM1];
                        Single[] WKjp_L   = Program.WK[i][jP1];
                        Single[] UKS_L    = Program.UKS[i][j];
                        Single[] VKS_L    = Program.VKS[i][j];
                        Single[] WKS_L    = Program.WKS[i][j];
                        Single[] DPMNEW_L = Program.DPMNEW[i][j];
                        Single[] VKim_L   = Program.VK[iM1][j];
                        Single[] VKip_L   = Program.VK[iP1][j];
                        Single[] VKjm_L   = Program.VK[i][jM1];
                        Single[] VKjp_L   = Program.VK[i][jP1];
                        Single[] UKSim_L  = Program.UKS[iM1][j];
                        Single[] UKSip_L  = Program.UKS[iP1][j];
                        Single[] VKSjm_L  = Program.VKS[i][jM1];
                        Single[] VKSjp_L  = Program.VKS[i][jP1];

                        Single[] UK_L = Program.UK[i][j];
                        Single[] VK_L = Program.VK[i][j];

                        KKART_LL                  = Program.KKART[i][j];
                        Vert_Index_LL             = Program.VerticalIndex[i][j];
                        Ustern_terrain_helpterm   = Program.UsternTerrainHelpterm[i][j];
                        Ustern_obstacles_helpterm = Program.UsternObstaclesHelpterm[i][j];
                        bool ADVDOM_JM            = (Program.ADVDOM[i][jM1] < 1) || (j == 2);
                        bool ADVDOM_JP            = (Program.ADVDOM[i][jP1] < 1) || (j == Program.NJJ - 1);
                        bool ADVDOM_IM            = (Program.ADVDOM[iM1][j] < 1) || (i == 2);
                        bool ADVDOM_IP            = (Program.ADVDOM[iP1][j] < 1) || (i == Program.NII - 1);

                        Single[] VEG_L = Program.VEG[i][j];
                        Single COV_L   = Program.COV[i][j];

                        HOKART_KKART = new Vector <float>(Program.HOKART[KKART_LL]);

                        int KSTART = 1;
                        if (Program.CUTK[i][j] == 0)
                        {
                            KSTART = KKART_LL + 1;
                        }

                        int KKART_LL_P1 = KKART_LL + 1;

                        int KEND   = Vert_Index_LL + 1;
                        bool end   = false; // flag for the last loop
                        int k_real = 0;

                        for (int k = KSTART; k < KEND; k += SIMD)
                        {
                            // to compute the top levels - reduce k, remember k_real and set end to true
                            k_real = k;
                            if (k > KEND - SIMD)
                            {
                                k   = KEND - SIMD;
                                end = true;
                            }
                            int kM1 = k - 1;
                            int kP1 = k + 1;

                            WKS_V = new Vector <float>(WKS_L, k);
                            VKS_V = new Vector <float>(VKS_L, k);
                            UKS_V = new Vector <float>(UKS_L, k);

                            DZK_V = new Vector <float>(Program.DZK, k);

                            DXKDZK = DXK_V * DZK_V;
                            DYKDZK = DYK_V * DZK_V;

                            UKS_M_V = new Vector <float>(UKS_L, kM1);
                            VKS_M_V = new Vector <float>(VKS_L, kM1);

                            //turbulence modelling
                            zs = new Vector <float>(Program.HOKART, k) - HOKART_KKART - DZK_V * Vect_05;

                            VIS = Vector <float> .Zero;

                            // Create Mask if KKART_LL is between k and k + SIMD
                            bool mask_v_enable = false;
                            if (KKART_LL_P1 >= k && KKART_LL_P1 < (k + SIMD))
                            {
                                for (int ii = 0; ii < Mask.Length; ++ii)
                                {
                                    if ((k + ii) > KKART_LL_P1)
                                    {
                                        Mask[ii] = 1;
                                    }
                                    else
                                    {
                                        Mask[ii] = 0;
                                    }
                                }
                                Mask_V        = new Vector <float>(Mask);
                                mask_v_enable = true;
                            }

                            if ((k + SIMD) > KKART_LL_P1)
                            {
                                //k-eps model
                                //float VIS = (float)Math.Sqrt(TURB_L[k]) * zs * Cmueh;

                                //mixing-length model
                                mixL = Vector.Min(Vect_0071 * zs, Vect_100);

                                //adapted mixing-leng th within vegetation layers
                                if (COV_L > 0)
                                {
                                    mixL *= (Vect_1 - Vect_099 * new Vector <float>(COV_L));
                                    //mixL = (float)Math.Min(0.071 * Math.Max(1 - Math.Min(2 * VEG_L[kM1], 1), 0.05) * zs, 100);
                                }

                                intern = (new Vector <float>(UK_L, kP1) - new Vector <float>(UK_L, kM1)) / (Vect_2 * DZK_V);
                                DUDZ   = intern * intern;

                                intern = (new Vector <float>(VK_L, kP1) - new Vector <float>(VK_L, kM1)) / (Vect_2 * DZK_V);
                                DVDZ   = intern * intern;

                                if (mask_v_enable)
                                {
                                    VIS = Vector.Min(mixL * mixL * Vector.SquareRoot(Vect_05 * (DUDZ + DVDZ)), Vect_15) * Mask_V;
                                }
                                else
                                {
                                    VIS = Vector.Min(mixL * mixL * Vector.SquareRoot(Vect_05 * (DUDZ + DVDZ)), Vect_15);
                                }
                            }

                            DE = Vector.Max(VIS, VISHMIN_V) * DYKDZK / DXK_V;
                            DW = DE;
                            DS = Vector.Max(VIS, VISHMIN_V) * DXKDZK / DYK_V;
                            DN = DS;
                            DT = Vector.Max(VIS, VISHMIN_V) * AREAxy_V / DZK_V;
                            DB = Vector <float> .Zero;

                            if (mask_v_enable)
                            {
                                DB = DT * Mask_V;
                            }
                            else if (k > KKART_LL_P1) // otherwise k < KKART_LL_P1 and DB=0!
                            {
                                DB = DT;
                            }

                            //BOUNDARY CONDITIONS FOR DIFFUSION TERMS
                            if (ADVDOM_JM)
                            {
                                DS = Vector <float> .Zero;
                            }
                            if (ADVDOM_JP)
                            {
                                DN = Vector <float> .Zero;
                            }
                            if (ADVDOM_IM)
                            {
                                DW = Vector <float> .Zero;
                            }
                            if (ADVDOM_IP)
                            {
                                DE = Vector <float> .Zero;
                            }

                            //ADVECTION TERMS
                            UKSim_LV  = new Vector <float>(UKSim_L, k);
                            UKSimM_LV = new Vector <float>(UKSim_L, kM1);
                            VKSjm_LV  = new Vector <float>(VKSjm_L, k);
                            VKSjmM_LV = new Vector <float>(VKSjm_L, kM1);

                            FE = Vect_025 * (UKS_V + new Vector <float>(UKSip_L, k) + UKS_M_V + new Vector <float>(UKSip_L, kM1)) * DYKDZK;
                            FW = Vect_025 * (UKS_V + UKSim_LV + UKS_M_V + UKSimM_LV) * DYKDZK;
                            FS = Vect_025 * (VKS_V + VKSjm_LV + VKS_M_V + VKSjmM_LV) * DXKDZK;
                            FN = Vect_025 * (VKS_V + new Vector <float>(VKSjp_L, k) + VKS_M_V + new Vector <float>(VKSjp_L, kM1)) * DXKDZK;
                            FT = WKS_V * AREAxy_V;
                            FB = Vector <float> .Zero;

                            if (mask_v_enable)
                            {
                                FB = new Vector <float>(WKS_L, kM1) * AREAxy_V * Mask_V;
                            }
                            else if (k > KKART_LL_P1) // otherwise k < KKART_LL_P1 and FB=0!
                            {
                                FB = new Vector <float>(WKS_L, kM1) * AREAxy_V;
                            }

                            //PECLET NUMBERS
                            DE = Vector.Max(DE, Vect_0001);
                            DB = Vector.Max(DB, Vect_0001);
                            DW = Vector.Max(DW, Vect_0001);
                            DS = Vector.Max(DS, Vect_0001);
                            DN = Vector.Max(DN, Vect_0001);
                            DT = Vector.Max(DT, Vect_0001);

                            PE = Vector.Abs(FE / DE);
                            PB = Vector.Abs(FB / DB);
                            PW = Vector.Abs(FW / DW);
                            PS = Vector.Abs(FS / DS);
                            PN = Vector.Abs(FN / DN);
                            PT = Vector.Abs(FT / DT);

                            //POWER LAW ADVECTION SCHEME
                            intern = Vect_1 - Vect_01 * PT;
                            BIM    = DT * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(-FT, Vect_0);

                            intern = Vect_1 - Vect_01 * PB;
                            CIM    = DB * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(FB, Vect_0);

                            intern = Vect_1 - Vect_01 * PE;
                            AE1    = DE * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(-FE, Vect_0);

                            intern = Vect_1 - Vect_01 * PW;
                            AW1    = DW * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(FW, Vect_0);

                            intern = Vect_1 - Vect_01 * PS;
                            AS1    = DS * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(FS, Vect_0);

                            intern = Vect_1 - Vect_01 * PN;
                            AN1    = DN * Vector.Max(Vect_0, intern * intern * intern * intern * intern)
                                     + Vector.Max(-FN, Vect_0);

                            //UPWIND SCHEME

                            /*
                             *  double BIM = DT + Program.fl_max(-FT, 0);
                             *  double CIM = DB + Program.fl_max(FB, 0);
                             *  double AE1 = DE + Program.fl_max(-FE, 0);
                             *  double AW1 = DW + Program.fl_max(FW, 0);
                             *  double AS1 = DS + Program.fl_max(FS, 0);
                             *  double AN1 = DN + Program.fl_max(-FN, 0);
                             */

                            AP0_V = new Vector <float>(PrognosticFlowfield.AP0, k);

                            AIM = BIM + CIM + AW1 + AS1 + AE1 + AN1 + AP0_V;

                            //SOURCE TERMS
                            intern2 = Vect_05 * (VKSjm_LV + VKS_V);
                            intern  = Vect_05 * (UKSim_LV + UKS_V);

                            windhilf = Vector.Max(Vector.SquareRoot(intern * intern + intern2 * intern2), Vect_001);

                            DDPZ = new Vector <float>(DPMNEW_L, kM1) - new Vector <float>(DPMNEW_L, k);

                            WK_LV = new Vector <float>(WK_L, k);

                            DIMW = AW1 * new Vector <float>(WKim_L, k) +
                                   AS1 * new Vector <float>(WKjm_L, k) +
                                   AE1 * new Vector <float>(WKip_L, k) +
                                   AN1 * new Vector <float>(WKjp_L, k) +
                                   AP0_V * Vect_05 * (new Vector <float>(WKS_L, kM1) + WKS_V) +
                                   DDPZ * AREAxy_V -
                                   new Vector <float>(VEG_L, kM1) * WK_LV * windhilf * AREAxy_V * DZK_V;

                            //additional terms of the eddy-viscosity model
                            if (k > KKART_LL_P1)
                            {
                                DUDZE = (Vect_05 * (new Vector <float>(UKS_L, kP1) + UKS_V) - Vect_05 * (UKS_V + UKS_M_V)) * DYKDZK;
                                DUDZW = (Vect_05 * (new Vector <float>(UKSim_L, kP1) + UKSim_LV) - Vect_05 * (UKSim_LV + UKSimM_LV)) * DYKDZK;
                                DVDZN = (Vect_05 * (new Vector <float>(VKS_L, kP1) + VKS_V) - Vect_05 * (VKS_V + VKS_M_V)) * DXKDZK;
                                DVDZS = (Vect_05 * (new Vector <float>(VKSjm_L, kP1) + VKSjm_LV) - Vect_05 * (VKSjm_LV + VKSjmM_LV)) * DXKDZK;
                                DWDZT = (new Vector <float>(WK_L, kP1) - WK_LV) * AREAxy_V;
                                DWDZB = (WK_LV - new Vector <float>(WK_L, kM1)) * AREAxy_V;

                                if (mask_v_enable) // compute ADD_DIFF and add to DIMW
                                {
                                    DIMW += VIS / DZK_V * (DUDZE - DUDZW + DVDZN - DVDZS + DWDZT - DWDZB) * Mask_V;
                                }
                                else
                                {
                                    DIMW += VIS / DZK_V * (DUDZE - DUDZW + DVDZN - DVDZS + DWDZT - DWDZB);
                                }
                            }

                            //RECURRENCE FORMULA
                            int kint_s = 0;
                            if (end) // restore original indices
                            {
                                kint_s  = k_real - k;
                                k_real -= kint_s;
                            }

                            AIM.CopyTo(AIM_A);
                            BIM.CopyTo(BIM_A);
                            CIM.CopyTo(CIM_A);
                            DIMW.CopyTo(DIMW_A);

                            for (int kint = kint_s; kint < AIM_A.Length; ++kint) // loop over all SIMD values
                            {
                                int index_i = k_real + kint;

                                if (index_i < KEND)
                                {
                                    if (index_i > KKART_LL_P1)
                                    {
                                        float TERMP   = 1 / (AIM_A[kint] - CIM_A[kint] * PIMW[index_i - 1]);
                                        PIMW[index_i] = BIM_A[kint] * TERMP;
                                        QIMW[index_i] = (DIMW_A[kint] + CIM_A[kint] * QIMW[index_i - 1]) * TERMP;
                                    }
                                    else
                                    {
                                        float TERMP   = 1 / AIM_A[kint];
                                        PIMW[index_i] = BIM_A[kint] * TERMP;
                                        QIMW[index_i] = DIMW_A[kint] * TERMP;
                                    }
                                }
                            }

                            if (end)
                            {
                                k = KEND + 1;
                            }
                        } // loop over all k

                        //OBTAIN NEW W-COMPONENTS
                        for (int k = Vert_Index_LL; k >= KSTART; --k)
                        {
                            WK_L[k] += relax * (PIMW[k] * WK_L[k + 1] + QIMW[k] - WK_L[k]);
                        }
                    }
                }
            });
        }
        /// <summary>
        /// Exports spatial elements, including rooms, areas and spaces. 2nd level space boundaries.
        /// </summary>
        /// <param name="ifcExporter">
        /// The Exporter object.
        /// </param>
        /// <param name="exporterIFC">
        /// The ExporterIFC object.
        /// </param>
        /// <param name="document">
        /// The Revit document.
        /// </param>
        /// <param name="filterView">
        /// The view not exported.
        /// </param>
        /// <param name="spaceExported">
        /// The output boolean value indicates if exported successfully.
        /// </param>
        public static void ExportSpatialElement2ndLevel(BIM.IFC.Exporter.Exporter ifcExporter, ExporterIFC exporterIFC, Document document, View filterView, ref bool spaceExported)
        {
            using (SubTransaction st = new SubTransaction(document))
            {
                st.Start();

                EnergyAnalysisDetailModel model = null;
                try
                {
                    IFCFile file = exporterIFC.GetFile();
                    using (IFCTransaction transaction = new IFCTransaction(file))
                    {

                        EnergyAnalysisDetailModelOptions options = new EnergyAnalysisDetailModelOptions();
                        options.Tier = EnergyAnalysisDetailModelTier.SecondLevelBoundaries; //2nd level space boundaries
                        options.SimplifyCurtainSystems = true;
                        try
                        {
                            model = EnergyAnalysisDetailModel.Create(document, options);
                        }
                        catch (System.Exception)
                        {
                            spaceExported = false;
                            return;
                        }

                        IList<EnergyAnalysisSpace> spaces = model.GetAnalyticalSpaces();
                        spaceExported = true;

                        foreach (EnergyAnalysisSpace space in spaces)
                        {
                            SpatialElement spatialElement = document.GetElement(space.SpatialElementId) as SpatialElement;

                            if (spatialElement == null)
                                continue;

                            //quick reject
                            bool isArea = spatialElement is Area;
                            if (isArea)
                            {
                                if (!IsAreaGrossInterior(exporterIFC, spatialElement))
                                    continue;
                            }

                            //current view only
                            if (filterView != null && !ElementFilteringUtil.IsElementVisible(filterView, spatialElement))
                                continue;
                            //

                            if (!ElementFilteringUtil.ShouldCategoryBeExported(exporterIFC, spatialElement))
                                continue;

                            Options geomOptions = GeometryUtil.GetIFCExportGeometryOptions();
                            View ownerView = spatialElement.Document.GetElement(spatialElement.OwnerViewId) as View;
                            if (ownerView != null)
                                geomOptions.View = ownerView;
                            GeometryElement geomElem = spatialElement.get_Geometry(geomOptions);

                            try
                            {
                                exporterIFC.PushExportState(spatialElement, geomElem);

                                using (ProductWrapper productWrapper = ProductWrapper.Create(exporterIFC, true))
                                {
                                    ElementId levelId = spatialElement.Level != null ? spatialElement.Level.Id : ElementId.InvalidElementId;
                                    using (IFCPlacementSetter setter = IFCPlacementSetter.Create(exporterIFC, spatialElement, null, null, levelId))
                                    {
                                        if (!CreateIFCSpace(exporterIFC, spatialElement, productWrapper, setter))
                                            continue;

                                        XYZ offset = GetSapceBoundaryOffset(setter);

                                        //get boundary information from surfaces
                                        IList<EnergyAnalysisSurface> surfaces = space.GetAnalyticalSurfaces();
                                        foreach (EnergyAnalysisSurface surface in surfaces)
                                        {
                                            Element boundingElement = GetBoundaryElement(document, surface.OriginatingElementId);

                                            IList<EnergyAnalysisOpening> openings = surface.GetAnalyticalOpenings();
                                            IFCAnyHandle connectionGeometry = CreateConnectionSurfaceGeometry(file, surface, openings, offset);
                                            CreateIFCSpaceBoundary(file, exporterIFC, spatialElement, boundingElement, setter.LevelId, connectionGeometry);

                                            // try to add doors and windows for host objects if appropriate.
                                            if (boundingElement is HostObject)
                                            {
                                                foreach (EnergyAnalysisOpening opening in openings)
                                                {
                                                    Element openingBoundingElem = GetBoundaryElement(document, opening.OriginatingElementId);
                                                    IFCAnyHandle openingConnectionGeom = CreateConnectionSurfaceGeometry(file, opening, offset);
                                                    CreateIFCSpaceBoundary(file, exporterIFC, spatialElement, openingBoundingElem, setter.LevelId, openingConnectionGeom);
                                                }
                                            }
                                        }
                                        PropertyUtil.CreateInternalRevitPropertySets(exporterIFC, spatialElement, productWrapper);
                                        CreateZoneInfos(exporterIFC, file, spatialElement, productWrapper);
                                        CreateSpaceOccupantInfo(exporterIFC, file, spatialElement, productWrapper);
                                        ifcExporter.ExportElementProperties(exporterIFC, spatialElement, productWrapper);
                                        ifcExporter.ExportElementQuantities(exporterIFC, spatialElement, productWrapper);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                ifcExporter.HandleUnexpectedException(ex, exporterIFC, spatialElement);
                            }
                            finally
                            {
                                exporterIFC.PopExportState();
                            }
                        }
                        transaction.Commit();
                    }
                }
                finally
                {
                    if (model != null)
                        EnergyAnalysisDetailModel.Destroy(model);
                }

                st.RollBack();
            }
        }
        /// <summary>
        /// Export spatial elements, including rooms, areas and spaces. 2nd level space boundaries.
        /// </summary>
        /// <param name="ifcExporter">
        /// The Exporter object.
        /// </param>
        /// <param name="exporterIFC">
        /// The ExporterIFC object.
        /// </param>
        /// <param name="document">
        /// The Revit document.
        /// </param>
        /// <param name="filterView">
        /// The view not exported.
        /// </param>
        /// <param name="spaceExported">
        /// The output boolean value indicates if exported successfully.
        /// </param>
        public static void ExportSpatialElement2ndLevel(BIM.IFC.Exporter.Exporter ifcExporter, ExporterIFC exporterIFC, Document document, View filterView)
        {
            using (SubTransaction st = new SubTransaction(document))
            {
                st.Start();

                bool createEnergyAnalysisDetailModelFailed = false;
                EnergyAnalysisDetailModel model = null;
                try
                {
                    IFCFile file = exporterIFC.GetFile();
                    using (IFCTransaction tr = new IFCTransaction(file))
                    {

                        EnergyAnalysisDetailModelOptions options = new EnergyAnalysisDetailModelOptions();
                        options.Tier = EnergyAnalysisDetailModelTier.SecondLevelBoundaries; //2nd level space boundaries
                        options.SimplifyCurtainSystems = true;
                        try
                        {
                            model = EnergyAnalysisDetailModel.Create(document, options);
                        }
                        catch (Exception)
                        {
                            createEnergyAnalysisDetailModelFailed = true;
                            throw;
                        }
                        IList<EnergyAnalysisSpace> spaces = model.GetAnalyticalSpaces();

                        foreach (EnergyAnalysisSpace space in spaces)
                        {
                            SpatialElement spatialElement = document.get_Element(space.SpatialElementId) as SpatialElement;

                            if (spatialElement == null)
                                continue;

                            //quick reject
                            bool isArea = spatialElement is Area;
                            if (isArea)
                            {
                                if (!IsAreaGrossInterior(exporterIFC, spatialElement))
                                    continue;
                            }

                            //current view only
                            if (filterView != null && !ElementFilteringUtil.IsElementVisible(filterView, spatialElement))
                                continue;
                            //

                            if (!ElementFilteringUtil.ShouldCategoryBeExported(exporterIFC, spatialElement))
                                continue;

                            Options geomOptions = new Options();
                            View ownerView = spatialElement.Document.get_Element(spatialElement.OwnerViewId) as View;
                            if (ownerView != null)
                                geomOptions.View = ownerView;
                            GeometryElement geomElem = spatialElement.get_Geometry(geomOptions);

                            try
                            {
                                exporterIFC.PushExportState(spatialElement, geomElem);

                                using (IFCProductWrapper productWrapper = IFCProductWrapper.Create(exporterIFC, true))
                                {
                                    ElementId levelId = spatialElement.Level != null ? spatialElement.Level.Id : ElementId.InvalidElementId;
                                    using (IFCPlacementSetter setter = IFCPlacementSetter.Create(exporterIFC, spatialElement, null, null, levelId))
                                    {
                                        try
                                        {
                                            CreateIFCSpace(exporterIFC, spatialElement, productWrapper, setter);
                                        }
                                        catch (System.Exception)
                                        {
                                            continue;
                                        }
                                        //get boundary information from surfaces
                                        IList<EnergyAnalysisSurface> surfaces = space.GetAnalyticalSurfaces();
                                        foreach (EnergyAnalysisSurface surface in surfaces)
                                        {
                                            Element boundingElement = GetBoundaryElement(document, surface.OriginatingElementId);
                                            if (boundingElement == null)
                                                continue;

                                            IList<EnergyAnalysisOpening> openings = surface.GetAnalyticalOpenings();
                                            IFCAnyHandle connectionGeometry = CreateConnectionSurfaceGeometry(file, surface, openings);
                                            CreateIFCSpaceBoundary(file, exporterIFC, spatialElement, boundingElement, connectionGeometry);

                                            // try to add doors and windows for host objects if appropriate.
                                            if (boundingElement is HostObject)
                                            {
                                                foreach (EnergyAnalysisOpening opening in openings)
                                                {
                                                    Element openingBoundingElem = GetBoundaryElement(document, opening.OriginatingElementId);
                                                    IFCAnyHandle openingConnectionGeom = CreateConnectionSurfaceGeometry(file, opening);
                                                    CreateIFCSpaceBoundary(file, exporterIFC, spatialElement, openingBoundingElem, openingConnectionGeom);
                                                }
                                            }
                                        }
                                        ExporterIFCUtils.CreateSpatialElementPropertySet(exporterIFC, spatialElement, productWrapper);
                                        ifcExporter.ExportElementProperties(exporterIFC, spatialElement, productWrapper);
                                        ifcExporter.ExportElementQuantities(exporterIFC, spatialElement, productWrapper);
                                    }
                                }
                            }
                            finally
                            {
                                exporterIFC.PopExportState();
                            }
                        }
                        tr.Commit();
                    }
                }
                catch (System.Exception ex)
                {
                    document.Application.WriteJournalComment("IFC error: " + ex.ToString(), true);
                }
                finally
                {
                    if (model != null)
                        EnergyAnalysisDetailModel.Destroy(model);
                }

                //if failed, just export the space element
                if (createEnergyAnalysisDetailModelFailed)
                {
                    IFCFile file = exporterIFC.GetFile();
                    using (IFCTransaction tr = new IFCTransaction(file))
                    {
                        ElementFilter spatialElementFilter = ElementFilteringUtil.GetSpatialElementFilter(document, exporterIFC);
                        FilteredElementCollector collector = (filterView == null) ? new FilteredElementCollector(document) : new FilteredElementCollector(document, filterView.Id);
                        collector.WherePasses(spatialElementFilter);
                        foreach (Element elem in collector)
                        {
                            SpatialElement spatialElement = elem as SpatialElement;

                            if (spatialElement == null)
                                continue;

                            //current view only
                            if (filterView != null && !ElementFilteringUtil.IsElementVisible(filterView, spatialElement))
                                continue;
                            //
                            if (!ElementFilteringUtil.ShouldCategoryBeExported(exporterIFC, spatialElement))
                                continue;

                            Options geomOptions = new Options();
                            View ownerView = spatialElement.Document.get_Element(spatialElement.OwnerViewId) as View;
                            if (ownerView != null)
                                geomOptions.View = ownerView;
                            GeometryElement geomElem = spatialElement.get_Geometry(geomOptions);

                            try
                            {
                                exporterIFC.PushExportState(spatialElement, geomElem);

                                using (IFCProductWrapper productWrapper = IFCProductWrapper.Create(exporterIFC, true))
                                {
                                    ElementId levelId = spatialElement.Level != null ? spatialElement.Level.Id : ElementId.InvalidElementId;
                                    using (IFCPlacementSetter setter = IFCPlacementSetter.Create(exporterIFC, spatialElement, null, null, levelId))
                                    {
                                        try
                                        {
                                            CreateIFCSpace(exporterIFC, spatialElement, productWrapper, setter);
                                        }
                                        catch (System.Exception)
                                        {
                                            continue;
                                        }
                                        if (!(spatialElement is Area))
                                            ExporterIFCUtils.CreateSpatialElementPropertySet(exporterIFC, spatialElement, productWrapper);
                                        ifcExporter.ExportElementProperties(exporterIFC, spatialElement, productWrapper);
                                        ifcExporter.ExportElementQuantities(exporterIFC, spatialElement, productWrapper);
                                    }
                                }
                            }
                            finally
                            {
                                exporterIFC.PopExportState();
                            }
                        }
                        tr.Commit();
                    }
                }
                st.RollBack();
            }
        }
Exemple #29
0
    // ``floor`` rounds towards negative infinity (like SMT-LIBv2's to_int).
    /// <summary>
    /// NOTE:  This may give wrong results, it hasn't been tested extensively
    /// If you're getting weird bugs, you may want to check this function out...
    /// Computes the floor and ceiling of this BigFloat. Note the choice of rounding towards negative
    /// infinity rather than zero for floor is because SMT-LIBv2's to_int function floors this way.
    /// </summary>
    /// <param name="floor">The Floor (rounded towards negative infinity)</param>
    /// <param name="ceiling">Ceiling (rounded towards positive infinity)</param>
    public void FloorCeiling(out BIM floor, out BIM ceiling)
    {
      BIM two = new BIM(2);

      BIM sig = Significand + BIM.Pow(two, SignificandSize); //Add hidden bit
      BIM exp = Exponent - BIM.Pow(two, ExponentSize-1) + 1;

      while (exp > BIM.Zero) {
        exp--;
        sig = sig << 1;
      }

      sig = sig >> SignificandSize;

      if (isNeg) {
        ceiling = -sig + 1;
        floor = -sig;
      }
      else {
        ceiling = sig + 1;
        floor = sig;
      }
    }
Exemple #30
0
    /// <summary>
    /// NOTE:  THIS METHOD MAY NOT WORK AS EXPECTED!!!!
    /// Converts value.dec_value to a the closest float approximation with the given significandSize, exponentSize
    /// Returns the result of this calculation
    /// </summary>
    /// <param name="value"></param>
    /// <param name="power"></param>
    /// <param name="significandSize"></param>
    /// <param name="exponentSize"></param>
    /// <returns></returns>
    public static BigFloat Round(BIM value, BIM dec_value, int exponentSize, int significandSize)
    {
      int exp = 0;
      BIM one = new BIM(1);
      BIM ten = new BIM(10);
      BIM dec_max = new BIM(0); //represents the order of magnitude of dec_value for carrying during calculations

      //First, determine the exponent
      while (value > one) { //Divide by two, increment exponent by 1
        if (!(value % two).IsZero) { //Add "1.0" to the decimal
          dec_max = new BIM(10);
          while (dec_max < dec_value)
            dec_max = dec_max * ten;
          dec_value = dec_value + dec_max;
        }
        value = value / two;
        if (!(dec_value % ten).IsZero)
          dec_value = dec_value * ten; //Creates excess zeroes to avoid losing data during division
        dec_value = dec_value / two;
        exp++;
      }
      if (value.IsZero && !dec_value.IsZero) {
        dec_max = new BIM(10);
        while (dec_max < dec_value)
          dec_max = dec_max * ten;
        while (value.IsZero) {//Multiply by two, decrement exponent by 1
          dec_value = dec_value * two;
          if (dec_value >= dec_max) {
            dec_value = dec_value - dec_max;
            value = value + one;
          }
          exp--;
        }
      }

      //Second, calculate the significand
      value = new BIM(0); //remove implicit bit
      dec_max = new BIM(10);
      while (dec_max < dec_value)
        dec_max = dec_max * ten;
      for (int i = significandSize; i > 0 && !dec_value.IsZero; i--) { //Multiply by two until the significand is fully calculated
        dec_value = dec_value * two;
        if (dec_value >= dec_max) {
          dec_value = dec_value - dec_max;
          value = value + two_n(i); //Note that i is decrementing so that the most significant bit is left-most in the representation
        }
      }

      return new BigFloat(false, BIM.Zero, BIM.Parse(value.ToString()), exponentSize, significandSize); //Sign not actually checked...
    }
Exemple #31
0
 public static BigFloat FromBigInt(BIM v)
 {
     return(new BigFloat(v.ToString(), 24, 8));
 }
Exemple #32
0
        public static BigFloat operator *(BigFloat x, BigFloat y)
        {
            Contract.Requires(x.exponentSize == y.exponentSize);
            Contract.Requires(x.significandSize == y.significandSize);

            if (x.value == "NaN" || y.value == "NaN" || (x.value == "+oo" || x.value == "-oo") && y.IsZero || (y.value == "+oo" || y.value == "-oo") && x.IsZero)
            {
                return(new BigFloat("NaN", x.significandSize, x.exponentSize));
            }

            if (x.value != "" || y.value != "")
            {
                bool xSignBitSet = x.value == "" ? x.isSignBitSet : x.value[0] == '-';
                bool ySignBitSet = y.value == "" ? y.isSignBitSet : y.value[0] == '-';
                return(new BigFloat((xSignBitSet ^ ySignBitSet ? "-" : "+") + "oo", x.significandSize, x.exponentSize));
            }

            BIM xsig = x.significand, ysig = y.significand;
            BIM xexp = x.exponent, yexp = y.exponent;

            BIM hiddenBitPow = BIM.Pow(2, x.significandSize - 1);

            if (xexp > 0)
            {
                xsig += hiddenBitPow;
            }
            else
            {
                ++xexp;
            }

            if (yexp > 0)
            {
                ysig += hiddenBitPow;
            }
            else
            {
                ++yexp;
            }

            ysig *= xsig;
            yexp += xexp - (BIM.Pow(2, x.exponentSize - 1) - 1) - (x.significandSize - 1);

            while (ysig >= hiddenBitPow * 2 || yexp <= 0)
            {
                ysig >>= 1;
                ++yexp;
            }

            while (ysig < hiddenBitPow && yexp > 1)
            {
                ysig <<= 1;
                --yexp;
            }

            if (ysig < hiddenBitPow)
            {
                yexp = 0;
            }
            else
            {
                ysig -= hiddenBitPow;
            }

            if (yexp >= BIM.Pow(2, x.exponentSize) - 1)
            {
                return(new BigFloat(x.isSignBitSet ^ y.isSignBitSet ? "-oo" : "+oo", x.significandSize, x.exponentSize));
            }

            return(new BigFloat(x.isSignBitSet ^ y.isSignBitSet, ysig, yexp, x.significandSize, x.exponentSize));
        }
Exemple #33
0
 public static BigFloat FromBigInt(BIM v, int significandSize, int exponentSize)
 {
   return new BigFloat(v.ToString(), significandSize, exponentSize);
 }
Exemple #34
0
 public static BigFloat FromBigInt(BIM v) {
   return new BigFloat(v.ToString(), 24, 8);
 }
Exemple #35
0
        public static BigFloat operator +(BigFloat x, BigFloat y)
        {
            Contract.Requires(x.exponentSize == y.exponentSize);
            Contract.Requires(x.significandSize == y.significandSize);

            if (x.value != "" || y.value != "")
            {
                if (x.value == "NaN" || y.value == "NaN" || x.value == "+oo" && y.value == "-oo" || x.value == "-oo" && y.value == "+oo")
                {
                    return(new BigFloat("NaN", x.significandSize, x.exponentSize));
                }

                if (x.value != "")
                {
                    return(new BigFloat(x.value, x.significandSize, x.exponentSize));
                }

                return(new BigFloat(y.value, y.significandSize, y.exponentSize));
            }

            if (x.exponent > y.exponent)
            {
                BigFloat temp = x;
                x = y;
                y = temp;
            }

            BIM xsig = x.significand, ysig = y.significand;
            BIM xexp = x.exponent, yexp = y.exponent;

            if (yexp - xexp > x.significandSize) //One of the numbers is relatively insignificant
            {
                return(new BigFloat(y.isSignBitSet, y.significand, y.exponent, y.significandSize, y.exponentSize));
            }

            BIM hiddenBitPow = BIM.Pow(2, x.significandSize - 1);

            if (xexp > 0)
            {
                xsig += hiddenBitPow;
            }
            else
            {
                ++xexp;
            }

            if (yexp > 0)
            {
                ysig += hiddenBitPow;
            }
            else
            {
                ++yexp;
            }

            if (x.isSignBitSet)
            {
                xsig = -xsig;
            }
            if (y.isSignBitSet)
            {
                ysig = -ysig;
            }

            xsig >>= (int)(yexp - xexp); //Guaranteed to fit in a 32-bit integer

            ysig += xsig;

            bool isNeg = ysig < 0;

            ysig = BIM.Abs(ysig);

            if (ysig == 0)
            {
                return(new BigFloat(x.isSignBitSet && y.isSignBitSet, 0, 0, x.significandSize, x.exponentSize));
            }

            if (ysig >= hiddenBitPow * 2)
            {
                ysig >>= 1;
                ++yexp;
            }

            while (ysig < hiddenBitPow && yexp > 1)
            {
                ysig <<= 1;
                --yexp;
            }

            if (ysig < hiddenBitPow)
            {
                yexp = 0;
            }
            else
            {
                ysig -= hiddenBitPow;
            }

            if (yexp >= BIM.Pow(2, x.exponentSize) - 1)
            {
                return(new BigFloat(y.isSignBitSet ? "-oo" : "+oo", x.significandSize, x.exponentSize));
            }

            return(new BigFloat(isNeg, ysig, yexp, x.significandSize, x.exponentSize));
        }
Exemple #36
0
 public BigFloat(bool isNeg, BIM significand, BIM exponent, int significandSize, int exponentSize) {
   this.exponentSize = exponentSize;
   this.exponent = exponent;
   this.significand = significand;
   this.significandSize = significandSize+1;
   this.isNeg = isNeg;
   this.value = "";
 }
Exemple #37
0
        /// <summary>
        /// NOTE:  THIS METHOD MAY NOT WORK AS EXPECTED!!!!
        /// Converts value.dec_value to a the closest float approximation with the given significandSize, exponentSize
        /// Returns the result of this calculation
        /// </summary>
        /// <param name="value"></param>
        /// <param name="power"></param>
        /// <param name="significandSize"></param>
        /// <param name="exponentSize"></param>
        /// <returns></returns>
        public static BigFloat Round(BIM value, BIM dec_value, int exponentSize, int significandSize)
        {
            int exp     = 0;
            BIM one     = new BIM(1);
            BIM ten     = new BIM(10);
            BIM dec_max = new BIM(0); //represents the order of magnitude of dec_value for carrying during calculations

            //First, determine the exponent
            while (value > one)            //Divide by two, increment exponent by 1
            {
                if (!(value % two).IsZero) //Add "1.0" to the decimal
                {
                    dec_max = new BIM(10);
                    while (dec_max < dec_value)
                    {
                        dec_max = dec_max * ten;
                    }
                    dec_value = dec_value + dec_max;
                }
                value = value / two;
                if (!(dec_value % ten).IsZero)
                {
                    dec_value = dec_value * ten; //Creates excess zeroes to avoid losing data during division
                }
                dec_value = dec_value / two;
                exp++;
            }
            if (value.IsZero && !dec_value.IsZero)
            {
                dec_max = new BIM(10);
                while (dec_max < dec_value)
                {
                    dec_max = dec_max * ten;
                }
                while (value.IsZero)//Multiply by two, decrement exponent by 1
                {
                    dec_value = dec_value * two;
                    if (dec_value >= dec_max)
                    {
                        dec_value = dec_value - dec_max;
                        value     = value + one;
                    }
                    exp--;
                }
            }

            //Second, calculate the significand
            value   = new BIM(0); //remove implicit bit
            dec_max = new BIM(10);
            while (dec_max < dec_value)
            {
                dec_max = dec_max * ten;
            }
            for (int i = significandSize; i > 0 && !dec_value.IsZero; i--) //Multiply by two until the significand is fully calculated
            {
                dec_value = dec_value * two;
                if (dec_value >= dec_max)
                {
                    dec_value = dec_value - dec_max;
                    value     = value + two_n(i); //Note that i is decrementing so that the most significant bit is left-most in the representation
                }
            }

            return(new BigFloat(false, BIM.Zero, BIM.Parse(value.ToString()), exponentSize, significandSize)); //Sign not actually checked...
        }
Exemple #38
0
 public static BigDec FromBigInt(BIM v) {
   return new BigDec(v, 0);
 }
Exemple #39
0
 internal BigDec(BIM mantissa, int exponent) {
   if (mantissa.IsZero) {
     this.mantissa = mantissa;
     this.exponent = 0;
   }
   else {
     while (mantissa % ten == BIM.Zero) {
       mantissa = mantissa / ten;
       exponent = exponent + 1;
     }
     this.mantissa = mantissa;
     this.exponent = exponent;
   }
 }
Exemple #40
0
        public static BigFloat FromString(String s)
        {
            /*
             * String must be either of the format [-]0x^.^e*f*e*
             * or of the special value formats: 0NaN*e* 0nan*e* 0+oo*e* 0-oo*e*
             * Where ^ indicates a hexadecimal value and * indicates an integer value
             */

            int posLastE = s.LastIndexOf('e');

            int expSize = int.Parse(s.Substring(posLastE + 1));

            if (expSize <= 1)
            {
                throw new FormatException("Exponent size must be greater than 1");
            }

            int posLastF = s.LastIndexOf('f');
            int posSig   = posLastF + 1;

            if (posLastF == -1)//NaN, +oo, -oo
            {
                posSig = 4;
            }

            int sigSize = int.Parse(s.Substring(posSig, posLastE - posSig));

            if (sigSize <= 1)
            {
                throw new FormatException("Significand size must be greater than 1");
            }

            if (posLastF == -1)//NaN, +oo, -oo
            {
                return(new BigFloat(s.Substring(1, 3), sigSize, expSize));
            }

            bool isNeg = s[0] == '-';

            int posX           = s.IndexOf('x');
            int posSecondLastE = s.LastIndexOf('e', posLastE - 1);

            string hexSig = s.Substring(posX + 1, posSecondLastE - (posX + 1));
            BIM    oldExp = BIM.Parse(s.Substring(posSecondLastE + 1, posLastF - (posSecondLastE + 1)));

            string binSig = string.Join(string.Empty,
                                        hexSig.Select(
                                            c => (c == '.' ? "." : Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0'))
                                            )
                                        );

            int posDec = binSig.IndexOf('.');

            binSig = binSig.Remove(posDec, 1);

            int posFirstOne = binSig.IndexOf('1');
            int posLastOne  = binSig.LastIndexOf('1');

            if (posFirstOne == -1)
            {
                return(new BigFloat(isNeg, 0, 0, sigSize, expSize));
            }

            binSig = binSig.Substring(posFirstOne, posLastOne - posFirstOne + 1);

            BIM bias       = two_n(expSize - 1) - 1;
            BIM upperBound = 2 * bias + 1;

            BIM newExp = 4 * oldExp + bias + (posDec - posFirstOne - 1);

            if (newExp <= 0)
            {
                if (-newExp <= (sigSize - 1) - binSig.Length)
                {
                    binSig = new string('0', (int)-newExp) + binSig;
                    newExp = 0;
                }
            }
            else
            {
                binSig = binSig.Substring(1);
            }

            if (newExp < 0 || newExp >= upperBound)
            {
                throw new FormatException("The given exponent cannot fit in the bit size " + expSize);
            }

            binSig = binSig.PadRight(sigSize - 1, '0');

            if (binSig.Length > sigSize - 1)
            {
                throw new FormatException("The given significand cannot fit in the bit size " + (sigSize - 1));
            }

            BIM newSig = 0;

            foreach (char b in binSig)
            {
                if (b != '.')
                {
                    newSig <<= 1;
                    newSig  += b - '0';
                }
            }

            return(new BigFloat(isNeg, newSig, newExp, sigSize, expSize));
        }
Exemple #41
0
    ////////////////////////////////////////////////////////////////////////////
    // Conversion operations

    // ``floor`` rounds towards negative infinity (like SMT-LIBv2's to_int).
    /// <summary>
    /// Computes the floor and ceiling of this BigDec. Note the choice of rounding towards negative
    /// infinity rather than zero for floor is because SMT-LIBv2's to_int function floors this way.
    /// </summary>
    /// <param name="floor">The Floor (rounded towards negative infinity)</param>
    /// <param name="ceiling">Ceiling (rounded towards positive infinity)</param>
    public void FloorCeiling(out BIM floor, out BIM ceiling) {
      BIM n = this.mantissa;
      int e = this.exponent;
      if (n.IsZero) {
        floor = ceiling = n;
      } else if (0 <= e) {
        // it's an integer
        for (; 0 < e; e--) {
          n = n * ten;
        }
        floor = ceiling = n;
      } else {
        // it's a non-zero integer, so the ceiling is one more than the floor
        for (; e < 0 && !n.IsZero; e++) {
          n = n / ten;  // Division rounds towards negative infinity
        }

        if (this.mantissa >= 0) {
          floor = n;
          ceiling = n + 1;
        } else {
          ceiling = n;
          floor = n - 1;
        }
      }
      Debug.Assert(floor <= ceiling, "Invariant was not maintained");
    }
Exemple #42
0
        ////////////////////////////////////////////////////////////////////////////
        // Conversion operations

        // ``floor`` rounds towards negative infinity (like SMT-LIBv2's to_int).
        /// <summary>
        /// Computes the floor and ceiling of this BigFloat. Note the choice of rounding towards negative
        /// infinity rather than zero for floor is because SMT-LIBv2's to_int function floors this way.
        /// </summary>
        /// <param name="floor">Floor (rounded towards negative infinity)</param>
        /// <param name="ceiling">Ceiling (rounded towards positive infinity)</param>
        public void FloorCeiling(out BIM floor, out BIM ceiling)
        {
            Contract.Requires(value == "");

            BIM sig = significand;
            BIM exp = exponent;

            BIM hiddenBitPow = BIM.Pow(2, significandSize - 1);

            if (exponent > 0)
            {
                sig += hiddenBitPow;
            }
            else
            {
                ++exp;
            }

            exp -= (BIM.Pow(2, exponentSize - 1) - 1) + (significandSize - 1);

            if (exp >= BIM.Zero)
            {
                while (exp >= int.MaxValue)
                {
                    sig <<= int.MaxValue;
                    exp  -= int.MaxValue;
                }

                sig <<= (int)exp;
                floor = ceiling = (isSignBitSet ? -sig : sig);
            }
            else
            {
                exp = -exp;

                if (exp > significandSize)
                {
                    if (sig == 0)
                    {
                        floor = ceiling = 0;
                    }
                    else
                    {
                        ceiling = isSignBitSet ? 0 : 1;
                        floor   = ceiling - 1;
                    }
                }
                else
                {
                    BIM frac = sig & ((BIM.One << (int)exp) - 1);
                    sig >>= (int)exp; //Guaranteed to fit in a 32-bit integer

                    if (frac == 0)
                    {
                        floor = ceiling = (isSignBitSet ? -sig : sig);
                    }
                    else
                    {
                        ceiling = isSignBitSet ? -sig : sig + 1;
                        floor   = ceiling - 1;
                    }
                }
            }
        }