Пример #1
0
REAL BUS_get_total_reg_gen_Qmin(Bus* bus) {
  Gen* gen;
  REAL Qmin = 0;
  if (!bus)
    return 0;
  for (gen = bus->reg_gen; gen != NULL; gen = GEN_get_reg_next(gen))
    Qmin += GEN_get_Q_min(gen);
  return Qmin;
}
Пример #2
0
void HEUR_PVPQ_apply_step(Heur* h, Constr* clist, Net* net, Branch* br, int t, Vec* var_values) {

  // Local variables
  Vec* f;
  Mat* A;
  Vec* b;
  Bus* bus[2];
  Gen* gen;
  char* bus_counted;
  Heur_PVPQ_Data* data;
  char* reg_flag;
  int bus_index_t[2];
  int k;
  int i;
  Constr* pf;
  Constr* fix;
  REAL v;
  REAL v_set;
  REAL Q;
  REAL Qmax;
  REAL Qmin;
  char switch_flag;
  int j_old;
  int j_new;
  REAL b_new;
  int T;
  int num_buses;

  // Num periods
  T = BRANCH_get_num_periods(br);

  // Num buses
  num_buses = NET_get_num_buses(net);

  // Heur data
  bus_counted = HEUR_get_bus_counted(h);
  data = (Heur_PVPQ_Data*)HEUR_get_data(h);
  reg_flag = data->reg_flag;

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

  // Bus from data
  bus[0] = BRANCH_get_bus_k(br);
  bus_index_t[0] = BUS_get_index(bus[0])*T+t;

  // Bus to data
  bus[1] = BRANCH_get_bus_m(br);
  bus_index_t[1] = BUS_get_index(bus[1])*T+t;

  // Power flow constraints
  for (pf = clist; pf != NULL; pf = CONSTR_get_next(pf)) {
    if (strcmp(CONSTR_get_name(pf),"AC power balance") == 0)
      break;
  }
  if (!pf)
    return;

  // Fix constraints
  for (fix = clist; fix != NULL; fix = CONSTR_get_next(fix)) {
    if (strcmp(CONSTR_get_name(fix),"variable fixing") == 0)
      break;
  }
  if (!fix)
    return;

  // Constr data
  f = CONSTR_get_f(pf);
  A = CONSTR_get_A(fix);
  b = CONSTR_get_b(fix);

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

    if (!bus_counted[bus_index_t[k]] &&                   // not counted
	!BUS_is_slack(bus[k]) &&                          // not slack
	BUS_is_regulated_by_gen(bus[k]) &&                // regulated
	BUS_has_flags(bus[k],FLAG_VARS,BUS_VAR_VMAG) &&   // v mag is variable
	BUS_has_flags(bus[k],FLAG_FIXED,BUS_VAR_VMAG) &&  // v mag is fixed
	GEN_has_flags(BUS_get_reg_gen(bus[k]),FLAG_VARS,GEN_VAR_Q)) { // reg gen Q is variable

      // Voltage magnitude
      v = VEC_get(var_values,BUS_get_index_v_mag(bus[k],t));
      v_set = BUS_get_v_set(bus[k],t);

      // Regulating generator (first one in list of reg gens)
      gen = BUS_get_reg_gen(bus[k]);
      Q = VEC_get(var_values,GEN_get_index_Q(gen,t)); // per unit
      Qmax = GEN_get_Q_max(gen);                      // per unit
      Qmin = GEN_get_Q_min(gen);                      // per unit

      // Switch flag
      switch_flag = FALSE;

      // Currently regulated
      if (reg_flag[bus_index_t[k]]) {

	// Violations
	if (Q > Qmax) {

	  // Set data
	  j_old = BUS_get_index_v_mag(bus[k],t);
	  j_new = GEN_get_index_Q(gen,t);
	  b_new = Qmax;
	  switch_flag = TRUE;
	  reg_flag[bus_index_t[k]] = FALSE;

	  // Update vector of var values
	  while (gen) {
	    if (GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q))
	      VEC_set(var_values,GEN_get_index_Q(gen,t),GEN_get_Q_max(gen));
	    gen = GEN_get_reg_next(gen);
	  }
	}
	else if (Q < Qmin) {

	  // Set data
	  j_old = BUS_get_index_v_mag(bus[k],t);
	  j_new = GEN_get_index_Q(gen,t);
	  b_new = Qmin;
	  switch_flag = TRUE;
	  reg_flag[bus_index_t[k]] = FALSE;

	  // Update vector of var values
	  while (gen) {
	    if (GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q))
	      VEC_set(var_values,GEN_get_index_Q(gen,t),GEN_get_Q_min(gen));
	    gen = GEN_get_reg_next(gen);
	  }
	}
      }

      // Previously regulated
      else {

	// Q at Qmin and v < v_set
	if (fabs(Q-Qmin) < fabs(Q-Qmax) && v < v_set) {

	  Q = Q - VEC_get(f,BUS_get_index_Q(GEN_get_bus(gen))+t*2*num_buses); // per unit (see constr_PF)

	  if (Q >= Qmax) {

	    // Set data
	    j_old = GEN_get_index_Q(gen,t);
	    j_new = GEN_get_index_Q(gen,t);
	    b_new = Qmax;
	    switch_flag = TRUE;

	    // Update vector of var values
	    while (gen) {
	      if (GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q))
		VEC_set(var_values,GEN_get_index_Q(gen,t),GEN_get_Q_max(gen));
	      gen = GEN_get_reg_next(gen);
	    }
	  }
	  else if (Qmin < Q && Q < Qmax) {

	    // Set data
	    j_old = GEN_get_index_Q(gen,t);
	    j_new = BUS_get_index_v_mag(bus[k],t);
	    b_new = v_set;
	    switch_flag = TRUE;
	    reg_flag[bus_index_t[k]] = TRUE;

	    // Udpate vector of var values
	    VEC_set(var_values,j_new,b_new);
	  }
	}

	// Q at Qmax and v > v_set
	else if (fabs(Q-Qmax) < fabs(Q-Qmin) && v > v_set) {

	  Q = Q - VEC_get(f,BUS_get_index_Q(GEN_get_bus(gen))+t*2*num_buses); // per unit (see constr_PF)

	  if (Q <= Qmin) {

	    // Set data
	    j_old = GEN_get_index_Q(gen,t);
	    j_new = GEN_get_index_Q(gen,t);
	    b_new = Qmin;
	    switch_flag = TRUE;

	    // Update vector of var values
	    while (gen) {
	      if (GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q))
		VEC_set(var_values,GEN_get_index_Q(gen,t),GEN_get_Q_min(gen));
	      gen = GEN_get_reg_next(gen);
	    }
	  }
	  else if (Qmin < Q && Q < Qmax) {

	    // Set data
	    j_old = GEN_get_index_Q(gen,t);
	    j_new = BUS_get_index_v_mag(bus[k],t);
	    b_new = v_set;
	    switch_flag = TRUE;
	    reg_flag[bus_index_t[k]] = TRUE;

	    // Udpate vector of var values
	    VEC_set(var_values,j_new,b_new);
	  }
	}
      }

      // Update fix constraints
      if (switch_flag) {
	for (i = 0; i < MAT_get_nnz(A); i++) {
	  if (MAT_get_j(A,i) == j_old)
	    MAT_set_d(A,i,0.);
	  if (MAT_get_j(A,i) == j_new) {
	    MAT_set_d(A,i,1.);
	    VEC_set(b,MAT_get_i(A,i),b_new);
	  }
	}
      }
    }

    // Update counted flag
    bus_counted[bus_index_t[k]] = TRUE;
  }
}
Пример #3
0
void FUNC_REG_PQ_eval_step(Func* f, Branch* br, int t, Vec* var_values) {

  // Local variables
  Bus* bus[2];
  Gen* gen;
  int bus_index_t[2];
  char* bus_counted;
  REAL* phi;
  REAL* gphi;
  REAL Qmid;
  REAL Pmid;
  REAL P;
  REAL Q;
  REAL dP;
  REAL dQ;
  int k;
  int T;
 
  // Num periods
  T = BRANCH_get_num_periods(br);

  // Constr data
  phi = FUNC_get_phi_ptr(f);
  gphi = VEC_get_data(FUNC_get_gphi(f));
  bus_counted = FUNC_get_bus_counted(f);

  // Check pointers
  if (!phi || !gphi || !bus_counted)
    return;

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

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

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

    if (!bus_counted[bus_index_t[k]]) {
      
      // Generators
      for (gen = BUS_get_gen(bus[k]); gen != NULL; gen = GEN_get_next(gen)) {

	// Mid value
	Qmid = (GEN_get_Q_max(gen)+GEN_get_Q_min(gen))/2.; // p.u.
	Pmid = (GEN_get_P_max(gen)+GEN_get_P_min(gen))/2.; // p.u.

	// Normalization factor
	dQ = GEN_get_Q_max(gen)-GEN_get_Q_min(gen); // p.u.
	if (dQ < FUNC_REG_PQ_PARAM)
	  dQ = FUNC_REG_PQ_PARAM;
	dP = GEN_get_P_max(gen)-GEN_get_P_min(gen); // p.u.
	if (dP < FUNC_REG_PQ_PARAM)
	  dP = FUNC_REG_PQ_PARAM;
	
	if (GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q)) { // Q var
	  
	  // Value
	  Q = VEC_get(var_values,GEN_get_index_Q(gen,t));

	  // phi
	  (*phi) += 0.5*pow((Q-Qmid)/dQ,2.);
	  
	  // gphi
	  gphi[GEN_get_index_Q(gen,t)] = (Q-Qmid)/(dQ*dQ);
	}
	else {
	  
	  // Value
	  Q = GEN_get_Q(gen,t);

	  // phi
	  (*phi) += 0.5*pow((Q-Qmid)/dQ,2.); 
	}

	if (GEN_has_flags(gen,FLAG_VARS,GEN_VAR_P)) { // P var
	  	
	  // Value
	  P = VEC_get(var_values,GEN_get_index_P(gen,t));

	  // phi
	  (*phi) += 0.5*pow((P-Pmid)/dP,2.);

	  // gphi
	  gphi[GEN_get_index_P(gen,t)] = (P-Pmid)/(dP*dP);
	}
	else {

	  // Value
	  P = GEN_get_P(gen,t);

	  // phi
	  (*phi) += 0.5*pow((P-Pmid)/dP,2.);
	}
      }
    }
    
    // Update counted flag
    bus_counted[bus_index_t[k]] = TRUE;
  }
}
Пример #4
0
void FUNC_REG_PQ_analyze_step(Func* f, Branch* br, int t) {

  // Local variables
  Bus* bus[2];
  Gen* gen;
  int bus_index_t[2];
  int* Hcounter;
  char* bus_counted;
  Mat* H;
  int k;
  REAL dv;
  int T;
 
  // Num periods
  T = BRANCH_get_num_periods(br);

  // Constr data
  H = FUNC_get_Hphi(f);
  Hcounter = FUNC_get_Hcounter_ptr(f);
  bus_counted = FUNC_get_bus_counted(f);

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

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

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

  // Buses
  for (k = 0; k < 2; k++) {
    
    if (!bus_counted[bus_index_t[k]]) {
      
      // Generators
      for (gen = BUS_get_gen(bus[k]); gen != NULL; gen = GEN_get_next(gen)) {
	
	if (GEN_has_flags(gen,FLAG_VARS,GEN_VAR_Q)) { // Q var

	  dv = GEN_get_Q_max(gen)-GEN_get_Q_min(gen); // p.u.
	  if (dv < FUNC_REG_PQ_PARAM)
	    dv = FUNC_REG_PQ_PARAM;

	  MAT_set_i(H,*Hcounter,GEN_get_index_Q(gen,t));
	  MAT_set_j(H,*Hcounter,GEN_get_index_Q(gen,t));
	  MAT_set_d(H,*Hcounter,1./(dv*dv));
	  (*Hcounter)++;
	}

	if (GEN_has_flags(gen,FLAG_VARS,GEN_VAR_P)) { // P var
	  
	  dv = GEN_get_P_max(gen)-GEN_get_P_min(gen); // p.u.
	  if (dv < FUNC_REG_PQ_PARAM)
	    dv = FUNC_REG_PQ_PARAM;

	  MAT_set_i(H,*Hcounter,GEN_get_index_P(gen,t));
	  MAT_set_j(H,*Hcounter,GEN_get_index_P(gen,t));
	  MAT_set_d(H,*Hcounter,1./(dv*dv));
	  (*Hcounter)++;
	}
      }  
    }
    
    // Update counted flag
    bus_counted[bus_index_t[k]] = TRUE;
  }  
}
Пример #5
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;    
  }  
}
Пример #6
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;
  }
}