Exemple #1
0
void CONSTR_PAR_GEN_P_count_step(Constr* c, Branch* br, int t) {

  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen1;
  Gen* gen2;
  int* A_nnz;
  int* A_row;
  char* bus_counted;
  int i;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);

  // Constr data
  A_nnz = CONSTR_get_A_nnz_ptr(c);
  A_row = CONSTR_get_A_row_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointer
  if (!A_nnz || !A_row || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_k(br);
  buses[1] = BRANCH_get_bus_m(br);

  // Buses
  for (i = 0; i < 2; i++) {

    bus = buses[i];

    if (!bus_counted[BUS_get_index(bus)*T+t]) {

      // Active power of slack generators
      if (BUS_is_slack(bus)) {
	gen1 = BUS_get_gen(bus);
	for (gen2 = GEN_get_next(gen1); gen2 != NULL; gen2 = GEN_get_next(gen2)) {
	  if (GEN_has_flags(gen1,FLAG_VARS,GEN_VAR_P))
	    (*A_nnz)++;
	  if (GEN_has_flags(gen2,FLAG_VARS,GEN_VAR_P))
	    (*A_nnz)++;
	  (*A_row)++;
	}
      }
    }

    // Update counted flag
    bus_counted[BUS_get_index(bus)*T+t] = TRUE;
  }
}
Exemple #2
0
void CONSTR_LOAD_PF_count_step(Constr* c, Branch* br, int t) {

  // Local variables
  Bus* buses[2];
  Bus* bus;
  Load* load;
  int* A_nnz;
  int* A_row;
  char* bus_counted;
  int i;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);

  // Constr data
  A_nnz = CONSTR_get_A_nnz_ptr(c);
  A_row = CONSTR_get_A_row_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointer
  if (!A_nnz || !A_row || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_k(br);
  buses[1] = BRANCH_get_bus_m(br);

  // Buses
  for (i = 0; i < 2; i++) {

    bus = buses[i];
    
    if (!bus_counted[BUS_get_index(bus)*T+t]) {

      // Loads
      for (load = BUS_get_load(bus); load != NULL; load = LOAD_get_next(load)) {

	// Variables
	if (LOAD_has_flags(load,FLAG_VARS,LOAD_VAR_P) && LOAD_has_flags(load,FLAG_VARS,LOAD_VAR_Q)) {
	  (*A_nnz)++; // P
	  (*A_nnz)++; // Q
	  (*A_row)++;
	}
      }
    }

    // Update counted flag
    bus_counted[BUS_get_index(bus)*T+t] = TRUE;
  }
}
Exemple #3
0
void CONSTR_PAR_GEN_Q_count_branch(Constr* c, Branch* br) {
  
  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen1;
  Gen* gen2;
  int* Acounter;
  int* Aconstr_index;
  char* bus_counted;
  int i;
  int j;
  
  // Constr data
  Acounter = CONSTR_get_Acounter_ptr(c);
  Aconstr_index = CONSTR_get_Aconstr_index_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);
  if (!Acounter || !Aconstr_index || !bus_counted)
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_from(br);
  buses[1] = BRANCH_get_bus_to(br);

  // Buses
  for (i = 0; i < 2; i++) {
    
    bus = buses[i];
    
    if (!bus_counted[BUS_get_index(bus)]) {
      
      // Reactive power of regulating generators
      if (BUS_is_regulated_by_gen(bus)) {
	gen1 = BUS_get_reg_gen(bus);
	for (gen2 = GEN_get_reg_next(gen1); gen2 != NULL; gen2 = GEN_get_reg_next(gen2)) {
	  if (GEN_has_flags(gen1,FLAG_VARS,GEN_VAR_Q))
	    (*Acounter)++;
	  if (GEN_has_flags(gen2,FLAG_VARS,GEN_VAR_Q))
	    (*Acounter)++;
	  (*Aconstr_index)++;
	}
      }
    }

    // Update counted flag
    bus_counted[BUS_get_index(bus)] = TRUE;    
  }
}
Exemple #4
0
void CONSTR_FIX_count_step(Constr* c, Branch* br, int t) {

  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen;
  Vargen* vargen;
  Shunt* shunt;
  Load* load;
  Bat* bat;
  int* A_nnz;
  int* A_row;
  char* bus_counted;
  int i;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);

  // Constr data
  A_nnz = CONSTR_get_A_nnz_ptr(c);
  A_row = CONSTR_get_A_row_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointers
  if (!A_nnz || !A_row || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_k(br);
  buses[1] = BRANCH_get_bus_m(br);

  // Tap ratio
  if (BRANCH_has_flags(br,FLAG_FIXED,BRANCH_VAR_RATIO) &&
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_RATIO)) {
    (*A_nnz)++;
    (*A_row)++;
  }

  // Phase shift
  if (BRANCH_has_flags(br,FLAG_FIXED,BRANCH_VAR_PHASE) &&
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_PHASE)) {
    (*A_nnz)++;
    (*A_row)++;
  }

  // Buses
  for (i = 0; i < 2; i++) {

    bus = buses[i];

    if (!bus_counted[BUS_get_index(bus)*T+t]) {

      // Voltage magnitude (V_MAG)
      if (BUS_has_flags(bus,FLAG_FIXED,BUS_VAR_VMAG) &&
	  BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VMAG)) {
	(*A_nnz)++;

	// Extra nz for regulating generator (for PV-PQ switching?)
	if (BUS_is_regulated_by_gen(bus) &&
	    GEN_has_flags(BUS_get_reg_gen(bus),FLAG_VARS,GEN_VAR_Q))
	  (*A_nnz)++;

	(*A_row)++;
      }

      // Voltage angle (V_ANG)
      if (BUS_has_flags(bus,FLAG_FIXED,BUS_VAR_VANG) &&
	  BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VANG)) {
	(*A_nnz)++;
	(*A_row)++;
      }

      // Generators
      for (gen = BUS_get_gen(bus); gen != NULL; gen = GEN_get_next(gen)) {

	// Active power (P)
	if (GEN_has_flags(gen,FLAG_FIXED,GEN_VAR_P) &&
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_P)) {
	  (*A_nnz)++;
	  (*A_row)++;
	}

	// Reactive power (Q)
	if (GEN_has_flags(gen,FLAG_FIXED,GEN_VAR_Q) &&
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q)) {
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }

      // Variable generators
      for (vargen = BUS_get_vargen(bus); vargen != NULL; vargen = VARGEN_get_next(vargen)) {

	// Active power (P)
	if (VARGEN_has_flags(vargen,FLAG_FIXED,VARGEN_VAR_P) &&
	    VARGEN_has_flags(vargen,FLAG_VARS,VARGEN_VAR_P)) {
	  (*A_nnz)++;
	  (*A_row)++;
	}

	// Reactive power (Q)
	if (VARGEN_has_flags(vargen,FLAG_FIXED,VARGEN_VAR_Q) &&
	    VARGEN_has_flags(vargen,FLAG_VARS,VARGEN_VAR_Q)) {
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }

      // Shunts
      for (shunt = BUS_get_shunt(bus); shunt != NULL; shunt = SHUNT_get_next(shunt)) {

	// Susceptance (b)
	if (SHUNT_has_flags(shunt,FLAG_FIXED,SHUNT_VAR_SUSC) &&
	    SHUNT_has_flags(shunt,FLAG_VARS,SHUNT_VAR_SUSC)) {
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }

      // Batteries
      for (bat = BUS_get_bat(bus); bat != NULL; bat = BAT_get_next(bat)) {

	// Charging/discharging power (P)
	if (BAT_has_flags(bat,FLAG_FIXED,BAT_VAR_P) &&
	    BAT_has_flags(bat,FLAG_VARS,BAT_VAR_P)) {
	  (*A_nnz) += 2;
	  (*A_row) += 2;
	}

	// Energy level (E)
	if (BAT_has_flags(bat,FLAG_FIXED,BAT_VAR_E) &&
	    BAT_has_flags(bat,FLAG_VARS,BAT_VAR_E)) {
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }

      // Loads
      for (load = BUS_get_load(bus); load != NULL; load = LOAD_get_next(load)) {

	// Active power (P)
	if (LOAD_has_flags(load,FLAG_FIXED,LOAD_VAR_P) &&
	    LOAD_has_flags(load,FLAG_VARS,LOAD_VAR_P)) {
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }
    }

    // Update counted flag
    bus_counted[BUS_get_index(bus)*T+t] = TRUE;
  }
}
Exemple #5
0
void CONSTR_FIX_analyze_step(Constr* c, Branch* br, int t) {

  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen;
  Vargen* vargen;
  Bat* bat;
  Load* load;
  Shunt* shunt;
  int* A_nnz;
  int* A_row;
  char* bus_counted;
  Vec* b;
  Mat* A;
  int i;
  REAL Pc;
  REAL Pd;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);

  // Cosntr data
  b = CONSTR_get_b(c);
  A = CONSTR_get_A(c);
  A_nnz = CONSTR_get_A_nnz_ptr(c);
  A_row = CONSTR_get_A_row_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointers
  if (!A_nnz || !A_row || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_k(br);
  buses[1] = BRANCH_get_bus_m(br);

  // Tap ratio
  if (BRANCH_has_flags(br,FLAG_FIXED,BRANCH_VAR_RATIO) &&
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_RATIO)) {
    VEC_set(b,*A_row,BRANCH_get_ratio(br,t));
    MAT_set_i(A,*A_nnz,*A_row);
    MAT_set_j(A,*A_nnz,BRANCH_get_index_ratio(br,t));
    MAT_set_d(A,*A_nnz,1.);
    (*A_nnz)++;
    (*A_row)++;
  }

  // Phase shift
  if (BRANCH_has_flags(br,FLAG_FIXED,BRANCH_VAR_PHASE) &&
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_PHASE)) {
    VEC_set(b,*A_row,BRANCH_get_phase(br,t));
    MAT_set_i(A,*A_nnz,*A_row);
    MAT_set_j(A,*A_nnz,BRANCH_get_index_phase(br,t));
    MAT_set_d(A,*A_nnz,1.);
    (*A_nnz)++;
    (*A_row)++;
  }

  // Buses
  for (i = 0; i < 2; i++) {

    bus = buses[i];

    if (!bus_counted[BUS_get_index(bus)*T+t]) {

      // Voltage magnitude (V_MAG)
      if (BUS_has_flags(bus,FLAG_FIXED,BUS_VAR_VMAG) &&
	  BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VMAG)) {
	if (BUS_is_regulated_by_gen(bus))
	  VEC_set(b,*A_row,BUS_get_v_set(bus,t));
	else
	  VEC_set(b,*A_row,BUS_get_v_mag(bus,t));
	MAT_set_i(A,*A_nnz,*A_row);
	MAT_set_j(A,*A_nnz,BUS_get_index_v_mag(bus,t));
	MAT_set_d(A,*A_nnz,1.);
	(*A_nnz)++;

	// Extra nz for regulating generator (for PV-PQ switching?)
	if (BUS_is_regulated_by_gen(bus) &&
	    GEN_has_flags(BUS_get_reg_gen(bus),FLAG_VARS,GEN_VAR_Q)) {
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,GEN_get_index_Q(BUS_get_reg_gen(bus),t));
	  MAT_set_d(A,*A_nnz,0.); // placeholder
	  (*A_nnz)++;
	}

	(*A_row)++;
      }

      // Voltage angle (V_ANG)
      if (BUS_has_flags(bus,FLAG_FIXED,BUS_VAR_VANG) &&
	  BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VANG)) {
	VEC_set(b,*A_row,BUS_get_v_ang(bus,t));
	MAT_set_i(A,*A_nnz,*A_row);
	MAT_set_j(A,*A_nnz,BUS_get_index_v_ang(bus,t));
	MAT_set_d(A,*A_nnz,1.);
	(*A_nnz)++;
	(*A_row)++;
      }

      // Generators
      for (gen = BUS_get_gen(bus); gen != NULL; gen = GEN_get_next(gen)) {

	// Active power (P)
	if (GEN_has_flags(gen,FLAG_FIXED,GEN_VAR_P) &&
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_P)) {
	  VEC_set(b,*A_row,GEN_get_P(gen,t));
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,GEN_get_index_P(gen,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;
	}

	// Reactive power (Q)
	if (GEN_has_flags(gen,FLAG_FIXED,GEN_VAR_Q) &&
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q)) {
	  VEC_set(b,*A_row,GEN_get_Q(gen,t));
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,GEN_get_index_Q(gen,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }

      // Variable generators
      for (vargen = BUS_get_vargen(bus); vargen != NULL; vargen = VARGEN_get_next(vargen)) {

	// Active power (P)
	if (VARGEN_has_flags(vargen,FLAG_FIXED,VARGEN_VAR_P) &&
	    VARGEN_has_flags(vargen,FLAG_VARS,VARGEN_VAR_P)) {
	  VEC_set(b,*A_row,VARGEN_get_P(vargen,t));
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,VARGEN_get_index_P(vargen,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;
	}

	// Reactive power (Q)
	if (VARGEN_has_flags(vargen,FLAG_FIXED,VARGEN_VAR_Q) &&
	    VARGEN_has_flags(vargen,FLAG_VARS,VARGEN_VAR_Q)) {
	  VEC_set(b,*A_row,VARGEN_get_Q(vargen,t));
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,VARGEN_get_index_Q(vargen,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }

      // Shunts
      for (shunt = BUS_get_shunt(bus); shunt != NULL; shunt = SHUNT_get_next(shunt)) {

	// Susceptance (b)
	if (SHUNT_has_flags(shunt,FLAG_FIXED,SHUNT_VAR_SUSC) &&
	    SHUNT_has_flags(shunt,FLAG_VARS,SHUNT_VAR_SUSC)) {
	  VEC_set(b,*A_row,SHUNT_get_b(shunt,t));
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,SHUNT_get_index_b(shunt,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }

      // Batteries
      for (bat = BUS_get_bat(bus); bat != NULL; bat = BAT_get_next(bat)) {

	// Charging/discharging power (P)
	if (BAT_has_flags(bat,FLAG_FIXED,BAT_VAR_P) &&
	    BAT_has_flags(bat,FLAG_VARS,BAT_VAR_P)) {

	  if (BAT_get_P(bat,t) >= 0) {
	    Pc = BAT_get_P(bat,t);
	    Pd = 0.;
	  }
	  else {
	    Pc = 0.;
	    Pd = -BAT_get_P(bat,t);
	  }

	  // Pc
	  VEC_set(b,*A_row,Pc);
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,BAT_get_index_Pc(bat,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;

	  // Pd
	  VEC_set(b,*A_row,Pd);
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,BAT_get_index_Pd(bat,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;
	}

	// Energy level (E)
	if (BAT_has_flags(bat,FLAG_FIXED,BAT_VAR_E) &&
	    BAT_has_flags(bat,FLAG_VARS,BAT_VAR_E)) {
	  VEC_set(b,*A_row,BAT_get_E(bat,t));
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,BAT_get_index_E(bat,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }

      // Loads
      for (load = BUS_get_load(bus); load != NULL; load = LOAD_get_next(load)) {

	// Active power (P)
	if (LOAD_has_flags(load,FLAG_FIXED,LOAD_VAR_P) &&
	    LOAD_has_flags(load,FLAG_VARS,LOAD_VAR_P)) {
	  VEC_set(b,*A_row,LOAD_get_P(load,t));
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,LOAD_get_index_P(load,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;
	  (*A_row)++;
	}
      }
    }

    // Update counted flag
    bus_counted[BUS_get_index(bus)*T+t] = TRUE;
  }
}
Exemple #6
0
void CONSTR_PAR_GEN_Q_analyze_branch(Constr* c, Branch* br) {
  
  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen1;
  Gen* gen2;
  int* Acounter;
  int* Aconstr_index;
  char* bus_counted;
  Vec* b;
  Mat* A;
  int i;
  int j;
  REAL Qmin1;
  REAL Qmin2;
  REAL dQ1;
  REAL dQ2;
  
  // Cosntr data
  b = CONSTR_get_b(c);
  A = CONSTR_get_A(c);
  Acounter = CONSTR_get_Acounter_ptr(c);
  Aconstr_index = CONSTR_get_Aconstr_index_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);
  if (!Acounter || !Aconstr_index || !bus_counted)
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_from(br);
  buses[1] = BRANCH_get_bus_to(br);

  // Buses
  for (i = 0; i < 2; i++) {
    
    bus = buses[i];
    
    if (!bus_counted[BUS_get_index(bus)]) {
      
      // Reactive power of regulating generators
      if (BUS_is_regulated_by_gen(bus)) {
	gen1 = BUS_get_reg_gen(bus);
	Qmin1 = GEN_get_Q_min(gen1);
	dQ1 = GEN_get_Q_max(gen1)-Qmin1;
	if (dQ1 < CONSTR_PAR_GEN_Q_PARAM)
	  dQ1 = CONSTR_PAR_GEN_Q_PARAM;
	for (gen2 = GEN_get_reg_next(gen1); gen2 != NULL; gen2 = GEN_get_reg_next(gen2)) {
	  Qmin2 = GEN_get_Q_min(gen2);
	  dQ2 = GEN_get_Q_max(gen2)-Qmin2;
	  if (dQ2 < CONSTR_PAR_GEN_Q_PARAM)
	    dQ2 = CONSTR_PAR_GEN_Q_PARAM;
	  VEC_set(b,*Aconstr_index,Qmin1/dQ1-Qmin2/dQ2);
	  if (GEN_has_flags(gen1,FLAG_VARS,GEN_VAR_Q)) {
	    MAT_set_i(A,*Acounter,*Aconstr_index);
	    MAT_set_j(A,*Acounter,GEN_get_index_Q(gen1));
	    MAT_set_d(A,*Acounter,1./dQ1);
	    (*Acounter)++;
	  }
	  else
	    VEC_add_to_entry(b,*Aconstr_index,-GEN_get_Q(gen1)/dQ1); 
	  if (GEN_has_flags(gen2,FLAG_VARS,GEN_VAR_Q)) {
	    MAT_set_i(A,*Acounter,*Aconstr_index);
	    MAT_set_j(A,*Acounter,GEN_get_index_Q(gen2));	      
	    MAT_set_d(A,*Acounter,-1./dQ2);
	    (*Acounter)++;
	  }
	  else
	    VEC_add_to_entry(b,*Aconstr_index,GEN_get_Q(gen2)/dQ2); 
	  (*Aconstr_index)++;
	}
      }
    }

    // Update counted flag
    bus_counted[BUS_get_index(bus)] = TRUE;    
  }  
}
Exemple #7
0
void CONSTR_LOAD_PF_analyze_step(Constr* c, Branch* br, int t) {

  // Local variables
  Bus* buses[2];
  Bus* bus;
  Load* load;
  int* A_nnz;
  int* A_row;
  char* bus_counted;
  REAL gamma;
  REAL factor;
  Vec* b;
  Mat* A;
  int i;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);

  // Cosntr data
  b = CONSTR_get_b(c);
  A = CONSTR_get_A(c);
  A_nnz = CONSTR_get_A_nnz_ptr(c);
  A_row = CONSTR_get_A_row_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointers
  if (!A_nnz || !A_row || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_k(br);
  buses[1] = BRANCH_get_bus_m(br);

  // Buses
  for (i = 0; i < 2; i++) {

    bus = buses[i];

    if (!bus_counted[BUS_get_index(bus)*T+t]) {

      // Loads
      for (load = BUS_get_load(bus); load != NULL; load = LOAD_get_next(load)) {

	// Variables
	if (LOAD_has_flags(load,FLAG_VARS,LOAD_VAR_P) && LOAD_has_flags(load,FLAG_VARS,LOAD_VAR_Q)) {

	  gamma = LOAD_get_target_power_factor(load);
	  factor = sqrt((1.-gamma*gamma)/(gamma*gamma));

	  // A
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,LOAD_get_index_P(load,t));
	  if (LOAD_get_P(load,t)*LOAD_get_Q(load,t) >= 0)
	    MAT_set_d(A,*A_nnz,-factor);
	  else
	    MAT_set_d(A,*A_nnz,factor);
	  (*A_nnz)++;

	  // A
	  MAT_set_i(A,*A_nnz,*A_row);
	  MAT_set_j(A,*A_nnz,LOAD_get_index_Q(load,t));
	  MAT_set_d(A,*A_nnz,1.);
	  (*A_nnz)++;

	  // b
	  VEC_set(b,*A_row,0.);

	  (*A_row)++;
	}
      }
    }

    // Update counted flag
    bus_counted[BUS_get_index(bus)*T+t] = TRUE;
  }
}
Exemple #8
0
void CONSTR_BOUND_store_sens_step(Constr* c, Branch* br, int t, Vec* sA, Vec* sf, Vec* sGu, Vec* sGl) {
  
  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen;
  Shunt* shunt;
  int* Jcounter;
  char* bus_counted;
  int bus_index_t[2];
  int k;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);
  
  // Constr data
  Jcounter = CONSTR_get_Jcounter_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointers
  if (!Jcounter || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;
 
  // Bus data
  buses[0] = BRANCH_get_bus_from(br);
  buses[1] = BRANCH_get_bus_to(br);
  for (k = 0; k < 2; k++)
    bus_index_t[k] = BUS_get_index(buses[k])*T+t;

  // Tap ratio
  if (BRANCH_has_flags(br,FLAG_BOUNDED,BRANCH_VAR_RATIO) && 
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_RATIO)) {
    (*Jcounter)++; // upper bound
    (*Jcounter)++; // lower bound
  }

  // Phase shift
  if (BRANCH_has_flags(br,FLAG_BOUNDED,BRANCH_VAR_PHASE) && 
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_PHASE)) {
    (*Jcounter)++; // upper bound
    (*Jcounter)++; // lower bound
  }
  
  // Buses
  for (k = 0; k < 2; k++) {

    bus = buses[k];

    if (!bus_counted[bus_index_t[k]]) { // not counted yet

      // Voltage magnitude (V_MAG)
      if (BUS_has_flags(bus,FLAG_BOUNDED,BUS_VAR_VMAG) && 
	  BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VMAG)) {
	BUS_set_sens_v_mag_u_bound(bus,VEC_get(sf,*Jcounter),t);
	(*Jcounter)++; // upper bound
	BUS_set_sens_v_mag_l_bound(bus,VEC_get(sf,*Jcounter),t);
	(*Jcounter)++; // lower bound
      }

      // Volage angle (V_ANG)
      if (BUS_has_flags(bus,FLAG_BOUNDED,BUS_VAR_VANG) && 
	  BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VANG)) {
	(*Jcounter)++; // upper bound
	(*Jcounter)++; // lower bound
      }

      // Generators
      for (gen = BUS_get_gen(bus); gen != NULL; gen = GEN_get_next(gen)) {
	
	// Active power (P)
	if (GEN_has_flags(gen,FLAG_BOUNDED,GEN_VAR_P) && 
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_P)) {
	  (*Jcounter)++; // upper bound
	  (*Jcounter)++; // lower bound
	}
	
	// Reactive power (Q)
	if (GEN_has_flags(gen,FLAG_BOUNDED,GEN_VAR_Q) && 
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q)) {
	  (*Jcounter)++; // upper bound
	  (*Jcounter)++; // lower bound
	}
      }
      
      // Shunts
      for (shunt = BUS_get_shunt(bus); shunt != NULL; shunt = SHUNT_get_next(shunt)) {
	
	// Susceptance
	if (SHUNT_has_flags(shunt,FLAG_BOUNDED,SHUNT_VAR_SUSC) && 
	    SHUNT_has_flags(shunt,FLAG_VARS,SHUNT_VAR_SUSC)) {
	  (*Jcounter)++; // upper bound
	  (*Jcounter)++; // lower bound
	}
      }
    }

    // Update counted flag
    bus_counted[bus_index_t[k]] = TRUE;
  }
}
Exemple #9
0
void CONSTR_BOUND_eval_step(Constr* c, Branch* br, int t, Vec* var_values) {

  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen;
  Shunt* shunt;
  Mat* H_array;
  REAL* f;
  REAL* J;
  Mat* H;
  int* Jcounter;
  char* bus_counted;
  int bus_index_t[2];
  int k;
  REAL u;
  REAL umin;
  REAL umax;
  REAL du;
  REAL a1;
  REAL a2;
  REAL b;
  REAL eps;
  REAL sqrterm1;
  REAL sqrterm2;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);

  // Constr data
  f = VEC_get_data(CONSTR_get_f(c));
  J = MAT_get_data_array(CONSTR_get_J(c));
  H_array = CONSTR_get_H_array(c);
  Jcounter = CONSTR_get_Jcounter_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointers
  if (!f || !J || !Jcounter || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;

  // Param
  eps = CONSTR_BOUND_PARAM;

  // Bus data
  buses[0] = BRANCH_get_bus_from(br);
  buses[1] = BRANCH_get_bus_to(br);
  for (k = 0; k < 2; k++)
    bus_index_t[k] = BUS_get_index(buses[k])*T+t;

  // Branch
  //*******

  // Tap ratio
  if (BRANCH_has_flags(br,FLAG_BOUNDED,BRANCH_VAR_RATIO) && 
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_RATIO)) {
    
    u = VEC_get(var_values,BRANCH_get_index_ratio(br,t));
    umax = BRANCH_get_ratio_max(br);
    umin = BRANCH_get_ratio_min(br);
    du = (umax-umin > eps) ? umax-umin : eps;
    
    a1 = umax-u;
    a2 = u-umin;
    b = eps*eps/du;
    sqrterm1 = sqrt(a1*a1+b*b+eps*eps);
    sqrterm2 = sqrt(a2*a2+b*b+eps*eps);

    // f
    f[*Jcounter]   = a1 + b - sqrterm1; // upper
    f[*Jcounter+1] = a2 + b - sqrterm2; // lower
    
    // J
    J[*Jcounter]   = -(1-a1/sqrterm1);
    J[*Jcounter+1] = (1-a2/sqrterm2);

    // H
    H = MAT_array_get(H_array,*Jcounter);
    MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm1*sqrterm1*sqrterm1));

    H = MAT_array_get(H_array,*Jcounter+1);
    MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm2*sqrterm2*sqrterm2));

    (*Jcounter)++;
    (*Jcounter)++;
  }
  
  // Phase shift
  if (BRANCH_has_flags(br,FLAG_BOUNDED,BRANCH_VAR_PHASE) && 
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_PHASE)) {
    
    u = VEC_get(var_values,BRANCH_get_index_phase(br,t));
    umax = BRANCH_get_phase_max(br);
    umin = BRANCH_get_phase_min(br);
    du = (umax-umin > eps) ? umax-umin : eps;
    
    a1 = umax-u;
    a2 = u-umin;
    b = eps*eps/du;
    sqrterm1 = sqrt(a1*a1+b*b+eps*eps);
    sqrterm2 = sqrt(a2*a2+b*b+eps*eps);

    // f
    f[*Jcounter]   = a1 + b - sqrterm1; // upper
    f[*Jcounter+1] = a2 + b - sqrterm2; // lower
    
    // J
    J[*Jcounter]   = -(1-a1/sqrterm1);
    J[*Jcounter+1] = (1-a2/sqrterm2);

    // H
    H = MAT_array_get(H_array,*Jcounter);
    MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm1*sqrterm1*sqrterm1));

    H = MAT_array_get(H_array,*Jcounter+1);
    MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm2*sqrterm2*sqrterm2));

    (*Jcounter)++;
    (*Jcounter)++;    
  }
  
  // Buses
  //******

  for (k = 0; k < 2; k++) {

    bus = buses[k];

    if (!bus_counted[bus_index_t[k]]) { // not counted yet
      
      // Voltage magnitude (V_MAG)
      if (BUS_has_flags(bus,FLAG_BOUNDED,BUS_VAR_VMAG) && 
	  BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VMAG)) {
	
	u = VEC_get(var_values,BUS_get_index_v_mag(bus,t));
	umax = BUS_get_v_max(bus);
	umin = BUS_get_v_min(bus);
	du = (umax-umin > eps) ? umax-umin : eps;
	
	a1 = umax-u;
	a2 = u-umin;
	b = eps*eps/du;
	sqrterm1 = sqrt(a1*a1+b*b+eps*eps);
	sqrterm2 = sqrt(a2*a2+b*b+eps*eps);
	
	// f
	f[*Jcounter]   = a1 + b - sqrterm1; // upper
	f[*Jcounter+1] = a2 + b - sqrterm2; // lower
	
	// J
	J[*Jcounter]   = -(1-a1/sqrterm1);
	J[*Jcounter+1] = (1-a2/sqrterm2);
	
	// H
	H = MAT_array_get(H_array,*Jcounter);
	MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm1*sqrterm1*sqrterm1));
	
	H = MAT_array_get(H_array,*Jcounter+1);
	MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm2*sqrterm2*sqrterm2));
	
	(*Jcounter)++;
	(*Jcounter)++;	
      }

      // Volage angle (V_ANG)
      if (BUS_has_flags(bus,FLAG_BOUNDED,BUS_VAR_VANG) && 
	  BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VANG)) {
	
	u = VEC_get(var_values,BUS_get_index_v_ang(bus,t));
	umax = 2*PI;
	umin = -2*PI;
	du = (umax-umin > eps) ? umax-umin : eps;
	
	a1 = umax-u;
	a2 = u-umin;
	b = eps*eps/du;
	sqrterm1 = sqrt(a1*a1+b*b+eps*eps);
	sqrterm2 = sqrt(a2*a2+b*b+eps*eps);
	
	// f
	f[*Jcounter]   = a1 + b - sqrterm1; // upper
	f[*Jcounter+1] = a2 + b - sqrterm2; // lower
	
	// J
	J[*Jcounter]   = -(1-a1/sqrterm1);
	J[*Jcounter+1] = (1-a2/sqrterm2);
	
	// H
	H = MAT_array_get(H_array,*Jcounter);
	MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm1*sqrterm1*sqrterm1));
	
	H = MAT_array_get(H_array,*Jcounter+1);
	MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm2*sqrterm2*sqrterm2));
	
	(*Jcounter)++;
	(*Jcounter)++;	
      }

      // Generators
      for (gen = BUS_get_gen(bus); gen != NULL; gen = GEN_get_next(gen)) {
	
	// Active power (P)
	if (GEN_has_flags(gen,FLAG_BOUNDED,GEN_VAR_P) && 
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_P)) {
	  
	  u = VEC_get(var_values,GEN_get_index_P(gen,t));
	  umax = GEN_get_P_max(gen);
	  umin = GEN_get_P_min(gen);
	  du = (umax-umin > eps) ? umax-umin : eps;
	  
	  a1 = umax-u;
	  a2 = u-umin;
	  b = eps*eps/du;
	  sqrterm1 = sqrt(a1*a1+b*b+eps*eps);
	  sqrterm2 = sqrt(a2*a2+b*b+eps*eps);
	  
	  // f
	  f[*Jcounter]   = a1 + b - sqrterm1; // upper
	  f[*Jcounter+1] = a2 + b - sqrterm2; // lower
	  
	  // J
	  J[*Jcounter]   = -(1-a1/sqrterm1);
	  J[*Jcounter+1] = (1-a2/sqrterm2);
	  
	  // H
	  H = MAT_array_get(H_array,*Jcounter);
	  MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm1*sqrterm1*sqrterm1));
	  
	  H = MAT_array_get(H_array,*Jcounter+1);
	  MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm2*sqrterm2*sqrterm2));
	  
	  (*Jcounter)++;
	  (*Jcounter)++;
	}
	
	// Reactive power (Q)
	if (GEN_has_flags(gen,FLAG_BOUNDED,GEN_VAR_Q) && 
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q)) {
	  
	  u = VEC_get(var_values,GEN_get_index_Q(gen,t));
	  umax = GEN_get_Q_max(gen);
	  umin = GEN_get_Q_min(gen);
	  du = (umax-umin > eps) ? umax-umin : eps;
	  
	  a1 = umax-u;
	  a2 = u-umin;
	  b = eps*eps/du;
	  sqrterm1 = sqrt(a1*a1+b*b+eps*eps);
	  sqrterm2 = sqrt(a2*a2+b*b+eps*eps);
	  
	  // f
	  f[*Jcounter]   = a1 + b - sqrterm1; // upper
	  f[*Jcounter+1] = a2 + b - sqrterm2; // lower
	  
	  // J
	  J[*Jcounter]   = -(1-a1/sqrterm1);
	  J[*Jcounter+1] = (1-a2/sqrterm2);
	  
	  // H
	  H = MAT_array_get(H_array,*Jcounter);
	  MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm1*sqrterm1*sqrterm1));
	  
	  H = MAT_array_get(H_array,*Jcounter+1);
	  MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm2*sqrterm2*sqrterm2));
	  
	  (*Jcounter)++;
	  (*Jcounter)++;
	}
      }

      // Shunts
      for (shunt = BUS_get_shunt(bus); shunt != NULL; shunt = SHUNT_get_next(shunt)) {
	
	// Susceptance
	if (SHUNT_has_flags(shunt,FLAG_BOUNDED,SHUNT_VAR_SUSC) && 
	    SHUNT_has_flags(shunt,FLAG_VARS,SHUNT_VAR_SUSC)) {
	  
	  u = VEC_get(var_values,SHUNT_get_index_b(shunt,t));
	  umax = SHUNT_get_b_max(shunt);
	  umin = SHUNT_get_b_min(shunt);
	  du = (umax-umin > eps) ? umax-umin : eps;
	  
	  a1 = umax-u;
	  a2 = u-umin;
	  b = eps*eps/du;
	  sqrterm1 = sqrt(a1*a1+b*b+eps*eps);
	  sqrterm2 = sqrt(a2*a2+b*b+eps*eps);
	  
	  // f
	  f[*Jcounter]   = a1 + b - sqrterm1; // upper
	  f[*Jcounter+1] = a2 + b - sqrterm2; // lower
	  
	  // J
	  J[*Jcounter]   = -(1-a1/sqrterm1);
	  J[*Jcounter+1] = (1-a2/sqrterm2);
	  
	  // H
	  H = MAT_array_get(H_array,*Jcounter);
	  MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm1*sqrterm1*sqrterm1));
	  
	  H = MAT_array_get(H_array,*Jcounter+1);
	  MAT_set_d(H,0,-(b*b+eps*eps)/(sqrterm2*sqrterm2*sqrterm2));
	  
	  (*Jcounter)++;
	  (*Jcounter)++;
	}
      }
    }
    
    // Update counted flag
    bus_counted[bus_index_t[k]] = TRUE;
  }
}
Exemple #10
0
void CONSTR_BOUND_analyze_step(Constr* c, Branch* br, int t) {

  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen;
  Shunt* shunt;
  Mat* J;
  Mat* H_array;
  Mat* H;
  int* Hi;
  int* Hj;
  int* Hi_comb;
  int* Hj_comb;
  int* Jcounter;
  char* bus_counted;
  int bus_index_t[2];
  int index_var;
  int k;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);

  // Constr data
  J = CONSTR_get_J(c);
  H_array = CONSTR_get_H_array(c);
  Jcounter = CONSTR_get_Jcounter_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointers
  if (!Jcounter || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_from(br);
  buses[1] = BRANCH_get_bus_to(br);
  for (k = 0; k < 2; k++)
    bus_index_t[k] = BUS_get_index(buses[k])*T+t;

  // Branch
  //*******

  // Tap ratio
  if (BRANCH_has_flags(br,FLAG_BOUNDED,BRANCH_VAR_RATIO) && 
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_RATIO)) {

    index_var = BRANCH_get_index_ratio(br,t);
    
    // J
    MAT_set_i(J,*Jcounter,*Jcounter);
    MAT_set_j(J,*Jcounter,index_var);

    MAT_set_i(J,*Jcounter+1,*Jcounter+1);
    MAT_set_j(J,*Jcounter+1,index_var);

    // H
    H = MAT_array_get(H_array,*Jcounter);
    MAT_set_i(H,0,index_var);
    MAT_set_j(H,0,index_var);

    H = MAT_array_get(H_array,*Jcounter+1);
    MAT_set_i(H,0,index_var);
    MAT_set_j(H,0,index_var);

    (*Jcounter)++;
    (*Jcounter)++;
  }

  // Phase shift
  if (BRANCH_has_flags(br,FLAG_BOUNDED,BRANCH_VAR_PHASE) && 
      BRANCH_has_flags(br,FLAG_VARS,BRANCH_VAR_PHASE)) {

    index_var = BRANCH_get_index_phase(br,t);

    // J
    MAT_set_i(J,*Jcounter,*Jcounter);
    MAT_set_j(J,*Jcounter,index_var);

    MAT_set_i(J,*Jcounter+1,*Jcounter+1);
    MAT_set_j(J,*Jcounter+1,index_var);

    // H
    H = MAT_array_get(H_array,*Jcounter);
    MAT_set_i(H,0,index_var);
    MAT_set_j(H,0,index_var);

    H = MAT_array_get(H_array,*Jcounter+1);
    MAT_set_i(H,0,index_var);
    MAT_set_j(H,0,index_var);
    
    (*Jcounter)++;
    (*Jcounter)++;
  }
  
  // Buses
  //******

  for (k = 0; k < 2; k++) {

    bus = buses[k];

    if (!bus_counted[bus_index_t[k]]) { // not counted yet
      
      // Voltage magnitude (V_MAG)
      if (BUS_has_flags(bus,FLAG_BOUNDED,BUS_VAR_VMAG) && BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VMAG)) {

	index_var = BUS_get_index_v_mag(bus,t);

	// J
	MAT_set_i(J,*Jcounter,*Jcounter);
	MAT_set_j(J,*Jcounter,index_var);

	MAT_set_i(J,*Jcounter+1,*Jcounter+1);
	MAT_set_j(J,*Jcounter+1,index_var);
	
	// H
	H = MAT_array_get(H_array,*Jcounter);
	MAT_set_i(H,0,index_var);
	MAT_set_j(H,0,index_var);

	H = MAT_array_get(H_array,*Jcounter+1);
	MAT_set_i(H,0,index_var);
	MAT_set_j(H,0,index_var);
	
	(*Jcounter)++;
	(*Jcounter)++;
      }

      // Volage angle (V_ANG)
      if (BUS_has_flags(bus,FLAG_BOUNDED,BUS_VAR_VANG) && BUS_has_flags(bus,FLAG_VARS,BUS_VAR_VANG)) {

	index_var = BUS_get_index_v_ang(bus,t);

	// J
	MAT_set_i(J,*Jcounter,*Jcounter);
	MAT_set_j(J,*Jcounter,index_var);

	MAT_set_i(J,*Jcounter+1,*Jcounter+1);
	MAT_set_j(J,*Jcounter+1,index_var);
	
	// H
	H = MAT_array_get(H_array,*Jcounter);
	MAT_set_i(H,0,index_var);
	MAT_set_j(H,0,index_var);

	H = MAT_array_get(H_array,*Jcounter+1);
	MAT_set_i(H,0,index_var);
	MAT_set_j(H,0,index_var);
	
	(*Jcounter)++;
	(*Jcounter)++;
      }

      // Generators
      for (gen = BUS_get_gen(bus); gen != NULL; gen = GEN_get_next(gen)) {
	
	// Active power (P)
	if (GEN_has_flags(gen,FLAG_BOUNDED,GEN_VAR_P) && 
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_P)) {

	  index_var = GEN_get_index_P(gen,t);
	  
	  // J
	  MAT_set_i(J,*Jcounter,*Jcounter);
	  MAT_set_j(J,*Jcounter,index_var);

	  MAT_set_i(J,*Jcounter+1,*Jcounter+1);
	  MAT_set_j(J,*Jcounter+1,index_var);
	  
	  // H
	  H = MAT_array_get(H_array,*Jcounter);
	  MAT_set_i(H,0,index_var);
	  MAT_set_j(H,0,index_var);

	  H = MAT_array_get(H_array,*Jcounter+1);
	  MAT_set_i(H,0,index_var);
	  MAT_set_j(H,0,index_var);
	  
	  (*Jcounter)++;
	  (*Jcounter)++;
	}
	
	// Reactive power (Q)
	if (GEN_has_flags(gen,FLAG_BOUNDED,GEN_VAR_Q) && 
	    GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q)) {

	  index_var = GEN_get_index_Q(gen,t);
	  
	  // J
	  MAT_set_i(J,*Jcounter,*Jcounter);
	  MAT_set_j(J,*Jcounter,index_var);

	  MAT_set_i(J,*Jcounter+1,*Jcounter+1);
	  MAT_set_j(J,*Jcounter+1,index_var);
	  
	  // H
	  H = MAT_array_get(H_array,*Jcounter);
	  MAT_set_i(H,0,index_var);
	  MAT_set_j(H,0,index_var);

	  H = MAT_array_get(H_array,*Jcounter+1);
	  MAT_set_i(H,0,index_var);
	  MAT_set_j(H,0,index_var);
	  
	  (*Jcounter)++;
	  (*Jcounter)++;
	}
      }

      // Shunts
      for (shunt = BUS_get_shunt(bus); shunt != NULL; shunt = SHUNT_get_next(shunt)) {
		
	// Susceptance
	if (SHUNT_has_flags(shunt,FLAG_BOUNDED,SHUNT_VAR_SUSC) && 
	    SHUNT_has_flags(shunt,FLAG_VARS,SHUNT_VAR_SUSC)) {
	  
	  index_var = SHUNT_get_index_b(shunt,t);
	  
	  // J
	  MAT_set_i(J,*Jcounter,*Jcounter);
	  MAT_set_j(J,*Jcounter,index_var);

	  MAT_set_i(J,*Jcounter+1,*Jcounter+1);
	  MAT_set_j(J,*Jcounter+1,index_var);
	  
	  // H
	  H = MAT_array_get(H_array,*Jcounter);
	  MAT_set_i(H,0,index_var);
	  MAT_set_j(H,0,index_var);

	  H = MAT_array_get(H_array,*Jcounter+1);
	  MAT_set_i(H,0,index_var);
	  MAT_set_j(H,0,index_var);
	  
	  (*Jcounter)++;
	  (*Jcounter)++;
	}
      }
    }	  
    
    // Update counted flag
    bus_counted[bus_index_t[k]] = TRUE;
  }

  // Done (last branch and period)
  if ((t == T-1) && (BRANCH_get_index(br) == NET_get_num_branches(CONSTR_get_network(c))-1)) {
    
    // Ensure lower triangular and save struct of H comb
    Hi_comb = MAT_get_row_array(CONSTR_get_H_combined(c));
    Hj_comb = MAT_get_col_array(CONSTR_get_H_combined(c));
    for (k = 0; k < CONSTR_get_H_array_size(c); k++) {
      Hi = MAT_get_row_array(MAT_array_get(H_array,k));
      Hj = MAT_get_col_array(MAT_array_get(H_array,k));
      Hi_comb[k] = Hi[0];
      Hj_comb[k] = Hj[0];
    }
  }
}
Exemple #11
0
void CONSTR_PAR_GEN_P_analyze_step(Constr* c, Branch* br, int t) {

  // Local variables
  Bus* buses[2];
  Bus* bus;
  Gen* gen1;
  Gen* gen2;
  int* A_nnz;
  int* A_row;
  char* bus_counted;
  Vec* b;
  Mat* A;
  int i;
  int T;

  // Number of periods
  T = BRANCH_get_num_periods(br);

  // Cosntr data
  b = CONSTR_get_b(c);
  A = CONSTR_get_A(c);
  A_nnz = CONSTR_get_A_nnz_ptr(c);
  A_row = CONSTR_get_A_row_ptr(c);
  bus_counted = CONSTR_get_bus_counted(c);

  // Check pointers
  if (!A_nnz || !A_row || !bus_counted)
    return;

  // Check outage
  if (BRANCH_is_on_outage(br))
    return;

  // Bus data
  buses[0] = BRANCH_get_bus_k(br);
  buses[1] = BRANCH_get_bus_m(br);

  // Buses
  for (i = 0; i < 2; i++) {

    bus = buses[i];

    if (!bus_counted[BUS_get_index(bus)*T+t]) {

      // Active power of slack generators
      if (BUS_is_slack(bus)) {
	gen1 = BUS_get_gen(bus);
	for (gen2 = GEN_get_next(gen1); gen2 != NULL; gen2 = GEN_get_next(gen2)) {
	  VEC_set(b,*A_row,0.);
	  if (GEN_has_flags(gen1,FLAG_VARS,GEN_VAR_P)) {
	    MAT_set_i(A,*A_nnz,*A_row);
	    MAT_set_j(A,*A_nnz,GEN_get_index_P(gen1,t));
	    MAT_set_d(A,*A_nnz,1.);
	    (*A_nnz)++;
	  }
	  else
	    VEC_add_to_entry(b,*A_row,-GEN_get_P(gen1,t));
	  if (GEN_has_flags(gen2,FLAG_VARS,GEN_VAR_P)) {
	    MAT_set_i(A,*A_nnz,*A_row);
	    MAT_set_j(A,*A_nnz,GEN_get_index_P(gen2,t));
	    MAT_set_d(A,*A_nnz,-1.);
	    (*A_nnz)++;
	  }
	  else
	    VEC_add_to_entry(b,*A_row,GEN_get_P(gen2,t));
	  (*A_row)++;
	}
      }
    }

    // Update counted flag
    bus_counted[BUS_get_index(bus)*T+t] = TRUE;
  }
}