Beispiel #1
0
        /// <summary>
        /// ctor
        /// </summary>
        public CudaMatrix(MsrMatrix M, string funcName, CudaEnviroment CudaEnv)
            : base(M)
        {
            m_CudaEnv = CudaEnv;
            base.PackMatrix(M);

            cu.StreamCreate(out intStream, 0);
            cu.StreamCreate(out extStream, 0);
            disposed = false;

            sparseMultiply = CudaEnv.Get_CudaMatrixKernelDP_Function(funcName);
            cuaccext       = CudaEnv.Get_CudaMatrixKernelDP_Function("accumulateExternal");

            //int numreg;
            //cu.FuncGetAttribute(out numreg, CUfunction_attribute.CU_FUNC_ATTRIBUTE_NUM_REGS, sparseMultiply);
            //int version;
            //cu.FuncGetAttribute(out version, CUfunction_attribute.CU_FUNC_ATTRIBUTE_BINARY_VERSION, sparseMultiply);
            //System.Console.WriteLine("Number of registers: " + numreg + ", version: " + version);

            LMAA();

            if (extSize > 0)
            {
                // allocate page-locked mem
                cu.MemHostAlloc(out h_ElementsToAcc, sizeof(double) * (uint)extSize, CUmem_host_alloc.CU_MEMHOSTALLOC_DEVICEMAP);
                //test_ext = new double[totLen];
                cu.MemHostGetDevicePointer(out d_ElementsToAcc, h_ElementsToAcc, 0);

                cu.MemAlloc(out d_IndicesToAccumulate, (uint)extSize * sizeof(int));

                // Copy indices for combining external and internal part to GPU as they don't change over execution
                cu.MemcpyHtoD(d_IndicesToAccumulate, h_IndicesToAccumulate, (uint)extSize * sizeof(int));
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected override MsrMatrix ComputeMatrix()
        {
            MsrMatrix Matrix = new MsrMatrix(m_Convection.OperatorMatrix);

            Matrix.Acc(m_Visc.OperatorMatrix, 1.0);
            return(Matrix);
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected override MsrMatrix ComputeMatrix()
        {
            MsrMatrix Matrix = new MsrMatrix(Convection.OperatorMatrix);

            Matrix.Acc(HeatConduction.OperatorMatrix, 1.0);
            return(Matrix);
        }
Beispiel #4
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="RowMapping"></param>
        /// <param name="ColMapping"></param>
        /// <param name="ParameterFields">
        /// Paramter fields for SpatialOperator - can be null.
        /// </param>
        /// <param name="SolverConf">
        /// Solver configuration.
        /// </param>
        /// <param name="IsConstant">
        /// If true, the operator is constant over time and SIMPLE iterations.
        /// That is the case for most operatores.
        /// Only operators like e.g. the <see cref="LinearizedConvection"/> are not constant.
        /// </param>
        /// <param name="ArgumentIndex">
        /// Argument index of dependent variable, e.g. spatial component u_i, for SpatialOperator
        /// - this parameter is optional.
        /// </param>
        /// <param name="SpatialDirection">
        /// Spatial direction index of independent variable, e.g. diff(p, x_i), for SpatialOperator
        /// - this parameter is optional.
        /// </param>
        /// <param name="OnlyAffine">
        /// If true, only the affine part of the operator is calculated.
        /// </param>
        /// <param name="OnlyBoundaryEdges">
        /// If true, the integration for calculating the affine part is carried out
        /// only on the boundary edges. Can be set to true for most operators.
        /// </param>
        /// <param name="MaxUsePerIterMatrix">
        /// For operators which are not constant the maximum number
        /// the matrix can be called via <see cref="OperatorMatrix"/> in one SIMPLE iteration.
        /// </param>
        /// <param name="MaxUsePerIterAffine">
        /// For operators which are not constant the maximum number
        /// the affine part can be called via <see cref="OperatorAffine"/> in one SIMPLE iteration.
        /// </param>
        protected SIMPLEOperator(UnsetteledCoordinateMapping RowMapping, UnsetteledCoordinateMapping ColMapping, SinglePhaseField[] ParameterFields,
                                 SolverConfiguration SolverConf, bool IsConstant, int ArgumentIndex = -1, int SpatialDirection = -1,
                                 bool OnlyAffine = false, bool OnlyBoundaryEdges = true, int MaxUsePerIterMatrix = 1, int MaxUsePerIterAffine = 1)
        {
            m_RowMapping = RowMapping;
            m_ColMapping = ColMapping;

            m_ParameterFields = ParameterFields;

            m_IsConstant        = IsConstant;
            m_OnlyAffine        = OnlyAffine;
            m_OnlyBoundaryEdges = OnlyBoundaryEdges;

            m_MaxUsePerIterMatrix = MaxUsePerIterMatrix;
            m_MaxUsePerIterAffine = MaxUsePerIterAffine;

            // Get SpatialOperator
            m_SpatialOperator = GetSpatialOperator(SolverConf, ArgumentIndex, SpatialDirection);

            // Initialize and compute matrix of this operator
            if (!m_OnlyAffine)
            {
                m_OperatorMatrix = new MsrMatrix(m_RowMapping, m_ColMapping);
                ComputeMatrix();
            }

            // Initialize and compute affine part of this operator
            m_OperatorAffine = new double[m_RowMapping.LocalLength];
            ComputeAffine();
        }
Beispiel #5
0
        protected override void CreateEquationsAndSolvers(GridUpdateDataVaultBase L)
        {
            using (FuncTrace tr = new FuncTrace()) {
                // assemble system, create matrix
                // ------------------------------

                var volQrSch = new CellQuadratureScheme(true, CellMask.GetFullMask(this.GridData));
                var edgQrSch = new EdgeQuadratureScheme(true, EdgeMask.GetFullMask(this.GridData));

                double D              = this.GridData.SpatialDimension;
                double penalty_base   = (T.Basis.Degree + 1) * (T.Basis.Degree + D) / D;
                double penalty_factor = base.Control.penalty_poisson;

                {
                    // equation assembly
                    // -----------------
                    tr.Info("creating sparse system...");
                    Console.WriteLine("creating sparse system for {0} DOF's ...", T.Mapping.Ntotal);
                    Stopwatch stw = new Stopwatch();
                    stw.Start();

                    SpatialOperator LapaceIp = new SpatialOperator(1, 1, QuadOrderFunc.SumOfMaxDegrees(), "T", "T");
                    var             flux     = new ipFlux(penalty_base * base.Control.penalty_poisson, this.GridData.Cells.cj, base.Control);
                    LapaceIp.EquationComponents["T"].Add(flux);


                    LapaceIp.Commit();

#if DEBUG
                    var RefLaplaceMtx = new MsrMatrix(T.Mapping);
#endif
                    LaplaceMtx    = new BlockMsrMatrix(T.Mapping);
                    LaplaceAffine = new double[T.Mapping.LocalLength];

                    LapaceIp.ComputeMatrixEx(T.Mapping, null, T.Mapping,
                                             LaplaceMtx, LaplaceAffine,
                                             volQuadScheme: volQrSch, edgeQuadScheme: edgQrSch);
#if DEBUG
                    LaplaceAffine.ClearEntries();
                    LapaceIp.ComputeMatrixEx(T.Mapping, null, T.Mapping,
                                             RefLaplaceMtx, LaplaceAffine,
                                             volQuadScheme: volQrSch, edgeQuadScheme: edgQrSch);
                    MsrMatrix ErrMtx = RefLaplaceMtx.CloneAs();
                    ErrMtx.Acc(-1.0, LaplaceMtx);
                    double err    = ErrMtx.InfNorm();
                    double infNrm = LaplaceMtx.InfNorm();
                    Console.WriteLine("Matrix comparison error: " + err + ", matrix norm is: " + infNrm);
                    Assert.Less(err, infNrm * 1e-10, "MsrMatrix2 comparison failed.");
#endif
                    //int q = LaplaceMtx._GetTotalNoOfNonZeros();
                    //tr.Info("finished: Number of non-zeros: " + q);
                    stw.Stop();
                    Console.WriteLine("done {0} sec.", stw.Elapsed.TotalSeconds);


                    //double condNo = LaplaceMtx.condest(BatchmodeConnector.Flavor.Octave);
                    //Console.WriteLine("condition number: {0:0.####E-00} ",condNo);
                }
            }
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            Application.InitMPI();

            MultidimensionalArray myMultidimensionalArray = MultidimensionalArray.Create(3, 6);

            myMultidimensionalArray[1, 2] = 3.0;
            myMultidimensionalArray[2, 5] = 2.0;
            TestShowVisualizer(myMultidimensionalArray);

            BlockDiagonalMatrix myBlockDiagonalMatrix = new BlockDiagonalMatrix(15, 3);

            myBlockDiagonalMatrix[1, 1] = 1.0;
            TestShowVisualizer(myBlockDiagonalMatrix);

            MsrMatrix myMsrMatrix = new MsrMatrix(4, 1);

            myMsrMatrix[1, 1] = 3.0;
            TestShowVisualizer(myMsrMatrix);

            MultidimensionalArray myNodeSet = MultidimensionalArray.Create(3, 2);

            myNodeSet[0, 0] = 1.0;
            myNodeSet[0, 1] = 2.0;
            myNodeSet[1, 0] = 3.0;
            myNodeSet[1, 1] = 4.0;
            myNodeSet[2, 0] = 5.0;
            myNodeSet[2, 1] = 6.0;
            NodeSet nodeSet = new NodeSet(null, myNodeSet);

            TestShowVisualizer(nodeSet);
        }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        public override MatrixBase CreateMatrix(MsrMatrix M, MatrixType mt)
        {
            if (disposed)
            {
                throw new ApplicationException("object is disposed.");
            }

            switch (mt)
            {
            case MatrixType.CCBCSR:
                return(new clCCBCSRMatrix(M, this));

            case MatrixType.CSR:
                return(new clCSRMatrix(M, this));

            case MatrixType.ELLPACK:
                return(new clELLPACKmodMatrix(M, this));

            case MatrixType.Auto:
            case MatrixType.ELLPACKcache:
                return(new clELLPACKcacheMatrix(M, this));

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #8
0
        /// <summary>
        /// A low-Performance method to permute the affine part of some operator according to the GlobalID-permutation;
        /// </summary>
        /// <param name="Affine"></param>
        /// <param name="grd"></param>
        /// <param name="RowMap"></param>
        /// <returns></returns>
        ///  <remarks>
        /// Low-Performance implementation, only for debugging purposes.
        /// </remarks>
        public static IList <double> ResortAffine(this IList <double> Affine, GridData grd, UnsetteledCoordinateMapping RowMap)
        {
            if (RowMap.LocalLength != Affine.Count)
            {
                throw new ArgumentException();
            }

            // Copy values of Affine to diagonal matrix
            MsrMatrix Matrix = new MsrMatrix(RowMap, RowMap);
            int       i0     = RowMap.i0;

            for (int row = 0; row < RowMap.LocalLength; row++)
            {
                Matrix[i0 + row, i0 + row] = Affine[row];
            }

            // Permute matrix
            MsrMatrix MatrixPermuted = Matrix.ResortMatrix(grd, RowMap, RowMap);

            // Copy values from permuted matrix to permuted affine
            IList <double> AffinePermuted = new double[Affine.Count];

            for (int row = 0; row < RowMap.LocalLength; row++)
            {
                AffinePermuted[row] = MatrixPermuted[i0 + row, i0 + row];
            }

            return(AffinePermuted);
        }
Beispiel #9
0
        /// <summary>
        /// Solves the system for <paramref name="Unknowns"/>.
        /// </summary>
        /// <param name="Unknowns">The unknowns to be solved for.</param>
        /// <param name="dt">time step size</param>
        /// <param name="SpatialComponent">Spatial component - this parameter is optional.</param>
        public virtual void Solve(IList <double> Unknowns, double dt, int SpatialComponent = -1)
        {
            using (new FuncTrace()) {
                //Define and set matrix - can be called only once during lifetime of this object.
                if (!MatrixIsDefined)
                {
                    m_SolverMatrix = DefineMatrix(dt);
                    m_Solver.DefineMatrix(m_SolverMatrix);
                    MatrixIsDefined = true;
                }

                //Define and set Rhs
                m_Rhs = DefineRhs(dt, SpatialComponent);


                //Some checks
                if ((m_SolverMatrix.RowPartitioning.LocalLength != Unknowns.Count) || (m_SolverMatrix.RowPartitioning.LocalLength != m_Rhs.Count))
                {
                    throw new ArgumentException("Mismatch of dimensions!");
                }

                //Solve
                SolverResult res = m_Solver.Solve(Unknowns, m_Rhs);

                if (m_solverConf.Control.PrintLinerSolverResults && (m_solverConf.MPIRank == 0))
                {
                    Console.WriteLine("Linear solver results:");
                    Console.WriteLine("Converged: {0}, NoOfIterations: {1}, Runtime: {2} sec", res.Converged.ToString(), res.NoOfIterations, res.RunTime.TotalSeconds);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected override MsrMatrix ComputeMatrix()
        {
            MsrMatrix CorrectorMatrix = new MsrMatrix(m_IPOperator.OperatorMatrix);

            switch (m_SolverConf.Control.Algorithm)
            {
            case SolutionAlgorithms.Steady_SIMPLE:
                break;

            case SolutionAlgorithms.Unsteady_SIMPLE:
                // gamma * dt / (beta_0 + gamma * dt)
                double UnsteadyFactor = m_BDF.gamma[m_SolverConf.BDFOrder - 1] * m_SolverConf.dt / (m_BDF.beta[m_SolverConf.BDFOrder - 1][0] + m_BDF.gamma[m_SolverConf.BDFOrder - 1] * m_SolverConf.dt);
                CorrectorMatrix.Scale(UnsteadyFactor);
                break;

            default:
                throw new NotImplementedException();
            }

            if (m_PressureStabilization != null)
            {
                CorrectorMatrix.Acc(-1.0, m_PressureStabilization.OperatorMatrix);
            }

            CorrectorMatrix.AssumeSymmetric = true;
            return(CorrectorMatrix);
        }
Beispiel #11
0
        /// <summary>
        /// ctor.
        /// </summary>
        public LinearSolver(ISparseSolver solver, MsrMatrix spatialOpMtx, IList <double> spatialOpAffine, CoordinateMapping UnknownsMap)
        {
            // check operator and arguments
            // ----------------------------

            if (spatialOpMtx.NoOfRows != spatialOpMtx.NoOfCols)
            {
                throw new ArgumentException("matrix must be quadratic.", "spatialOpMtx");
            }
            if (spatialOpMtx.NoOfRows != UnknownsMap.GlobalCount)
            {
                throw new ArgumentException("matrix size must be equal to the GlobalCount of fields mapping", "fields,spatialOpMtx");
            }
            if (spatialOpMtx.RowPartitioning.LocalLength != UnknownsMap.LocalLength)
            {
                throw new ArgumentException("number of locally stored matrix rows nust be equal to NUpdate of fields mapping.", "fields,spatialOpMtx");
            }
            if (spatialOpAffine.Count < UnknownsMap.LocalLength)
            {
                throw new ArgumentException("length affine offset vector must be equal or larger than NUpdate of the mapping", "spatialOpAffine");
            }

            m_Solver  = solver;
            m_Mapping = UnknownsMap;


            // finish constructor
            // ------------------
            m_AffineOffset = spatialOpAffine.ToArray();
            ConstructorCommon(spatialOpMtx);
        }
Beispiel #12
0
        /// <summary>
        /// Method that is called by the base class constructor. In this implicit euler scheme, it simply
        /// adds the diagonal entries to the operator matrix.
        /// Currently only working for fields that should be included in the time stepping.
        /// </summary>
        /// <param name="OperatorMatrix">The operator matrix that should be adjusted and passed onto the solver</param>
        /// <param name="dt">time step size</param>
        protected override void DefineMatrix(MsrMatrix OperatorMatrix, double dt)
        {
            int[]         cells            = m_Subgrid.SubgridIndex2LocalCellIndex;
            IList <Field> fields           = m_SubgridMapping.Fields;
            int           CoordinateOffset = 0;

            for (int g = 0; g < temporalOp.Length; g++)
            {
                if (temporalOp[g])
                {
                    Field gamma = fields[g];

                    for (int j = 0; j < cells.Length; j++)
                    {
                        int iglob = j * m_SubgridMapping.MaxTotalNoOfCoordinatesPerCell + CoordinateOffset + (int)OperatorMatrix.RowPartition.i0;
                        for (int n = 0; n < gamma.Basis.MaximalLength; n++)
                        {
                            double vadd = 1.0 / dt;
                            double v    = OperatorMatrix.GetDiagElement(iglob);
                            v += vadd;
                            OperatorMatrix.SetDiagElement(iglob, v);
                            iglob++;
                        }
                    }
                }
                CoordinateOffset += fields[g].Basis.MaximalLength;
            }
            m_Solver.DefineMatrix(OperatorMatrix);
            OperatorMatrix.SaveToTextFileSparse("Matrix.txt");
        }
Beispiel #13
0
        /// <summary>
        /// Create matrix
        /// </summary>
        /// <param name="M">Original matrix</param>
        /// <param name="device">Corresponding OpenCL device</param>
        /// <param name="kernelName">Name of the kernel function</param>
        public clMatrix(MsrMatrix M, clDevice device, string kernelName)
            : base(M)
        {
            this.device = device;
            base.PackMatrix(M);
            this.clmultiply = cl.CreateKernel(device.matrixProgram, kernelName);
            this.claccext   = cl.CreateKernel(device.matrixProgram, "accumulateExternal");
            disposed        = false;

            LMAA();

            if (extSize > 0)
            {
                extglobalsize = extSize;
                int m = extSize % extlocalsize;
                if (m > 0)
                {
                    extglobalsize += extlocalsize - m;
                }

                h_ElementsToAcc = Marshal.AllocHGlobal(extSize * sizeof(double));
                d_ElementsToAcc = cl.CreateBuffer(device.env.context, cl_mem_flags.CL_MEM_READ_ONLY, (uint)extSize * sizeof(double));

                d_IndicesToAccumulate = cl.CreateBuffer(device.env.context, cl_mem_flags.CL_MEM_READ_ONLY, (uint)extSize * sizeof(int));
                cl.EnqueueWriteBuffer(device.cq, d_IndicesToAccumulate, true, 0, (uint)extSize * sizeof(int), h_IndicesToAccumulate);
            }
        }
Beispiel #14
0
        protected override MsrMatrix ComputeMatrix()
        {
            MsrMatrix Approx    = m_Approx.AssemblyMatrix;
            MsrMatrix ApproxInv = new MsrMatrix(Approx.RowPartitioning, Approx.ColPartition);

            switch (m_SolverConf.PredictorApproximation)
            {
            case PredictorApproximations.Identity:
            case PredictorApproximations.Identity_IP1:
            case PredictorApproximations.Diagonal:
                int i0          = Approx.RowPartitioning.i0;
                int LocalLength = Approx.RowPartitioning.LocalLength;

                for (int row = 0; row < LocalLength; row++)
                {
                    double Approx_ii = Approx[row + i0, row + i0];
                    ApproxInv[row + i0, row + i0] = 1.0 / Approx_ii;
                }
                break;

            case PredictorApproximations.BlockDiagonal:
                BlockDiagonalMatrix ApproxBlock    = new BlockDiagonalMatrix(Approx, Approx.RowPartitioning.LocalLength / m_LocalNoOfCells, Approx.ColPartition.LocalLength / m_LocalNoOfCells);
                BlockDiagonalMatrix ApproxBlockInv = ApproxBlock.Invert();
                ApproxInv.Acc(1.0, ApproxBlockInv);
                break;

            default:
                throw new ArgumentException();
            }

            return(ApproxInv);
        }
Beispiel #15
0
        public ExtVelSolver_PDEbased(Basis ExtPropertyBasis)
        {
            var map = new UnsetteledCoordinateMapping(ExtPropertyBasis);

            m_ExtvelMatrix = new MsrMatrix(map, map);
            m_ExtvelAffine = new double[m_ExtvelMatrix.RowPartitioning.LocalLength];
        }
Beispiel #16
0
        static void Main(string[] args)
        {
            bool dummy;

            ilPSP.Environment.Bootstrap(args, Application.GetBoSSSInstallDir(), out dummy);

            MultidimensionalArray myMultidimensionalArray = MultidimensionalArray.Create(3, 6);

            myMultidimensionalArray[1, 2] = 3.0;
            myMultidimensionalArray[2, 5] = 2.0;
            TestShowVisualizer(myMultidimensionalArray);

            BlockDiagonalMatrix myBlockDiagonalMatrix = new BlockDiagonalMatrix(15, 3);

            myBlockDiagonalMatrix[1, 1] = 1.0;
            TestShowVisualizer(myBlockDiagonalMatrix);

            MsrMatrix myMsrMatrix = new MsrMatrix(4, 1);

            myMsrMatrix[1, 1] = 3.0;
            TestShowVisualizer(myMsrMatrix);

            MultidimensionalArray myNodeSet = MultidimensionalArray.Create(3, 2);

            myNodeSet[0, 0] = 1.0;
            myNodeSet[0, 1] = 2.0;
            myNodeSet[1, 0] = 3.0;
            myNodeSet[1, 1] = 4.0;
            myNodeSet[2, 0] = 5.0;
            myNodeSet[2, 1] = 6.0;
            NodeSet nodeSet = new NodeSet(null, myNodeSet);

            TestShowVisualizer(nodeSet);
        }
Beispiel #17
0
        public void Init(MultigridOperator op)
        {
            int D = op.GridData.SpatialDimension;

            CodName = new string[] { "momX", "momY", "div", "constitutiveXX", "constitutiveXY", "constitutiveYY" };
            Params  = new string[] { "VelocityX_GradientX", "VelocityX_GradientY", "VelocityY_GradientX", "VelocityY_GradientY" };
            DomName = new string[] { "VelocityX", "VelocityY", "Pressure", "StressXX", "StressXY", "StressYY" };

            LocalOp = new SpatialOperator(DomName, Params, CodName, (A, B, C) => 4);

            LocalOp.EquationComponents["constitutiveXX"].Add(new LocalJacobiFlux()
            {
                m_component = 0, We = m_We
            });
            LocalOp.EquationComponents["constitutiveXY"].Add(new LocalJacobiFlux()
            {
                m_component = 1, We = m_We
            });
            LocalOp.EquationComponents["constitutiveYY"].Add(new LocalJacobiFlux()
            {
                m_component = 2, We = m_We
            });

            LocalOp.Commit();

            var   U0      = ((BoSSS.Foundation.CoordinateMapping)op.Mapping.ProblemMapping).Fields.GetSubVector(0, 2);
            Basis U0Basis = new Basis(op.Mapping.GridData, 0);

            VectorField <SinglePhaseField> VelocityXGradient = new VectorField <SinglePhaseField>(D, U0Basis, "VelocityX_Gradient", SinglePhaseField.Factory);
            VectorField <SinglePhaseField> VelocityYGradient = new VectorField <SinglePhaseField>(D, U0Basis, "VelocityY_Gradient", SinglePhaseField.Factory);

            VelocityXGradient.Clear();
            VelocityXGradient.GradientByFlux(1.0, U0[0]);
            VelocityYGradient.Clear();
            VelocityYGradient.GradientByFlux(1.0, U0[1]);

            var Parameters = ArrayTools.Cat <DGField>(VelocityXGradient, VelocityYGradient);

            LocalMatrix = LocalOp.ComputeMatrix(op.BaseGridProblemMapping, Parameters, op.BaseGridProblemMapping);

            ConstEqIdx = op.Mapping.ProblemMapping.GetSubvectorIndices(true, 3, 4, 5);

            P = (BlockMsrMatrix)op.MassMatrix.Clone();

            for (int i = ConstEqIdx[0]; i <= ConstEqIdx.Length; i++)
            {
                for (int j = ConstEqIdx[0]; j <= ConstEqIdx.Length; j++)
                {
                    if (LocalMatrix[i, j] != 0)
                    {
                        P[i, j] = LocalMatrix[i, j];
                    }
                }
            }

            LocalMatrix.SaveToTextFileSparse("LocalMatrix");
            op.MassMatrix.SaveToTextFileSparse("MassMatrix");
            P.SaveToTextFileSparse("PrecondMatrix");
        }
Beispiel #18
0
        protected override double RunSolverOneStep(int TimestepNo, double phystime, double dt)
        {
            //phystime = 1.8;
            LsUpdate(phystime);

            // operator-matrix assemblieren
            OperatorMatrix    = new BlockMsrMatrix(ProblemMapping);
            AltOperatorMatrix = new MsrMatrix(ProblemMapping);
            double[] Affine = new double[OperatorMatrix.RowPartitioning.LocalLength];
            MultiphaseCellAgglomerator Agg;

            // Agglomerator setup
            //Agg = new MultiphaseCellAgglomerator(new CutCellMetrics(MomentFittingVariant, m_quadOrder, LsTrk, LsTrk.GetSpeciesId("B")), this.THRESHOLD, false);
            Agg = LsTrk.GetAgglomerator(new SpeciesId[] { LsTrk.GetSpeciesId("B") }, m_quadOrder, __AgglomerationTreshold: this.THRESHOLD);
            Console.WriteLine("Inter-Process agglomeration? " + Agg.GetAgglomerator(LsTrk.GetSpeciesId("B")).AggInfo.InterProcessAgglomeration);

            // operator matrix assembly
            //Op.ComputeMatrixEx(LsTrk,
            //    ProblemMapping, null, ProblemMapping,
            //    OperatorMatrix, Affine, false, 0.0, true,
            //    Agg.CellLengthScales, null, null,
            //    LsTrk.SpeciesIdS.ToArray());
            XSpatialOperatorMk2.XEvaluatorLinear mtxBuilder = Op.GetMatrixBuilder(base.LsTrk, ProblemMapping, null, ProblemMapping, LsTrk.SpeciesIdS.ToArray());
            mtxBuilder.time = 0.0;
            mtxBuilder.ComputeMatrix(OperatorMatrix, Affine);
            Agg.ManipulateMatrixAndRHS(OperatorMatrix, Affine, this.ProblemMapping, this.ProblemMapping);

            //Op.ComputeMatrixEx(LsTrk,
            //    ProblemMapping, null, ProblemMapping,
            //    AltOperatorMatrix, Affine, false, 0.0, true,
            //    Agg.CellLengthScales, null, null,
            //    LsTrk.SpeciesIdS.ToArray());
            mtxBuilder.ComputeMatrix(AltOperatorMatrix, Affine);
            Agg.ManipulateMatrixAndRHS(AltOperatorMatrix, Affine, this.ProblemMapping, this.ProblemMapping);


            int nnz = this.OperatorMatrix.GetTotalNoOfNonZeros();

            Console.WriteLine("Number of non-zeros in matrix: " + nnz);

            int nnz2 = this.AltOperatorMatrix.GetTotalNoOfNonZeros();

            Assert.IsTrue(nnz == nnz2, "Number of non-zeros in matrix different for " + OperatorMatrix.GetType() + " and " + AltOperatorMatrix.GetType());
            Console.WriteLine("Number of non-zeros in matrix (reference): " + nnz2);

            MsrMatrix Comp = AltOperatorMatrix.CloneAs();

            Comp.Acc(-1.0, OperatorMatrix);
            double CompErr    = Comp.InfNorm();
            double Denom      = Math.Max(AltOperatorMatrix.InfNorm(), OperatorMatrix.InfNorm());
            double CompErrRel = Denom > Math.Sqrt(double.Epsilon) ? CompErr / Denom : CompErr;

            Console.WriteLine("Comparison: " + CompErrRel);

            Assert.LessOrEqual(CompErrRel, 1.0e-7, "Huge difference between MsrMatrix and BlockMsrMatrix.");

            base.TerminationKey = true;
            return(0.0);
        }
Beispiel #19
0
 public void DefineMatrix(IMutableMatrixEx M)
 {
     if (M.RowPartitioning.TotalLength != M.ColPartition.TotalLength)
     {
         throw new ArgumentException("Matrix must be symmetric.");
     }
     this.Matrix = M.ToMsrMatrix();
 }
Beispiel #20
0
        protected override MsrMatrix ComputeMatrix()
        {
            MsrMatrix Matrix = new MsrMatrix(Convection[0].OperatorMatrix);

            Matrix.Acc(ViscTerm1[0].OperatorMatrix, 1.0);
            Matrix.Acc(ViscTerm2Term3[Component, Component].AssemblyMatrix, 1.0);
            return(Matrix);
        }
        /// <summary>
        /// method for setting up the timestepper, i.e. the necessary
        /// </summary>
        protected void Setup1(Context ctx, ISparseSolver solver, bool[] temporalOp, MsrMatrix spatialOpMtx, IList <double> spatialOpAffine,
                              SubGrid subgrid, SubgridCoordinateMapping fields, double InitialDeltat)
        {
            // check operator and arguments
            if (spatialOpMtx.NoOfRows != spatialOpMtx.NoOfCols)
            {
                throw new ArgumentException("matrix must be quadratic.", "spatialOpMtx");
            }
            if (spatialOpMtx.NoOfRows != fields.GlobalCount)
            {
                throw new ArgumentException("matrix size must be equal to the GlobalCount of fields mapping", "fields,spatialOpMtx");
            }
            if (spatialOpMtx.RowPartition.LocalLength != fields.NUpdate)
            {
                throw new ArgumentException("number of locally stored matrix rows nust be equal to NUpdate of fields mapping.", "fields,spatialOpMtx");
            }
            if (spatialOpAffine.Count < fields.NUpdate)
            {
                throw new ArgumentException("length affine offset vector must be equal or larger than NUpdate of the mapping", "spatialOpAffine");
            }

            m_Context        = ctx;
            m_Solver         = solver;
            m_Subgrid        = subgrid;
            m_SubgridMapping = fields;
            m_AffineOffset1  = spatialOpAffine.ToArray();
            this.temporalOp  = temporalOp;
            BLAS.dscal(m_AffineOffset1.Length, -1.0, m_AffineOffset1, 1);
            {
                // check temporal operator
                // -----------------------
                if (m_SubgridMapping.Fields.Count != temporalOp.Length)
                {
                    throw new ArgumentException(
                              "lenght of temporalOp must be equal to number of domain/codomain variables of the spatial differential operator",
                              "temporalOp");
                }
                m_SubgridMapping.Compress();
                m_SubgridDGCoordinates = m_SubgridMapping.subgridCoordinates;

                m_SubgridMapping.SetupSubmatrix(m_AffineOffset1, spatialOpMtx, out m_CompressedAffine, out m_CompressedMatrix);

                bool timedep      = false;
                bool fullyTimeDep = true;
                foreach (bool b in temporalOp)
                {
                    timedep      = timedep || b;
                    fullyTimeDep = fullyTimeDep & b;
                }
                if (!timedep)
                {
                    throw new ArgumentException("At least one equation must be time-dependent; one entry in temporalOp must be true;", "temporalOp");
                }

                DefineMatrix(m_CompressedMatrix, InitialDeltat);
            }
        }
Beispiel #22
0
        public static MsrMatrix ConvertToMsr(this MultidimensionalArray M)
        {
            Partitioning rowpart = new Partitioning(M.Lengths[0], MPI.Wrappers.csMPI.Raw._COMM.SELF);
            Partitioning colpart = new Partitioning(M.Lengths[1], MPI.Wrappers.csMPI.Raw._COMM.SELF);
            var          bla     = new MsrMatrix(rowpart, colpart);

            bla.AccBlock(0, 0, 1.0, M);
            return(bla);
        }
Beispiel #23
0
        /// <summary>
        /// computes <see cref="LaplaceMtx"/> and <see cref="LaplaceAffine"/>
        /// </summary>
        private void UpdateMatrices() {
            using (var tr = new FuncTrace()) {
                // time measurement for matrix assembly
                Stopwatch stw = new Stopwatch();
                stw.Start();

                // console
                Console.WriteLine("creating sparse system for {0} DOF's ...", T.Mapping.Ntotal);

                // quadrature domain
                var volQrSch = new CellQuadratureScheme(true, CellMask.GetFullMask(this.GridData, MaskType.Geometrical));
                var edgQrSch = new EdgeQuadratureScheme(true, EdgeMask.GetFullMask(this.GridData, MaskType.Geometrical));

#if DEBUG
                // in DEBUG mode, we compare 'MsrMatrix' (old, reference implementation) and 'BlockMsrMatrix' (new standard)
                var RefLaplaceMtx = new MsrMatrix(T.Mapping);
#endif
                using (new BlockTrace("SipMatrixAssembly", tr)) {
                    LaplaceMtx = new BlockMsrMatrix(T.Mapping);
                    LaplaceAffine = new double[T.Mapping.LocalLength];

                    LapaceIp.ComputeMatrixEx(T.Mapping, null, T.Mapping,
                                             LaplaceMtx, LaplaceAffine,
                                             volQuadScheme: volQrSch, edgeQuadScheme: edgQrSch);
                }
#if DEBUG
                LaplaceAffine.ClearEntries();
                LapaceIp.ComputeMatrixEx(T.Mapping, null, T.Mapping,
                                         RefLaplaceMtx, LaplaceAffine,
                                         volQuadScheme: volQrSch, edgeQuadScheme: edgQrSch);
                MsrMatrix ErrMtx = RefLaplaceMtx.CloneAs();
                ErrMtx.Acc(-1.0, LaplaceMtx);
                double err = ErrMtx.InfNorm();
                double infNrm = LaplaceMtx.InfNorm();
                Console.WriteLine("Matrix comparison error: " + err + ", matrix norm is: " + infNrm);
                Assert.Less(err, infNrm * 1e-10, "MsrMatrix2 comparison failed.");
#endif
                stw.Stop();
                Console.WriteLine("done {0} sec.", stw.Elapsed.TotalSeconds);


                //var JB = LapaceIp.GetFDJacobianBuilder(T.Mapping.Fields, null, T.Mapping, edgQrSch, volQrSch);
                //var JacobiMtx = new BlockMsrMatrix(T.Mapping);
                //var JacobiAffine = new double[T.Mapping.LocalLength];
                //JB.ComputeMatrix(JacobiMtx, JacobiAffine);
                //double L2ErrAffine = GenericBlas.L2Dist(JacobiAffine, LaplaceAffine);
                //var ErrMtx2 = LaplaceMtx.CloneAs();
                //ErrMtx2.Acc(-1.0, JacobiMtx);
                //double LinfErrMtx2 = ErrMtx2.InfNorm();

                //JacobiMtx.SaveToTextFileSparse("D:\\tmp\\Jac.txt");
                //LaplaceMtx.SaveToTextFileSparse("D:\\tmp\\Lap.txt");

                //Console.WriteLine("FD Jacobi Mtx: {0:e14}, Affine: {1:e14}", LinfErrMtx2, L2ErrAffine);
            }
        }
Beispiel #24
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="M">Sparse matrix in MSR format</param>
 /// <param name="CudaEnv"></param>
 public CudaELLPACKmodMatrix(MsrMatrix M, CudaEnviroment CudaEnv)
     : base(M, "ellMultiply", CudaEnv)
 {
     m_internalData = (ELLPACKmod)m_LocalMtx;
     size           = m_internalData.NoOfRows;
     colCount       = m_internalData.NoOfPackedCols;
     valStride      = m_internalData.MtxEntries.ColStride;
     colStride      = m_internalData.ColInd.ColStride;
     blockcount     = (int)Math.Ceiling((Decimal)size / blocksize);
 }
Beispiel #25
0
        /// <summary>
        /// Calculate Extension
        /// </summary>
        public void ConstructExtension(VectorField <SinglePhaseField> NewExtension, VectorField <SinglePhaseField> InterfaceValue, ref SubGrid subGrid)
        {
            using (new FuncTrace()) {
                NewExtension.Clear();
                ComputeMatrices(new List <DGField> {
                    InterfaceValue[0]
                }, Control.subGridRestriction);
                ISparseSolver slv = Control.solverFactory();

                int[]     SubVecIdx = null;
                int       L;
                MsrMatrix SubMatrix;
                double[]  SubRHS      = null;
                double[]  SubSolution = null;

                if (subGrid != null)
                {
                    SubVecIdx   = Extension.Mapping.GetSubvectorIndices(subGrid, true, new int[] { 0 });
                    L           = SubVecIdx.Length;
                    SubMatrix   = new MsrMatrix(L);
                    SubRHS      = new double[L];
                    SubSolution = new double[L];

                    OpMatrix.AccSubMatrixTo(1.0, SubMatrix, SubVecIdx, default(int[]), SubVecIdx, default(int[]));
                    slv.DefineMatrix(SubMatrix);
                }
                else
                {
                    slv.DefineMatrix(OpMatrix);
                }

                for (int d = 0; d < D; d++)
                {
                    UpdateRHS(NewExtension[d], InterfaceValue[d], Control.subGridRestriction);
                    double[] RHS = OpAffine.CloneAs();
                    RHS.ScaleV(-1.0);
                    if (subGrid != null)
                    {
                        SubRHS.Clear();
                        SubSolution.Clear();
                        SubRHS.AccV(1.0, RHS, default(int[]), SubVecIdx);
                        SubSolution.AccV(1.0, NewExtension[d].CoordinateVector, default(int[]), SubVecIdx);
                        slv.Solve(SubSolution, SubRHS);

                        NewExtension[d].Clear(subGrid.VolumeMask);
                        NewExtension[d].CoordinateVector.AccV(1.0, SubSolution, SubVecIdx, default(int[]));
                    }
                    else
                    {
                        slv.Solve(NewExtension[d].CoordinateVector, RHS);
                    }
                }
                slv.Dispose();
            }
        }
Beispiel #26
0
        protected override MsrMatrix ComputeMatrix()
        {
            MsrMatrix Src    = m_Src.AssemblyMatrix;
            MsrMatrix Approx = new MsrMatrix(Src.RowPartitioning, Src.ColPartition);

            int i0          = Src.RowPartitioning.i0;
            int LocalLength = Src.RowPartitioning.LocalLength;

            double BDFfactor;

            switch (m_SolverConf.Control.Algorithm)
            {
            case SolutionAlgorithms.Steady_SIMPLE:
                BDFfactor = 0.0;
                break;

            case SolutionAlgorithms.Unsteady_SIMPLE:
                int    BDFOrder = m_SolverConf.BDFOrder;
                double dt       = m_SolverConf.dt;
                BDFfactor = m_BDF.beta[BDFOrder - 1][0] / (m_BDF.gamma[BDFOrder - 1] * dt);
                break;

            default:
                throw new ArgumentException();
            }

            switch (m_SolverConf.Control.PredictorApproximation)
            {
            case PredictorApproximations.Identity:
            case PredictorApproximations.Identity_IP1:
                Approx.AccEyeSp();
                Approx.AccEyeSp(BDFfactor);
                break;

            case PredictorApproximations.Diagonal:
                for (int row = 0; row < LocalLength; row++)
                {
                    double Src_ii = Src[row + i0, row + i0];
                    double Rho_ii = m_Rho[row + i0, row + i0];
                    Approx[row + i0, row + i0] = BDFfactor * Rho_ii + Src_ii;
                }
                break;

            case PredictorApproximations.BlockDiagonal:
                BlockDiagonalMatrix SrcBlock = new BlockDiagonalMatrix(Src, Src.RowPartitioning.LocalLength / m_NoOfCells, Src.ColPartition.LocalLength / m_NoOfCells);
                Approx.Acc(1.0, SrcBlock);
                Approx.Acc(BDFfactor, m_Rho);
                break;

            default:
                throw new ArgumentException();
            }

            return(Approx);
        }
        protected override MsrMatrix DefineMatrix(double dt)
        {
            MsrMatrix res = MatAsmblyCorrector.AssemblyMatrix;

            if (base.m_solverConf.Control.PressureReferencePoint != null)
            {
                BoSSS.Solution.NSECommon.SolverUtils.SetRefPtPressure_Matrix(res, base.m_solverConf.PressureReferencePointIndex);
            }

            return(res);
        }
Beispiel #28
0
        /// <summary>
        /// Test if the matrix is symmetric positive definite
        /// </summary>
        /// <returns>bool array res=[symmetry, positive definit]</returns>
        public bool[] Symmetry()
        {
            bool[] res = new bool[2];

            //extract submatrix for selected dependent variables
            int[]    DepVars        = this.VarGroup;
            double[] DepVars_subvec = this.m_map.GetSubvectorIndices(true, DepVars).Select(i => i + 1.0).ToArray();

            //MsrMatrix OpMtxMSR = m_OpMtx.ToMsrMatrix();

            int[] SubMatrixIdx_Row  = m_map.GetSubvectorIndices(false, DepVars);
            int[] SubMatrixIdx_Cols = m_map.GetSubvectorIndices(false, DepVars);
            int   L = SubMatrixIdx_Row.Length;

            MsrMatrix SubOpMtx = new MsrMatrix(L, L, 1, 1);

            m_OpMtx.WriteSubMatrixTo(SubOpMtx, SubMatrixIdx_Row, default(int[]), SubMatrixIdx_Cols, default(int[]));


            // symmetry test by calculation of symmetry deviation
            bool   sym     = false;
            double SymmDev = m_OpMtx.SymmetryDeviation();

            if (SymmDev < 1e-5)
            {
                sym = true;
            }

            res[0] = sym;


            // positive definite test by Cholesky decomposition
            var FullyPopulatedMatrix = m_OpMtx.ToFullMatrixOnProc0();

            bool posDef = true;

            // only proc 0 gets info so the following is executed exclusively on rank 0
            if (ilPSP.Environment.MPIEnv.MPI_Rank == 0)
            {
                try
                {
                    FullyPopulatedMatrix.Cholesky();
                }
                catch (ArithmeticException)
                {
                    posDef = false;
                }
            }
            res[1] = MPIEnviroment.Broadcast <bool>(posDef, 0, ilPSP.Environment.MPIEnv.Mpi_comm);

            Debug.Assert(res[0].MPIEquals(), "value does not match on procs");
            Debug.Assert(res[1].MPIEquals(), "value does not match on procs");
            return(res);
        }
Beispiel #29
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="M">Sparse matrix in MSR format</param>
 /// <param name="CudaEnv"></param>
 public CudaELLPACKcacheMatrix(MsrMatrix M, CudaEnviroment CudaEnv)
     : base(M, "mcellMultiply", CudaEnv)
 {
     using (new FuncTrace()) {
         m_internalData = (ManualCacheELLPACK)m_LocalMtx;
         size           = m_internalData.NoOfRows;
         colCount       = m_internalData.NoOfPackedCols;
         valStride      = m_internalData.MtxEntries.ColStride;
         colStride      = m_internalData.ColIndBlock.ColStride;
         blockcount     = (int)Math.Ceiling((Decimal)size / blocksize);
     }
 }
Beispiel #30
0
        public clCSRMatrix(MsrMatrix M, clDevice device)
            : base(M, device, "csrMultiply")
        {
            size       = base.RowPartitioning.LocalLength;
            localsize  = 256;
            globalsize = size;
            int m = size % localsize;

            if (m > 0)
            {
                globalsize += localsize - m;
            }
        }