Beispiel #1
0
        /*public Vector<double> IntegrateStep(DAESemiImplicitSystem system, Vector<double> x, Vector<double> z, double t)
         * {
         *  int sizeX = system.SizeX * Stages;
         *  int sizeZ = system.SizeZ * Stages;
         *  int size = sizeX + sizeZ;
         *  Vector<double>[] kx = new Vector<double>[Stages];// vector k=[k1_1,k1_2,...k1_n,...,kn_1,...,kn_n]
         *  Vector<double>[] kz = new Vector<double>[Stages];// vector k=[k1_1,k1_2,...k1_n,...,kn_1,...,kn_n]
         *
         *  for (int i = 0; i < Stages; i++)
         *  {
         *      kx[i] = Vector<double>.Build.Dense(system.SizeX);
         *      kz[i] = Vector<double>.Build.Dense(system.SizeZ);
         *  }
         *
         *  for (int i = 0; i < newtonIterations; i++)
         *  {
         *      Matrix<double> jacobiMatrix = Matrix<double>.Build.Dense(size, size);
         *      Vector<double> f = Vector<double>.Build.Dense(size);
         *      for (int j = 0; j < Stages; j++)
         *      {
         *          Vector<double> t_x = x;
         *          Vector<double> t_z = z;
         *          for (int m = 0; m < Stages; m++)
         *          {
         *              t_x += step * a[j, m] * kx[m];
         *              t_z += step * a[j, m] * kz[m];
         *          }
         *          double time = t + step * c[j];
         *          Matrix<double> dFdX = system.dFdX(t_x, t_z, time); //use k as dx
         *          Matrix<double> dFdZ = system.dFdZ(t_x, t_z, time); //use k as dx
         *          Matrix<double> dGdX = system.dGdX(t_x, t_z, time);
         *          Matrix<double> dGdZ = system.dGdZ(t_x, t_z, time);
         *          double[] F = system.F(t_x, t_z, time);
         *          double[] G = system.G(t_x, t_z, time);
         *          for (int m = 0; m < system.SizeX; m++)
         *          {
         *              int row = j * system.SizeX + m;
         *              f[row] = F[m];
         *              for (int p = 0; p < Stages; p++)
         *              {
         *                  for (int l = 0; l < system.SizeX; l++)
         *                  {
         *                      int column = l + p * system.Size;
         *                      //jacobiMatrix[row, column] = dFdX[m, l] * Step * a[j, p] + dFddX[m, l];
         *                  }
         *                  for (int l = 0; l < system.SizeZ; l++)
         *                  {
         *                      int column = l + p * system.Size;
         *                      //jacobiMatrix[row, column] = dFdX[m, l] * Step * a[j, p] + dFddX[m, l];
         *                  }
         *              }
         *          }
         *          for (int m = 0; m < system.SizeZ; m++)
         *          {
         *              int row=j*system.SizeZ + sizeX + m;
         *              f[row] = G[m];
         *
         *              for (int p = 0; p < Stages; p++)
         *              {
         *                  for (int l = 0; l < system.SizeX; l++)
         *                  {
         *                      int column = l + p * system.Size;
         *                      //jacobiMatrix[row, column] = dFdX[m, l] * Step * a[j, p] + dFddX[m, l];
         *                  }
         *                  for (int l = 0; l < system.SizeZ; l++)
         *                  {
         *                      int column = l + p * system.Size;
         *                      //jacobiMatrix[row, column] = dFdX[m, l] * Step * a[j, p] + dFddX[m, l];
         *                  }
         *              }
         *          }
         *      }
         *      Vector<double> dk = jacobiMatrix.Solve(-f);
         *      for (int j = 0; j < Stages; j++)
         *      {
         *          kx[j] += dk.SubVector(j * system.Size, system.Size);
         *          kz[j] += dk.SubVector(j * system.Size, system.Size);
         *      }
         *  }
         *
         *  for (int i = 0; i < Stages; i++)
         *  {
         *      x += kx[i] * step * b[i];
         *      x += kz[i] * step * b[i];
         *  }
         *  return x;
         *
         *
         *  throw new NotImplementedException();
         * }*/
        // system is f(x,x',t)=0
        // x(n+1)=x(n)+sum k_i*b_i
        // k_i = x'(x_n + h*sum k_j * a_ij, t_n + h*c_i) where i is [1,Stages] and j is [1,Stages]
        // k_i is a vector and sizeOf(k_i) == sizeOf(x)
        // or f(x_n + h*sum k_j * a_ij, k_i, t_n + h * c_i)
        // solve this implicit nonlinear system for all k's
        public override Vector <double> IntegrateStep(DAEISystem system, Vector <double> x, double t)
        {
            int size = system.Size * Stages;

            Vector <double> [] k = new Vector <double> [Stages];// vector k=[k1_1,k1_2,...k1_n,...,kn_1,...,kn_n]

            for (int i = 0; i < Stages; i++)
            {
                k[i] = Vector <double> .Build.Dense(system.Size);
            }
            for (int i = 0; i < newtonIterations; i++)
            {
                Matrix <double> jacobiMatrix = Matrix <double> .Build.Dense(size, size);

                Vector <double> f = Vector <double> .Build.Dense(size);

                for (int j = 0; j < Stages; j++)
                {
                    Vector <double> t_x = x;
                    //[row,column]
                    for (int m = 0; m < Stages; m++)
                    {
                        t_x += step * a[j, m] * k[m];
                    }
                    double          time  = t + step * c[j];
                    Matrix <double> dFdX  = system.DFdX(t_x, k[j], time); //use k as dx
                    Matrix <double> dFddX = system.DFddX(t_x, k[j], time);
                    Vector <double> F     = system.F(t_x, k[j], time);
                    //subMatrix Size*Size
                    for (int m = 0; m < system.Size; m++)
                    {
                        int row = m + j * system.Size;
                        f[row] = F[m] * alpha;
                        for (int p = 0; p < Stages; p++)
                        {
                            for (int l = 0; l < system.Size; l++)
                            {
                                int    column = l + p * system.Size;
                                double dfddx  = p == j ? dFddX[m, l]:0.0;
                                jacobiMatrix[row, column] = dFdX[m, l] * step * a[j, p] + dfddx;
                            }
                        }
                    }
                }
                Vector <double> dk = jacobiMatrix.Solve(-f);
                for (int j = 0; j < Stages; j++)
                {
                    k[j] += dk.SubVector(j * system.Size, system.Size);
                }
                if (f.L2Norm() < fAbsTol)
                {
                    for (int j = 0; j < Stages; j++)
                    {
                        x += k[j] * step * b[j];
                    }
                    return(x);
                }
            }
            throw new Exception("Решение не сошлось");
        }
Beispiel #2
0
        public override Vector <double> IntegrateStep(DAEISystem system, Vector <double> x, double t)
        {
            //solve f(xn+1,(xn+1-xn)/h,t+h)
            Vector <double> xNew = x;

            for (int i = 0; i < newtonIterations; i++)
            {
                Vector <double> dx    = (xNew - x) / step;
                double          time  = t + step;
                Matrix <double> dFdX  = system.DFdX(xNew, dx, time); //use k as dx
                Matrix <double> dFddX = system.DFddX(xNew, dx, time);
                Vector <double> F     = system.F(xNew, dx, time);
                F = F * alpha;
                //Vector<double> f = Vector<double>.Build.SparseOfArray(F).Multiply(alpha);
                Matrix <double> jacobiMatrix;// = Matrix<double>.Build.Sparse(system.Size, system.Size);
                jacobiMatrix = dFdX.Add(dFddX.Divide(step));

                /* for (int m = 0; m < system.Size; m++)
                 * {
                 *   for (int l = 0; l < system.Size; l++)
                 *   {
                 *       jacobiMatrix[m, l] = dFdX[m, l]  + dFddX[m, l]/step;
                 *   }
                 * }*/
                Vector <double> deltax = jacobiMatrix.Solve(-F);
                xNew += deltax;
                if (F.L2Norm() < fAbsTol)
                {
                    return(xNew);
                }
            }
            throw new Exception("Решение не сошлось");
        }