public static void centerColum(DoubleMatrix2D mat) {
   System.out.println("Standardizing probe mean");
   for (int c = 0; c < mat.columns(); c++) {
     double mean = Descriptives.mean(mat.viewColumn(c).toArray());
     for (int r = 0; r < mat.rows(); r++) {
       mat.set(r, c, ((mat.getQuick(r, c) - mean)));
     }
   }
 }
 public static Pair<Integer, Integer> getIndexOfFirstZeroIgnoreDiagonal(DoubleMatrix2D a) {
   for (int i = 0; i < a.rows(); i++) {
     for (int j = 0; j < a.columns(); j++) {
       if (i != j && a.get(i, j) == 0) {
         return new Pair<Integer, Integer>(i, j);
       }
     }
   }
   return null;
 }
 public static Pair<Integer, Integer> getIndexOfFirstNegative(DoubleMatrix2D a) {
   for (int i = 0; i < a.rows(); i++) {
     for (int j = 0; j < a.columns(); j++) {
       if (a.get(i, j) < 0) {
         return new Pair<Integer, Integer>(i, j);
       }
     }
   }
   return null;
 }
 public static boolean containsNaNs(DoubleMatrix2D a) {
   for (int i = 0; i < a.rows(); i++) {
     for (int j = 0; j < a.columns(); j++) {
       if (Double.isNaN(a.get(i, j))) {
         return true;
       }
     }
   }
   return false;
 }
  public static void rankColumns(DoubleMatrix2D matrix) {
    RankingAlgorithm COV_RANKER_TIE = new NaturalRanking(NaNStrategy.FAILED, TiesStrategy.AVERAGE);

    for (int c = 0; c < matrix.columns(); c++) {
      double[] rank = COV_RANKER_TIE.rank(matrix.viewColumn(c).toArray());
      for (int r = 0; r < matrix.rows(); r++) {
        matrix.set(r, c, (rank[r]));
      }
    }
  }
 public static void scaleColum(DoubleMatrix2D mat) {
   System.out.println("Standardizing probe standard deviation");
   for (int c = 0; c < mat.columns(); c++) {
     double[] t = mat.viewColumn(c).toArray();
     double mean = Descriptives.mean(t);
     double stdev = Math.sqrt(Descriptives.variance(t, mean));
     for (int r = 0; r < mat.rows(); r++) {
       mat.set(r, c, ((mat.getQuick(r, c)) / stdev));
     }
   }
 }
  private double gcvTsvdDCT2D(DoubleMatrix2D S, DoubleMatrix2D Bhat) {
    int length = S.rows() * S.columns();
    DoubleMatrix1D s = new DenseDoubleMatrix1D(length);
    DoubleMatrix1D bhat = new DenseDoubleMatrix1D(length);
    System.arraycopy(
        ((DenseDoubleMatrix2D) S).elements(), 0, ((DenseDoubleMatrix1D) s).elements(), 0, length);
    System.arraycopy(
        ((DenseDoubleMatrix2D) Bhat).elements(),
        0,
        ((DenseDoubleMatrix1D) bhat).elements(),
        0,
        length);
    s.assign(DoubleFunctions.abs);
    bhat.assign(DoubleFunctions.abs);
    final double[] svalues = (double[]) ((DenseDoubleMatrix1D) s).elements();

    IntComparator compDec =
        new IntComparator() {
          public int compare(int a, int b) {
            if (svalues[a] != svalues[a] || svalues[b] != svalues[b])
              return compareNaN(svalues[a], svalues[b]); // swap NaNs to
            // the end
            return svalues[a] < svalues[b] ? 1 : (svalues[a] == svalues[b] ? 0 : -1);
          }
        };
    int[] indices = DoubleSorting.quickSort.sortIndex(s, compDec);
    s = s.viewSelection(indices);
    bhat = bhat.viewSelection(indices);
    int n = s.size();
    double[] rho = new double[n - 1];
    rho[n - 2] = bhat.getQuick(n - 1) * bhat.getQuick(n - 1);
    DoubleMatrix1D G = new DenseDoubleMatrix1D(n - 1);
    double[] elemsG = (double[]) G.elements();
    elemsG[n - 2] = rho[n - 2];
    double bhatel, temp1;
    for (int k = n - 2; k > 0; k--) {
      bhatel = bhat.getQuick(k);
      rho[k - 1] = rho[k] + bhatel * bhatel;
      temp1 = n - k;
      temp1 = temp1 * temp1;
      elemsG[k - 1] = rho[k - 1] / temp1;
    }
    for (int k = 0; k < n - 3; k++) {
      if (s.getQuick(k) == s.getQuick(k + 1)) {
        elemsG[k] = Double.POSITIVE_INFINITY;
      }
    }
    return s.getQuick((int) G.getMinLocation()[1]);
  }
Beispiel #8
0
  /**
   * Solves for bus voltages given the full system admittance matrix (for all buses), the complex
   * bus power injection vector (for all buses), the initial vector of complex bus voltages, and
   * column vectors with the lists of bus indices for the swing bus, PV buses, and PQ buses,
   * respectively. The bus voltage vector contains the set point for generator (including ref bus)
   * buses, and the reference angle of the swing bus, as well as an initial guess for remaining
   * magnitudes and angles. JPOPT is a JPOWER options vector which can be used to set the
   * termination tolerance, maximum number of iterations, and output options (see JPOPTION for
   * details). Uses default options if this parameter is not given. Returns the final complex
   * voltages, a flag which indicates whether it converged or not, and the number of iterations
   * performed.
   *
   * @param Ybus
   * @param Sbus
   * @param V0
   * @param ref
   * @param pv
   * @param pq
   * @param jpopt
   * @return
   */
  @SuppressWarnings("static-access")
  public static Object[] jp_newtonpf(
      DComplexMatrix2D Ybus,
      DComplexMatrix1D Sbus,
      DComplexMatrix1D V0,
      int ref,
      int[] pv,
      int[] pq,
      Map<String, Double> jpopt) {
    int i, max_it, verbose, npv, npq, j1, j2, j3, j4, j5, j6;
    int[] pvpq;
    double tol, normF;
    boolean converged;
    DoubleMatrix1D F, dx;
    DoubleMatrix2D J11, J12, J21, J22, J1, J2, J;
    DComplexMatrix1D mis, V, Va, Vm, dxz;
    DComplexMatrix2D dSbus_dVm, dSbus_dVa;
    DComplexMatrix2D[] dSbus_dV;
    SparseRCDoubleMatrix2D JJ;

    /* options */
    tol = jpopt.get("PF_TOL");
    max_it = jpopt.get("PF_MAX_IT").intValue();
    verbose = jpopt.get("VERBOSE").intValue();

    /* initialize */
    pvpq = Djp_util.icat(pv, pq);
    converged = false;
    i = 0;
    V = V0;
    Va = V.copy().assign(cfunc.arg);
    Vm = V.copy().assign(cfunc.abs);

    /* set up indexing for updating V */
    npv = pv.length;
    npq = pq.length;
    j1 = 0;
    j2 = npv; // j1:j2 - V angle of pv buses
    j3 = j2;
    j4 = j2 + npq; // j3:j4 - V angle of pq buses
    j5 = j4;
    j6 = j4 + npq; // j5:j6 - V mag of pq buses

    /* evaluate F(x0) */
    mis = Ybus.zMult(V, null).assign(cfunc.conj);
    mis.assign(V, cfunc.mult).assign(Sbus, cfunc.minus);
    F =
        DoubleFactory1D.sparse.make(
            new DoubleMatrix1D[] {
              mis.viewSelection(pvpq).getRealPart(), mis.viewSelection(pq).getImaginaryPart()
            });

    /* check tolerance */
    normF = DenseDoubleAlgebra.DEFAULT.norm(F, Norm.Infinity);
    if (verbose > 0) System.out.print("(Newton)\n");
    if (verbose > 1) {
      System.out.printf("\n it    max P & Q mismatch (p.u.)");
      System.out.printf("\n----  ---------------------------");
      System.out.printf("\n%3d        %10.3e", i, normF);
    }
    if (normF < tol) {
      converged = true;
      if (verbose > 1) System.out.printf("\nConverged!\n");
    }

    /* do Newton iterations */
    while ((!converged) & (i < max_it)) {
      /* update iteration counter */
      i += 1;

      /* evaluate Jacobian */
      dSbus_dV = Djp_dSbus_dV.jp_dSbus_dV(Ybus, V);
      dSbus_dVm = dSbus_dV[0];
      dSbus_dVa = dSbus_dV[1];

      J11 = dSbus_dVa.getRealPart().viewSelection(pvpq, pvpq).copy();
      J12 = dSbus_dVm.getRealPart().viewSelection(pvpq, pq).copy();
      J21 = dSbus_dVa.getImaginaryPart().viewSelection(pq, pvpq).copy();
      J22 = dSbus_dVm.getImaginaryPart().viewSelection(pq, pq).copy();
      J1 = DoubleFactory2D.sparse.appendColumns(J11, J12);
      J2 = DoubleFactory2D.sparse.appendColumns(J21, J22);
      J = DoubleFactory2D.sparse.appendRows(J1, J2);
      JJ = new SparseRCDoubleMatrix2D(J.rows(), J.columns());
      JJ.assign(J);

      dx = SparseDoubleAlgebra.DEFAULT.solve(JJ, F).assign(dfunc.neg);
      dxz = Djp_util.complex(dx, null);

      /* update voltage */
      if (npv > 0)
        Va.viewSelection(pv).assign(dxz.viewSelection(Djp_util.irange(j1, j2)), cfunc.plus);
      if (npq > 0) {
        Va.viewSelection(pq).assign(dxz.viewSelection(Djp_util.irange(j3, j4)), cfunc.plus);
        Vm.viewSelection(pq).assign(dxz.viewSelection(Djp_util.irange(j5, j6)), cfunc.plus);
      }

      V = Djp_util.polar(Vm.getRealPart(), Va.getRealPart());
      /* update Vm and Va again in case we wrapped around with a negative Vm */
      Va = V.copy().assign(cfunc.arg);
      Vm = V.copy().assign(cfunc.abs);

      /* evalute F(x) */
      mis = Ybus.zMult(V, null).assign(cfunc.conj);
      mis.assign(V, cfunc.mult).assign(Sbus, cfunc.minus);
      F =
          DoubleFactory1D.sparse.make(
              new DoubleMatrix1D[] {
                mis.viewSelection(pvpq).getRealPart(), mis.viewSelection(pq).getImaginaryPart()
              });

      /* check for convergence */
      normF = DenseDoubleAlgebra.DEFAULT.norm(F, Norm.Infinity);
      if (verbose > 1) System.out.printf("\n%3d        %10.3e", i, normF);
      if (normF < tol) {
        converged = true;
        if (verbose > 0)
          System.out.printf("\nNewton's method power flow converged in %d iterations.\n", i);
      }
    }
    if (verbose > 0)
      if (!converged)
        System.out.printf("\nNewton''s method power did not converge in %d iterations.\n", i);

    return new Object[] {V, converged, i};
  }