Example #1
0
        /// <summary>
        /// Quadrature rule on cell boundaries
        /// </summary>
        public IQuadRuleFactory <CellBoundaryQuadRule> GetCellFaceFactory(int levSetIndex, RefElement Kref, JumpTypes jumpType)
        {
            int D = this.m_LevelSetDatas[0].GridDat.SpatialDimension;

            if (D == 2)
            {
                if (jumpType != JumpTypes.Heaviside && jumpType != JumpTypes.OneMinusHeaviside)
                {
                    throw new NotSupportedException();
                }
                Debug.Assert(CellFaceVolume_in3D == null);

                if (LineAndPoint_in2D == null)
                {
                    LineAndPoint_in2D = new LineAndPointQuadratureFactory[this.m_LevelSetDatas.Length];
                }

                if (LineAndPoint_in2D[levSetIndex] == null)
                {
                    LineAndPoint_in2D[levSetIndex] = new LineAndPointQuadratureFactory(Kref, this.m_LevelSetDatas[levSetIndex], true);
                }

                return(LineAndPoint_in2D[levSetIndex].GetLineFactory(jumpType == JumpTypes.Heaviside ? true : false));
            }
            else if (D == 3)
            {
                Debug.Assert(LineAndPoint_in2D == null);

                if (jumpType != JumpTypes.Heaviside)
                {
                    throw new NotSupportedException();
                }

                if (CellFaceVolume_in3D == null)
                {
                    CellFaceVolume_in3D = new LevelSetEdgeVolumeQuadRuleFactory[this.m_LevelSetDatas.Length];
                }

                var rootFindingAlgorithm = new LineSegment.SafeGuardedNewtonMethod(1e-14);
                CellFaceVolume_in3D[levSetIndex] = new LevelSetEdgeVolumeQuadRuleFactory(
                    this.m_LevelSetDatas[levSetIndex], rootFindingAlgorithm, JumpTypes.Heaviside);



                return(CellFaceVolume_in3D[levSetIndex]);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Example #2
0
        public void Test2DVolumeConvergenceUnstructured()
        {
            int[]       orders = Enumerable.Range(0, 6).ToArray();
            GridSizes[] sizes  = new GridSizes[] { GridSizes.Small, GridSizes.Normal, GridSizes.Large };
            double[,] results = new double[sizes.Length, orders.Length];
            var rootFindingAlgorithm = new LineSegment.SafeGuardedNewtonMethod(1e-14);

            for (int i = 0; i < sizes.Length; i++)
            {
                ITestCase testCase = new MinGibou1EllipseArea(sizes[i], GridTypes.PseudoStructured);
                testCase.ScaleShifts(0.5 * testCase.GridSpacing);

                Program app = new Program(testCase);
                app.Init(null);
                app.SetUpEnvironment();
                app.SetInitial();
                testCase.ProceedToNextShift();
                double referenceValue = app.SetUpConfiguration();

                for (int j = 0; j < orders.Length; j++)
                {
                    var result = app.PerformConfiguration(
                        Modes.HMFClassic,
                        orders[j],
                        rootFindingAlgorithm: rootFindingAlgorithm);
                    results[i, j] = Math.Abs(result.Item1 - referenceValue);
                }
            }

            double[] xValues = sizes.Select(s => - Math.Log(2.0) * (int)s).ToArray();
            for (int j = 0; j < orders.Length; j++)
            {
                double[] yValues = new double[sizes.Length];

                for (int i = 0; i < sizes.Length; i++)
                {
                    yValues[i] = Math.Log(results[i, j]);
                }

                double eoc = Regression(xValues, yValues);

                Console.WriteLine(eoc);
                Assert.That(
                    eoc > orders[j] + 1,
                    "Convergence order too low for order " + orders[j]);
            }
        }
Example #3
0
        /// <summary>
        /// Returns a rule for the edges of surface-elements (elements on the zero-level-set surface,
        /// i.e. on \f$  K \cap \mathfrak{I}\f$ .
        /// (point integrals in 2D, Line integrals in 3D)
        /// </summary>
        /// <returns>
        /// the returned factory produces <see cref="CellBoundaryQuadRule"/>'s
        /// </returns>
        IQuadRuleFactory <CellBoundaryQuadRule> _GetSurfaceElement_BoundaryRuleFactory(int levSetIndex, RefElement KrefVol)
        {
            int D = this.m_LevelSetDatas[0].GridDat.SpatialDimension;

            if (D == 2)
            {
                if (LineAndPoint_in2D == null)
                {
                    LineAndPoint_in2D = new LineAndPointQuadratureFactory[this.m_LevelSetDatas.Length];
                }

                if (LineAndPoint_in2D[levSetIndex] == null)
                {
                    LineAndPoint_in2D[levSetIndex] = new LineAndPointQuadratureFactory(
                        KrefVol,
                        this.m_LevelSetDatas[levSetIndex],
                        CutCellQuadratureType == MomentFittingVariants.OneStepGaussAndStokes);
                }

                return(LineAndPoint_in2D[levSetIndex].GetPointFactory());
            }
            else
            {
                //throw new NotImplementedException("3d is not implemented yet");
                Debug.Assert(LineAndPoint_in2D == null);

                if (CellFaceSurface_in3D == null)
                {
                    CellFaceSurface_in3D = new LevelSetEdgeSurfaceQuadRuleFactory[this.m_LevelSetDatas.Length];
                }

                var rootFindingAlgorithm  = new LineSegment.SafeGuardedNewtonMethod(1e-14);
                var CoFaceQuadRuleFactory = new CutLineOnEdgeQuadRuleFactory(
                    this.m_LevelSetDatas[levSetIndex],
                    rootFindingAlgorithm,
                    JumpTypes.Heaviside);
                CellFaceSurface_in3D[levSetIndex] = new LevelSetEdgeSurfaceQuadRuleFactory(
                    this.m_LevelSetDatas[levSetIndex],
                    CoFaceQuadRuleFactory,
                    JumpTypes.Heaviside);
                //new LevelSetEdgeVolumeQuadRuleFactory(
                //    lsTrk, levSetIndex, rootFindingAlgorithm, JumpTypes.Heaviside);

                return(CellFaceSurface_in3D[levSetIndex]);
            }
        }
Example #4
0
        protected override double RunSolverOneStep(int TimestepNo, double phystime, double dt)
        {
            Stopwatch globalWatch = new Stopwatch();

            globalWatch.Start();

            /*
             * Configuration options
             */
            // Only applies to subdivision-based quadrature (brute force and adaptive)
            int[] divisions = new int[] { (int)testCase.GridSize };

            // Only applies to adaptive quadrature
            int leafDivisions = -1;

            // Only applies to regularized quadrature
            double[] widths                = new double[] { double.NaN };
            int[]    vanishingMonents      = new int[] { int.MinValue };
            int[]    continuousDerivatives = new int[] { int.MinValue };

            // Only applies to HMF quadrature
            LineSegment.IRootFindingAlgorithm rootFindingAlgorithm;


            /*
             * ENTER CONFIGURATION HERE
             */

            // Export options
            int  plotSuperSampling           = 3;
            bool logVolumeNodes              = true;
            int  logVolumeNodes_selectedCell = -1;
            bool logSurfaceNodes             = false;
            bool logConsole    = true;
            int  selectedShift = -1;

            // Quadrature variant

            Modes mode = Modes.SayeGaussRules;

            int[] orders = new int[] { 3 };

            //Modes mode = Modes.HMFClassic;
            //int[] orders = new int[] { 3, 4, 5, 6, 7, 8 };
            //LevelSetSurfaceQuadRuleFactory.RestrictNodes = false;
            //LevelSetSurfaceQuadRuleFactory.UseGaussNodes = true;
            //LevelSetVolumeQuadRuleFactory.RestrictNodes = false;
            //LevelSetVolumeQuadRuleFactory.UseGaussNodes = true;
            //LevelSetVolumeQuadRuleFactory.NodeCountSafetyFactor = 1.0;

            //Modes mode = Modes.HMFClassic;
            //int[] orders = new int[] { 1 };
            //LevelSetSurfaceQuadRuleFactory.RestrictNodes = false;
            //LevelSetSurfaceQuadRuleFactory.UseGaussNodes = true;
            //LevelSetVolumeQuadRuleFactory.RestrictNodes = false;
            //LevelSetVolumeQuadRuleFactory.UseGaussNodes = true;
            //LevelSetVolumeQuadRuleFactory.NodeCountSafetyFactor = 1.0;

            //Modes mode = Modes.HMFOneStepGaussAndStokes;
            //int[] orders = new int[] { 2, 4, 6, 8, 10 };

            //Modes mode = Modes.Adaptive;
            //int[] orders = new int[] { 1 };
            //int[] divisions = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
            //int leafDivisions = 1;

            //Modes mode = Modes.Regularized;
            //int[] orders = Enumerable.Range(1, 20).Select(k => 2 * k - 1).ToArray();
            //widths = new double[] { 0.1, 0.2 };
            //vanishingMonents = new int[] { 0, 2, 4, 6 };
            //continuousDerivatives = new int[] { 1, 3, 5 };

            //Modes mode = Modes.BruteForce;
            //int[] orders = new int[] { 1 };
            //divisions = new int[] { 0, 1, 2, 3, 4 };

            //Modes mode = Modes.Standard;
            //int[] orders = Enumerable.Range(1, 20).Select(k => 2 * k - 1).ToArray();

            if (levelSet is IAnalyticLevelSet)
            {
                rootFindingAlgorithm = new AnalyticLevelSetRootFindingAlgorithm();
            }
            else
            {
                rootFindingAlgorithm = new LineSegment.SafeGuardedNewtonMethod(1e-14);
                //rootFindingAlgorithm = new LineSegment.GSLRootFindingAlgorithm(1e-14);
            }

            /*
             * END OF CONFIGURATION
             */


            SubGrid  cutCellGrid     = levelSetTracker.Regions.GetCutCellSubGrid();
            CellMask cellMask        = CellMask.GetFullMask(GridData);
            SubGrid  selectedSubGrid = new SubGrid(cellMask);

            testCase.ScaleShifts(0.5 * testCase.GridSpacing);
            double hBase = ((BoSSS.Foundation.Grid.Classic.GridData)GridData).Cells.h_maxGlobal;

            string logName = ""
                             + testCase.GetType().Name
                             + "_" + Grid.RefElements[0].GetType().Name
                             + "_" + testCase.GridSize
                             + "_" + mode.ToString();

            if (logConsole)
            {
                string filename = logName + "_stdout.txt";
                ilPSP.Environment.StdOut.WriterS.Add(new StreamWriter(filename));
            }

            Console.WriteLine("Test case: " + testCase.GetType().Name);

            var errorMap = new Dictionary <Tuple <int, int, double, int, int>, List <Tuple <double, double, int> > >();

            foreach (int division in divisions)
            {
                foreach (int order in orders)
                {
                    foreach (double width in widths)
                    {
                        foreach (int vanishingMoment in vanishingMonents)
                        {
                            foreach (int continuousDerivative in continuousDerivatives)
                            {
                                errorMap[Tuple.Create(division, order, width, vanishingMoment, continuousDerivative)] =
                                    new List <Tuple <double, double, int> >();
                            }
                        }
                    }
                }
            }

            int i = 1;

            while (testCase.ProceedToNextShift())
            {
                Console.WriteLine("Processing shift " + i + " of " + testCase.NumberOfShifts);

                if (selectedShift > 0 && i != selectedShift)
                {
                    i++;
                    continue;
                }

                cutCellGrid = new SubGrid(
                    levelSetTracker.Regions.GetCutCellSubGrid().VolumeMask.Intersect(
                        selectedSubGrid.VolumeMask));

                double referenceValue = SetUpConfiguration();

                StreamWriter volumeNodesLog  = null;
                StreamWriter surfaceNodesLog = null;
                if (plotSuperSampling >= 0 && i == 1)
                {
                    PlotCurrentState(0.0, 0, plotSuperSampling, cutCellGrid);
                }

                Console.WriteLine();
                foreach (int division in divisions)
                {
                    Console.WriteLine("Number of divisions: " + division);

                    if (logVolumeNodes)
                    {
                        string filename = Path.Combine(
                            Path.GetFullPath("."),
                            "volumeNodes_" + testCase.GetType().Name + "_" + i + "_" + division);
                        volumeNodesLog = new StreamWriter(filename + ".txt");
                        if (GridData.SpatialDimension == 2)
                        {
                            volumeNodesLog.WriteLine("Cell\tNode\tx\ty\tweight");
                        }
                        else
                        {
                            volumeNodesLog.WriteLine("Cell\tNode\tx\ty\tz\tweight");
                        }
                    }

                    if (logSurfaceNodes)
                    {
                        string filename = Path.Combine(
                            Path.GetFullPath("."),
                            "surfaceNodes_" + testCase.GetType().Name + "_" + i + "_" + division);
                        surfaceNodesLog = new StreamWriter(filename + ".txt");
                        if (Grid.SpatialDimension == 2)
                        {
                            surfaceNodesLog.WriteLine("Cell\tNode\tx\ty\tweight");
                        }
                        else
                        {
                            surfaceNodesLog.WriteLine("Cell\tNode\tx\ty\tz\tweight");
                        }
                    }

                    foreach (int order in orders)
                    {
                        Console.WriteLine("Order: " + order);

                        foreach (double width in widths)
                        {
                            foreach (int vanishingMoment in vanishingMonents)
                            {
                                foreach (int continuousDerivative in continuousDerivatives)
                                {
                                    var result = PerformConfiguration(
                                        mode,
                                        order,
                                        division,
                                        volumeNodesLog,
                                        surfaceNodesLog,
                                        leafDivisions,
                                        vanishingMoment,
                                        continuousDerivative,
                                        width,
                                        hBase,
                                        rootFindingAlgorithm,
                                        logVolumeNodes_selectedCell);
                                    double error = Math.Abs(result.Item1 - referenceValue);

                                    var key = Tuple.Create(
                                        division, order, width, vanishingMoment, continuousDerivative);
                                    errorMap[key].Add(Tuple.Create(
                                                          result.Item1 + testCase.Solution - referenceValue,
                                                          error,
                                                          result.Item2));
                                }
                            }
                        }
                    }

                    if (volumeNodesLog != null)
                    {
                        volumeNodesLog.Close();
                    }
                    if (surfaceNodesLog != null)
                    {
                        surfaceNodesLog.Close();
                    }
                }
                Console.WriteLine();

                i++;
            }
            testCase.ResetShifts();

            using (StreamWriter log = new StreamWriter(logName + ".txt")) {
                log.WriteLine("Divisions\tOrder\tWidth\tMoments\tDerivatives\tResult\tMeanError\tMeanRelError\tStdDev\tMinError\tMaxError\tMaxErrorCase\tTime");

                foreach (var entry in errorMap)
                {
                    if (entry.Value.Count == 0)
                    {
                        continue;
                    }

                    IEnumerable <double> errors = entry.Value.Select((tuple) => tuple.Item2);
                    double meanResult           = entry.Value.Select(tuple => tuple.Item1).Average();
                    double meanError            = errors.Average();
                    double stdDev = Math.Sqrt(errors.Select((error) => error * error).Average() - meanError * meanError);
                    double time   = entry.Value.Select((tuple) => tuple.Item3).Average();

                    string line = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}",
                                                entry.Key.Item1, // division
                                                entry.Key.Item2, // order
                                                entry.Key.Item3, // width
                                                entry.Key.Item4, // vanishing moments
                                                entry.Key.Item5, // continuous derivative
                                                meanResult.ToString(formatInfo),
                                                meanError.ToString(formatInfo),
                                                (meanError / testCase.Solution).ToString(formatInfo),
                                                stdDev.ToString(formatInfo),
                                                errors.Min().ToString(formatInfo),
                                                errors.Max().ToString(formatInfo),
                                                errors.IndexOfMax((d) => d) + 1,
                                                time.ToString(formatInfo));


                    log.WriteLine(line);
                }
            }

            globalWatch.Stop();
            Console.WriteLine("Finished case " + testCase.GetType().Name + " after " + globalWatch.ElapsedMilliseconds + "ms");

            return(dt);
        }