Exemple #1
0
        public void ExportBinary(Database DB, int[] nDOF_reduction, double[] F, int inc, string path, string type)
        {
            Stopwatch sw = new Stopwatch(); sw.Start();

            Console.Write("   Export to C++ Linear Solver: "); // Create output in console

            // Global Stiffness Matrix Initialization
            int nFixDOF = nDOF_reduction.Where(x => x == -1).Count();
            Dictionary <Tuple <int, int>, double> K = new Dictionary <Tuple <int, int>, double>();

            // Assembly loop

            Parallel.ForEach(DB.ElemLib.Values, Elem =>
            {
                MatrixST k = new MatrixST(3 * Elem.NList.Count, 3 * Elem.NList.Count);
                if (type == "Initial")
                {
                    k = Elem.K_Initial(inc, DB);
                }
                if (type == "Tangent")
                {
                    k = Elem.K_Tangent(inc, DB);
                }

                // Assembly to Global Stiffness Matrix
                for (int i = 0; i < Elem.NList.Count; i++)
                {
                    for (int m = 0; m < 3; m++)
                    {
                        for (int j = 0; j < Elem.NList.Count; j++)
                        {
                            for (int n = 0; n < 3; n++)
                            {
                                int row = DB.NodeLib[Elem.NList[i]].DOF[m];
                                int col = DB.NodeLib[Elem.NList[j]].DOF[n];

                                // Only Upper Left triangle is needed
                                if (col >= row)
                                {
                                    // K matrix with Esential BC included
                                    if (nDOF_reduction[row] != -1)
                                    {
                                        if (nDOF_reduction[col] != -1)
                                        {
                                            lock (K)
                                            {
                                                Tuple <int, int> key = new Tuple <int, int>(row - nDOF_reduction[row], col - nDOF_reduction[col]);
                                                if (K.ContainsKey(key))
                                                {
                                                    K[key] += k.GetFast(i * 3 + m, j * 3 + n);
                                                }
                                                else
                                                {
                                                    K.Add(key, k.GetFast(i * 3 + m, j * 3 + n));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                ;
            });

            // Write binary K matrix
            using (BinaryWriter b = new BinaryWriter(File.Open(path + @"\K.bin", FileMode.Create)))
            {
                foreach (KeyValuePair <Tuple <int, int>, double> i in K)
                {
                    b.Write(i.Key.Item1); b.Write(i.Key.Item2); b.Write(i.Value);
                }
            }
            // Write binary F vector
            using (BinaryWriter b = new BinaryWriter(File.Open(path + @"\F.bin", FileMode.Create)))
            {
                foreach (double i in F)
                {
                    b.Write(i);
                }
            }
            //using (StreamWriter b = new StreamWriter(File.Open(path + @"\STAN_KMatrix.txt", FileMode.Create)))
            //{
            //    foreach (KeyValuePair<Tuple<int, int>, double> i in K)
            //    {
            //        b.Write(i.Key.Item1); b.Write(" "); b.Write(i.Key.Item2); b.Write(" "); b.Write(i.Value); b.Write("\n");
            //    }
            //}
            sw.Stop();
            Console.WriteLine("          Done in " + sw.Elapsed.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + "s");
        }
Exemple #2
0
        // -------------------- Finite Element matrix methods -----------------------------

        public alglib.sparsematrix ParallelAssembly_K(Database DB, int[] nDOF_reduction, int inc, string type)
        {
            Stopwatch sw = new Stopwatch(); sw.Start();

            // Global Stiffness Matrix Initialization
            int nFixDOF = nDOF_reduction.Where(x => x == -1).Count();

            alglib.sparsecreate(DB.nDOF - nFixDOF, DB.nDOF - nFixDOF, out alglib.sparsematrix K);

            // Assembly loop

            Console.Write("   K Matrix assembly: "); // Create output in console

            Parallel.ForEach(DB.ElemLib.Values, Elem =>
            {
                MatrixST k = new MatrixST(3 * Elem.NList.Count, 3 * Elem.NList.Count);

                if (type == "Initial")
                {
                    k = Elem.K_Initial(inc, DB);
                }
                if (type == "Tangent")
                {
                    k = Elem.K_Tangent(inc, DB);
                }

                // Assembly to Global Stiffness Matrix
                for (int i = 0; i < Elem.NList.Count; i++)
                {
                    for (int m = 0; m < 3; m++)
                    {
                        for (int j = 0; j < Elem.NList.Count; j++)
                        {
                            for (int n = 0; n < 3; n++)
                            {
                                int row = DB.NodeLib[Elem.NList[i]].DOF[m];
                                int col = DB.NodeLib[Elem.NList[j]].DOF[n];

                                // Only Upper Left triangle is needed
                                if (col >= row)
                                {
                                    // K matrix with Esential BC included
                                    if (nDOF_reduction[row] != -1)
                                    {
                                        if (nDOF_reduction[col] != -1)
                                        {
                                            lock (K)
                                            {
                                                alglib.sparseadd(K, row - nDOF_reduction[row], col - nDOF_reduction[col],
                                                                 k.GetFast(i * 3 + m, j * 3 + n));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                ;
            });

            sw.Stop();
            Console.WriteLine("          Done in " + sw.Elapsed.TotalSeconds.ToString("F2", CultureInfo.InvariantCulture) + "s");

            return(K);
        }