예제 #1
0
파일: Problem.cs 프로젝트: Hrenaki/2D-FEM
        public static ParabolicProblem ReadProblemFrom(string directory)
        {
            ExpressionParser parser = new ExpressionParser("x", "y", "t");

            string        elemsPath               = "";
            string        vertexesPath            = "";
            List <string> boundaryConditionsPaths = new List <string>();
            string        sigmaPath               = "";
            string        lambdaPath              = "";
            string        functionPath            = "";
            string        timeLayersPath          = "";
            string        initialConditionPath    = "";

            string[] files = null;

            if (!Directory.Exists(directory) || (files = Directory.GetFiles(directory)).Length == 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Directory doesnt exist or empty. Try again");
                Console.ResetColor();
                return(null);
            }

            int    i;
            string temp;

            for (i = 0; i < files.Length; i++)
            {
                temp = Path.GetFileName(files[i]);
                switch (temp)
                {
                case "vertexes.txt": vertexesPath = files[i]; break;

                case "elements.txt": elemsPath = files[i]; break;

                case "sigma.txt": sigmaPath = files[i]; break;

                case "lambda.txt": lambdaPath = files[i]; break;

                case "function.txt": functionPath = files[i]; break;

                case "timeLayers.txt": timeLayersPath = files[i]; break;

                case "initialCondition.txt": initialConditionPath = files[i]; break;

                default:
                    if (temp.StartsWith("S") && temp.EndsWith(".txt"))
                    {
                        boundaryConditionsPaths.Add(files[i]);
                    }
                    break;
                }
            }

            // Reading vertexes
            string[] content       = File.ReadAllLines(vertexesPath);
            int      vertexesCount = content.Length;

            double[][] vertexes = new double[vertexesCount][];
            for (i = 0; i < vertexesCount; i++)
            {
                vertexes[i] = content[i].Split(' ').Select(v => double.Parse(v)).ToArray();
            }

            // Reading and compiling all function parts in right part of equation
            content = File.ReadAllLines(functionPath);
            Func[] function = new Func[content.Length];
            for (i = 0; i < function.Length; i++)
            {
                function[i] = parser.GetFunction(content[i]);
            }

            // Reading elements
            content = File.ReadAllLines(elemsPath);
            int elementCount = content.Length;

            int[][] elements = new int[elementCount][];
            for (i = 0; i < elementCount; i++)
            {
                elements[i] = content[i].Split(' ').Select(e => int.Parse(e)).ToArray();
            }

            // Reading materials and diff coeffs
            double[] sigma  = File.ReadAllText(sigmaPath).Split(' ').Select(m => double.Parse(m)).ToArray();
            double[] lambda = File.ReadAllText(lambdaPath).Split(' ').Select(m => double.Parse(m)).ToArray();

            // Reading time layers
            double[] timeLayers = File.ReadAllText(timeLayersPath).Split(' ').Select(m => double.Parse(m)).ToArray();
            //Reading initial function
            Func initialCondition = parser.GetFunction(File.ReadAllText(initialConditionPath));

            // Reading boundary conditions
            string[][] boundCond_str = new string[boundaryConditionsPaths.Count][];
            for (i = 0; i < boundaryConditionsPaths.Count; i++)
            {
                boundCond_str[i] = File.ReadAllLines(boundaryConditionsPaths[i]);
            }
            BoundaryCondition[] conditions = new BoundaryCondition[boundCond_str.Length];
            Func[] values = new Func[boundCond_str.Length];
            for (i = 0; i < conditions.Length; i++)
            {
                conditions[i] = BoundaryCondition.Parse(parser, boundCond_str[i][0],
                                                        boundCond_str[i][1], boundCond_str[i][2], boundCond_str[i].Length > 3 ? boundCond_str[i][3] : null);
            }

            return(new ParabolicProblem(elementCount, vertexesCount, elements, vertexes,
                                        sigma, lambda, function, timeLayers, initialCondition, conditions));
        }
예제 #2
0
파일: Problem.cs 프로젝트: Hrenaki/2D-FEM
        public static EllipticalProblem ReadProblemFrom(string directory)
        {
            ExpressionParser parser = new ExpressionParser();

            string        elemsPath               = "";
            string        vertexesPath            = "";
            List <string> boundaryConditionsPaths = new List <string>();
            string        materialPath            = "";
            string        diffCoeffsPath          = "";
            string        functionPath            = "";

            string[] files = Directory.GetFiles(directory);

            if (!Directory.Exists(directory) && files.Length != 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Directory isn't exist or empty. Try again");
                Console.ResetColor();
                return(null);
            }

            int    i;
            string temp;

            for (i = 0; i < files.Length; i++)
            {
                temp = Path.GetFileName(files[i]);
                switch (temp)
                {
                case "vertexes.txt": vertexesPath = files[i]; break;

                case "elements.txt": elemsPath = files[i]; break;

                case "gamma.txt": materialPath = files[i]; break;

                case "lambda.txt": diffCoeffsPath = files[i]; break;

                case "function.txt": functionPath = files[i]; break;

                default:
                    if (temp.StartsWith("S") && temp.EndsWith(".txt"))
                    {
                        boundaryConditionsPaths.Add(files[i]);
                    }
                    break;
                }
            }

            // Reading vertexes
            string[] content       = File.ReadAllLines(vertexesPath);
            int      vertexesCount = content.Length;

            double[][] vertexes = new double[vertexesCount][];
            for (i = 0; i < vertexesCount; i++)
            {
                vertexes[i] = content[i].Split(' ').Select(v => double.Parse(v)).ToArray();
            }

            // Reading and compiling all function parts in right part of equation
            content = File.ReadAllLines(functionPath);
            Func[] function = new Func[content.Length];
            for (i = 0; i < function.Length; i++)
            {
                function[i] = parser.GetFunction(content[i]);
            }

            // Reading elements
            content = File.ReadAllLines(elemsPath);
            int elemCount = content.Length;

            int[][] elems = new int[elemCount][];
            for (i = 0; i < elemCount; i++)
            {
                elems[i] = content[i].Split(' ').Select(e => int.Parse(e)).ToArray();
            }

            // Reading materials and diff coeffs
            double[] materials  = File.ReadAllText(materialPath).Split(' ').Select(m => double.Parse(m)).ToArray();
            double[] diffCoeffs = File.ReadAllText(diffCoeffsPath).Split(' ').Select(m => double.Parse(m)).ToArray();

            // Reading boundary conditions
            string[][] boundCond_str = new string[boundaryConditionsPaths.Count][];
            for (i = 0; i < boundaryConditionsPaths.Count; i++)
            {
                boundCond_str[i] = File.ReadAllLines(boundaryConditionsPaths[i]);
            }
            BoundaryCondition[] conditions = new BoundaryCondition[boundCond_str.Length];
            Func[] values = new Func[boundCond_str.Length];
            for (i = 0; i < conditions.Length; i++)
            {
                conditions[i] = BoundaryCondition.Parse(parser, boundCond_str[i][0],
                                                        boundCond_str[i][1], boundCond_str[i][2]);
            }

            return(new EllipticalProblem(elemCount, vertexesCount, elems, vertexes,
                                         materials, diffCoeffs, function, conditions));
        }