Пример #1
0
int main(int argc, char **argv) {

    float old_loss;
    float new_loss;
    float epsilon;
    float lambda;

    epsilon = .1;
    lambda = .00000001;

    build_neural_net_from_cmds(argc, argv);

    samples = get_samples_from_file("training_data/mnist_test.csv", 3000, 784);   

    old_loss = calculate_loss(nn, samples);

printf("%f\n", old_loss);

    new_loss = old_loss + 1;

    while ((old_loss - new_loss) * (old_loss - new_loss) > epsilon) {

        old_loss = new_loss;

        train_neural_net(nn, samples, lambda);

        new_loss = calculate_loss(nn, samples); 

        printf("%f, %f\n", new_loss, calculate_percent_predicted_correctly(nn, samples));

    }
 

}
double my_microturbineImpl::microturbine_sync(double CircuitA_V_Out_re, double CircuitA_V_Out_im, double CircuitB_V_Out_re, double CircuitB_V_Out_im, double CircuitC_V_Out_re, double CircuitC_V_Out_im, double LineA_V_Out_re, double LineA_V_Out_im, double LineB_V_Out_re, double LineB_V_Out_im, double LineC_V_Out_re, double LineC_V_Out_im)
{
  //gather V_Out for each phase
	//gather I_Out for each phase
	//gather VA_Out for each phase
	//gather Q_Out
	//gather S_Out
	//gather Pf_Out

	phaseA_V_Out.Re() = CircuitA_V_Out_re;
	phaseB_V_Out.Re() = CircuitB_V_Out_re;
	phaseC_V_Out.Re()= CircuitC_V_Out_re;

	phaseA_V_Out.Im() = CircuitA_V_Out_im;
	phaseB_V_Out.Im() = CircuitB_V_Out_im;
	phaseC_V_Out.Im()= CircuitC_V_Out_im;

	phaseA_I_Out.Re() = LineA_V_Out_re;
	phaseB_I_Out.Re() = LineB_V_Out_re;
	phaseC_I_Out.Re()= LineC_V_Out_re;

       phaseA_I_Out.Im() = LineA_V_Out_im;
       	phaseB_I_Out.Im() = LineB_V_Out_im;
       	phaseC_I_Out.Im()= LineC_V_Out_im;



	power_A_Out = (~phaseA_I_Out) * phaseA_V_Out;
	power_B_Out = (~phaseB_I_Out) * phaseB_V_Out;
	power_C_Out = (~phaseC_I_Out) * phaseC_V_Out;
printf("%f %f",phaseA_I_Out.Re(),phaseA_V_Out.Im());


	VA_Out = power_A_Out + power_B_Out + power_C_Out;

	E_A_Internal = determine_source_voltage(phaseA_V_Out, Rinternal, Rload);
	E_B_Internal = determine_source_voltage(phaseB_V_Out, Rinternal, Rload);
	E_C_Internal = determine_source_voltage(phaseC_V_Out, Rinternal, Rload);





	frequency = determine_frequency(VA_Out);



	double loss = calculate_loss(frequency);
	efficiency = 1 - loss;
	Heat_Out = determine_heat(VA_Out, loss);
	Fuel_Used = Heat_Out + VA_Out.Mag();



return VA_Out.Re();

}
Пример #3
0
/* Sync is called when the clock needs to advance on the bottom-up pass */
TIMESTAMP microturbine::sync(TIMESTAMP t0, TIMESTAMP t1) 
{
	//gather V_Out for each phase
	//gather I_Out for each phase
	//gather VA_Out for each phase
	//gather Q_Out
	//gather S_Out
	//gather Pf_Out

	phaseA_V_Out = pCircuit_V_A[0];
	phaseB_V_Out = pCircuit_V_B[0];
	phaseC_V_Out = pCircuit_V_C[0];

	phaseA_I_Out = pLine_I_A[0];
	phaseB_I_Out = pLine_I_B[0];
	phaseC_I_Out = pLine_I_C[0];

	gl_verbose("microturbine sync: phaseA_V_Out from parent is: (%f , %f)", phaseA_V_Out.Re(), phaseA_V_Out.Im());
	gl_verbose("microturbine sync: phaseB_V_Out from parent is: (%f , %f)", phaseB_V_Out.Re(), phaseB_V_Out.Im());
	gl_verbose("microturbine sync: phaseC_V_Out from parent is: (%f , %f)", phaseC_V_Out.Re(), phaseC_V_Out.Im());

	gl_verbose("microturbine sync: phaseA_I_Out from parent is: (%f , %f)", phaseA_I_Out.Re(), phaseA_I_Out.Im());
	gl_verbose("microturbine sync: phaseB_I_Out from parent is: (%f , %f)", phaseB_I_Out.Re(), phaseB_I_Out.Im());
	gl_verbose("microturbine sync: phaseC_I_Out from parent is: (%f , %f)", phaseC_I_Out.Re(), phaseC_I_Out.Im());

	power_A_Out = (~phaseA_I_Out) * phaseA_V_Out;
	power_B_Out = (~phaseB_I_Out) * phaseB_V_Out;
	power_C_Out = (~phaseC_I_Out) * phaseC_V_Out;


	gl_verbose("microturbine sync: power_A_Out from parent is: (%f , %f)", power_A_Out.Re(), power_A_Out.Im());
	gl_verbose("microturbine sync: power_B_Out from parent is: (%f , %f)", power_B_Out.Re(), power_B_Out.Im());
	gl_verbose("microturbine sync: power_C_Out from parent is: (%f , %f)", power_C_Out.Re(), power_C_Out.Im());


	VA_Out = power_A_Out + power_B_Out + power_C_Out;

	E_A_Internal = determine_source_voltage(phaseA_V_Out, Rinternal, Rload);
	E_B_Internal = determine_source_voltage(phaseB_V_Out, Rinternal, Rload);
	E_C_Internal = determine_source_voltage(phaseC_V_Out, Rinternal, Rload);


	
	gl_verbose("microturbine sync: E_A_Internal calc is: (%f , %f)", E_A_Internal.Re(), E_A_Internal.Im());
	gl_verbose("microturbine sync: E_B_Internal calc is: (%f , %f)", E_B_Internal.Re(), E_B_Internal.Im());
	gl_verbose("microturbine sync: E_C_Internal calc is: (%f , %f)", E_C_Internal.Re(), E_C_Internal.Im());


	frequency = determine_frequency(VA_Out);

	gl_verbose("microturbine sync: determined frequency is: %f", frequency);

	if(frequency > Max_Frequency){
		throw ("the frequency asked for from the microturbine is too high!");
	}
	if(frequency < Min_Frequency){
		throw ("the frequency asked for from the microturbine is too low!");
	}


	double loss = calculate_loss(frequency);
	efficiency = 1 - loss;
	Heat_Out = determine_heat(VA_Out, loss);
	Fuel_Used = Heat_Out + VA_Out.Mag();
	
	gl_verbose("microturbine sync: about to exit");

return TS_NEVER;
}
Пример #4
0
TIMESTAMP inverter::sync(TIMESTAMP t0, TIMESTAMP t1) 
{
	if (*NR_mode == false)
	{
		phaseA_V_Out = pCircuit_V[0];	//Syncs the meter parent to the generator.
		phaseB_V_Out = pCircuit_V[1];
		phaseC_V_Out = pCircuit_V[2];

		internal_losses = 1 - calculate_loss(Rtotal, Ltotal, Ctotal, DC, AC);
		//gl_verbose("inverter sync: internal losses are: %f", 1 - internal_losses);
		frequency_losses = 1 - calculate_frequency_loss(output_frequency, Rtotal,Ltotal, Ctotal);
		//gl_verbose("inverter sync: frequency losses are: %f", 1 - frequency_losses);

		
		switch(gen_mode_v)
		{
			case CONSTANT_PF:
			{
				VA_In = V_In * ~ I_In; //DC

				// need to differentiate between different pulses...
			
				VA_Out = VA_In * efficiency * internal_losses * frequency_losses;
				//losses = VA_Out * Rtotal / (Rtotal + Rload);
				//VA_Out = VA_Out * Rload / (Rtotal + Rload);

				if (number_of_phases_out == 4)  //Triplex-line -> Assume it's only across the 240 V for now.
				{
					power_A = complex(VA_Out.Mag()*abs(power_factor),power_factor/abs(power_factor)*VA_Out.Mag()*sin(acos(power_factor)));
					if (phaseA_V_Out.Mag() != 0.0)
						phaseA_I_Out = ~(power_A / phaseA_V_Out);
					else
						phaseA_I_Out = complex(0.0,0.0);

					*pLine12 += -phaseA_I_Out;

					//Update this value for later removal
					last_current[3] = -phaseA_I_Out;
					
					//Get rid of these for now
					//complex phaseA_V_Internal = filter_voltage_impact_source(phaseA_I_Out, phaseA_V_Out);
					//phaseA_I_Out = filter_current_impact_out(phaseA_I_Out, phaseA_V_Internal);
				}
				else if (number_of_phases_out == 3)
				{
					power_A = power_B = power_C = complex(VA_Out.Mag()*abs(power_factor),power_factor/abs(power_factor)*VA_Out.Mag()*sin(acos(power_factor)))/3;
					if (phaseA_V_Out.Mag() != 0.0)
						phaseA_I_Out = ~(power_A / phaseA_V_Out); // /sqrt(2.0);
					else
						phaseA_I_Out = complex(0.0,0.0);
					if (phaseB_V_Out.Mag() != 0.0)
						phaseB_I_Out = ~(power_B / phaseB_V_Out); // /sqrt(2.0);
					else
						phaseB_I_Out = complex(0.0,0.0);
					if (phaseC_V_Out.Mag() != 0.0)
						phaseC_I_Out = ~(power_C / phaseC_V_Out); // /sqrt(2.0);
					else
						phaseC_I_Out = complex(0.0,0.0);

					pLine_I[0] += -phaseA_I_Out;
					pLine_I[1] += -phaseB_I_Out;
					pLine_I[2] += -phaseC_I_Out;

					//Update this value for later removal
					last_current[0] = -phaseA_I_Out;
					last_current[1] = -phaseB_I_Out;
					last_current[2] = -phaseC_I_Out;

					//complex phaseA_V_Internal = filter_voltage_impact_source(phaseA_I_Out, phaseA_V_Out);
					//complex phaseB_V_Internal = filter_voltage_impact_source(phaseB_I_Out, phaseB_V_Out);
					//complex phaseC_V_Internal = filter_voltage_impact_source(phaseC_I_Out, phaseC_V_Out);

					//phaseA_I_Out = filter_current_impact_out(phaseA_I_Out, phaseA_V_Internal);
					//phaseB_I_Out = filter_current_impact_out(phaseB_I_Out, phaseB_V_Internal);
					//phaseC_I_Out = filter_current_impact_out(phaseC_I_Out, phaseC_V_Internal);
				}
				else if(number_of_phases_out == 2)
				{
					OBJECT *obj = OBJECTHDR(this);
					node *par = OBJECTDATA(obj->parent, node);

					if (par->has_phase(PHASE_A) && phaseA_V_Out.Mag() != 0)
					{
						power_A = complex(VA_Out.Mag()*abs(power_factor),power_factor/abs(power_factor)*VA_Out.Mag()*sin(acos(power_factor)))/2;;
						phaseA_I_Out = ~(power_A / phaseA_V_Out);
					}
					else 
						phaseA_I_Out = complex(0,0);

					if (par->has_phase(PHASE_B) && phaseB_V_Out.Mag() != 0)
					{
						power_B = complex(VA_Out.Mag()*abs(power_factor),power_factor/abs(power_factor)*VA_Out.Mag()*sin(acos(power_factor)))/2;;
						phaseB_I_Out = ~(power_B / phaseB_V_Out);
					}
					else 
						phaseB_I_Out = complex(0,0);

					if (par->has_phase(PHASE_C) && phaseC_V_Out.Mag() != 0)
					{
						power_C = complex(VA_Out.Mag()*abs(power_factor),power_factor/abs(power_factor)*VA_Out.Mag()*sin(acos(power_factor)))/2;;
						phaseC_I_Out = ~(power_C / phaseC_V_Out);
					}
					else 
						phaseC_I_Out = complex(0,0);

					pLine_I[0] += -phaseA_I_Out;
					pLine_I[1] += -phaseB_I_Out;
					pLine_I[2] += -phaseC_I_Out;

					//Update this value for later removal
					last_current[0] = -phaseA_I_Out;
					last_current[1] = -phaseB_I_Out;
					last_current[2] = -phaseC_I_Out;

				}
				else if(number_of_phases_out == 1)
				{
					if(phaseA_V_Out.Mag() != 0)
					{
						power_A = complex(VA_Out.Mag()*abs(power_factor),power_factor/abs(power_factor)*VA_Out.Mag()*sin(acos(power_factor)));
						phaseA_I_Out = ~(power_A / phaseA_V_Out); 
						//complex phaseA_V_Internal = filter_voltage_impact_source(phaseA_I_Out, phaseA_V_Out);
						//phaseA_I_Out = filter_current_impact_out(phaseA_I_Out, phaseA_V_Internal);
					}
					else if(phaseB_V_Out.Mag() != 0)
					{
						power_B = complex(VA_Out.Mag()*abs(power_factor),power_factor/abs(power_factor)*VA_Out.Mag()*sin(acos(power_factor)));
						phaseB_I_Out = ~(power_B / phaseB_V_Out); 
						//complex phaseB_V_Internal = filter_voltage_impact_source(phaseB_I_Out, phaseB_V_Out);
						//phaseB_I_Out = filter_current_impact_out(phaseB_I_Out, phaseB_V_Internal);
					}
					else if(phaseC_V_Out.Mag() != 0)
					{
						power_C = complex(VA_Out.Mag()*abs(power_factor),power_factor/abs(power_factor)*VA_Out.Mag()*sin(acos(power_factor)));
						phaseC_I_Out = ~(power_C / phaseC_V_Out); 
						//complex phaseC_V_Internal = filter_voltage_impact_source(phaseC_I_Out, phaseC_V_Out);
						//phaseC_I_Out = filter_current_impact_out(phaseC_I_Out, phaseC_V_Internal);
					}
					else
					{
						gl_warning("None of the phases specified have voltages!");
						phaseA_I_Out = phaseB_I_Out = phaseC_I_Out = complex(0.0,0.0);
					}
					pLine_I[0] += -phaseA_I_Out;
					pLine_I[1] += -phaseB_I_Out;
					pLine_I[2] += -phaseC_I_Out;

					//Update this value for later removal
					last_current[0] = -phaseA_I_Out;
					last_current[1] = -phaseB_I_Out;
					last_current[2] = -phaseC_I_Out;

				}
				else
				{
					throw ("The number of phases given is unsupported.");
				}
				return TS_NEVER;
			}
				break;
			case CONSTANT_PQ:
			{
				GL_THROW("Constant PQ mode not supported at this time");
				/* TROUBLESHOOT
				This will be worked on at a later date and is not yet correctly implemented.
				*/
				gl_verbose("inverter sync: constant pq");
				//TODO
				//gather V_Out for each phase
				//gather V_In (DC) from line -- can not gather V_In, for now set equal to V_Out
				//P_Out is either set or input from elsewhere
				//Q_Out is either set or input from elsewhere
				//Gather Rload

				if(parent_string = "meter")
				{
					VA_Out = complex(P_Out,Q_Out);
				}
				else if (parent_string = "triplex_meter")
				{
					VA_Out = complex(P_Out,Q_Out);
				}
				else
				{
					phaseA_I_Out = pLine_I[0];
					phaseB_I_Out = pLine_I[1];
					phaseC_I_Out = pLine_I[2];

					//Erm, there's no good way to handle this from a "multiply attached" point of view.
					//TODO: Think about how to do this if the need arrises

					VA_Out = phaseA_V_Out * (~ phaseA_I_Out) + phaseB_V_Out * (~ phaseB_I_Out) + phaseC_V_Out * (~ phaseC_I_Out);
				}

				pf_out = P_Out/VA_Out.Mag();
				
				//VA_Out = VA_In * efficiency * internal_losses;

				if (number_of_phases_out == 3)
				{
					power_A = power_B = power_C = VA_Out /3;
					phaseA_I_Out = (power_A / phaseA_V_Out); // /sqrt(2.0);
					phaseB_I_Out = (power_B / phaseB_V_Out); // /sqrt(2.0);
					phaseC_I_Out = (power_C / phaseC_V_Out); // /sqrt(2.0);

					phaseA_I_Out = ~ phaseA_I_Out;
					phaseB_I_Out = ~ phaseB_I_Out;
					phaseC_I_Out = ~ phaseC_I_Out;

				}
				else if(number_of_phases_out == 1)
				{
					if(phaseAOut)
					{
						power_A = VA_Out;
						phaseA_I_Out = (power_A / phaseA_V_Out); // /sqrt(2);
						phaseA_I_Out = ~ phaseA_I_Out;
					}
					else if(phaseBOut)
					{
						power_B = VA_Out;
						phaseB_I_Out = (power_B / phaseB_V_Out);  // /sqrt(2);
						phaseB_I_Out = ~ phaseB_I_Out;
					}
					else if(phaseCOut)
					{
						power_C = VA_Out;
						phaseC_I_Out = (power_C / phaseC_V_Out); // /sqrt(2);
						phaseC_I_Out = ~ phaseC_I_Out;
					}
					else
					{
						throw ("none of the phases have voltages!");
					}
				}
				else
				{
					throw ("unsupported number of phases");
				}

				VA_In = VA_Out / (efficiency * internal_losses * frequency_losses);
				losses = VA_Out * (1 - (efficiency * internal_losses * frequency_losses));

				//V_In = complex(0,0);
				//
				////is there a better way to do this?
				//if(phaseAOut){
				//	V_In += abs(phaseA_V_Out.Re());
				//}
				//if(phaseBOut){
				//	V_In += abs(phaseB_V_Out.Re());
				//}
				//if(phaseCOut){
				//	V_In += abs(phaseC_V_Out.Re());
				//}else{
				//	throw ("none of the phases have voltages!");
				//}

				V_In = Vdc;



				I_In = VA_In / V_In;
				I_In = ~I_In;

				V_In = filter_voltage_impact_source(I_In, V_In);
				I_In = filter_current_impact_source(I_In, V_In);

				gl_verbose("Inverter sync: V_In asked for by inverter is: (%f , %f)", V_In.Re(), V_In.Im());
				gl_verbose("Inverter sync: I_In asked for by inverter is: (%f , %f)", I_In.Re(), I_In.Im());


				pLine_I[0] += phaseA_I_Out;
				pLine_I[1] += phaseB_I_Out;
				pLine_I[2] += phaseC_I_Out;

				//Update this value for later removal
				last_current[0] = phaseA_I_Out;
				last_current[1] = phaseB_I_Out;
				last_current[2] = phaseC_I_Out;

				return TS_NEVER;
			}
				break;
			case CONSTANT_V:
			{
				GL_THROW("Constant V mode not supported at this time");
				/* TROUBLESHOOT
				This will be worked on at a later date and is not yet correctly implemented.
				*/
				gl_verbose("inverter sync: constant v");
				bool changed = false;
				
				//TODO
				//Gather V_Out
				//Gather VA_Out
				//Gather Rload
				if(phaseAOut)
				{
					if (phaseA_V_Out.Re() < (V_Set_A - margin))
					{
						phaseA_I_Out = phaseA_I_Out_prev + I_step_max/2;
						changed = true;
					}
					else if (phaseA_V_Out.Re() > (V_Set_A + margin))
					{
						phaseA_I_Out = phaseA_I_Out_prev - I_step_max/2;
						changed = true;
					}
					else
					{
						changed = false;
					}
				}
				if (phaseBOut)
				{
					if (phaseB_V_Out.Re() < (V_Set_B - margin))
					{
						phaseB_I_Out = phaseB_I_Out_prev + I_step_max/2;
						changed = true;
					}
					else if (phaseB_V_Out.Re() > (V_Set_B + margin))
					{
						phaseB_I_Out = phaseB_I_Out_prev - I_step_max/2;
						changed = true;
					}
					else
					{
						changed = false;
					}
				}
				if (phaseCOut)
				{
					if (phaseC_V_Out.Re() < (V_Set_C - margin))
					{
						phaseC_I_Out = phaseC_I_Out_prev + I_step_max/2;
						changed = true;
					}
					else if (phaseC_V_Out.Re() > (V_Set_C + margin))
					{
						phaseC_I_Out = phaseC_I_Out_prev - I_step_max/2;
						changed = true;
					}
					else
					{
						changed = false;
					}
				}
				
				power_A = (~phaseA_I_Out) * phaseA_V_Out;
				power_B = (~phaseB_I_Out) * phaseB_V_Out;
				power_C = (~phaseC_I_Out) * phaseC_V_Out;

				//check if inverter is overloaded -- if so, cap at max power
				if (((power_A + power_B + power_C) > Rated_kVA) ||
					((power_A.Re() + power_B.Re() + power_C.Re()) > Max_P) ||
					((power_A.Im() + power_B.Im() + power_C.Im()) > Max_Q))
				{
					VA_Out = Rated_kVA / number_of_phases_out;
					//if it's maxed out, don't ask for the simulator to re-call
					changed = false;
					if(phaseAOut)
					{
						phaseA_I_Out = VA_Out / phaseA_V_Out;
						phaseA_I_Out = (~phaseA_I_Out);
					}
					if(phaseBOut)
					{
						phaseB_I_Out = VA_Out / phaseB_V_Out;
						phaseB_I_Out = (~phaseB_I_Out);
					}
					if(phaseCOut)
					{
						phaseC_I_Out = VA_Out / phaseC_V_Out;
						phaseC_I_Out = (~phaseC_I_Out);
					}
				}
				
				//check if power is negative for some reason, should never be
				if(power_A < 0)
				{
					power_A = 0;
					phaseA_I_Out = 0;
					throw("phaseA power is negative!");
				}
				if(power_B < 0)
				{
					power_B = 0;
					phaseB_I_Out = 0;
					throw("phaseB power is negative!");
				}
				if(power_C < 0)
				{
					power_C = 0;
					phaseC_I_Out = 0;
					throw("phaseC power is negative!");
				}

				VA_In = VA_Out / (efficiency * internal_losses * frequency_losses);
				losses = VA_Out * (1 - (efficiency * internal_losses * frequency_losses));

				//V_In = complex(0,0);
				//
				////is there a better way to do this?
				//if(phaseAOut){
				//	V_In += abs(phaseA_V_Out.Re());
				//}
				//if(phaseBOut){
				//	V_In += abs(phaseB_V_Out.Re());
				//}
				//if(phaseCOut){
				//	V_In += abs(phaseC_V_Out.Re());
				//}else{
				//	throw ("none of the phases have voltages!");
				//}

				V_In = Vdc;

				I_In = VA_In / V_In;
				I_In  = ~I_In;
				
				gl_verbose("Inverter sync: I_In asked for by inverter is: (%f , %f)", I_In.Re(), I_In.Im());

				V_In = filter_voltage_impact_source(I_In, V_In);
				I_In = filter_current_impact_source(I_In, V_In);

				//TODO: check P and Q components to see if within bounds

				if(changed)
				{
					pLine_I[0] += phaseA_I_Out;
					pLine_I[1] += phaseB_I_Out;
					pLine_I[2] += phaseC_I_Out;
					
					//Update this value for later removal
					last_current[0] = phaseA_I_Out;
					last_current[1] = phaseB_I_Out;
					last_current[2] = phaseC_I_Out;

					TIMESTAMP t2 = t1 + 10 * 60 * TS_SECOND;
					return t2;
				}
				else
				{
					pLine_I[0] += phaseA_I_Out;
					pLine_I[1] += phaseB_I_Out;
					pLine_I[2] += phaseC_I_Out;

					//Update this value for later removal
					last_current[0] = phaseA_I_Out;
					last_current[1] = phaseB_I_Out;
					last_current[2] = phaseC_I_Out;

					return TS_NEVER;
				}
			}
				break;
			case SUPPLY_DRIVEN: {
				GL_THROW("SUPPLY_DRIVEN mode for inverters not supported at this time");
			}
			default:
			{
				pLine_I[0] += phaseA_I_Out;
				pLine_I[1] += phaseB_I_Out;
				pLine_I[2] += phaseC_I_Out;

				//Update this value for later removal
				last_current[0] = phaseA_I_Out;
				last_current[1] = phaseB_I_Out;
				last_current[2] = phaseC_I_Out;

				return TS_NEVER;
			}
				break;
		}
		if (number_of_phases_out == 4)
		{
			*pLine12 += phaseA_I_Out;

			//Update this value for later removal
			last_current[3] = phaseA_I_Out;
		}
		else
		{
			pLine_I[0] += phaseA_I_Out;
			pLine_I[1] += phaseB_I_Out;
			pLine_I[2] += phaseC_I_Out;

			//Update this value for later removal
			last_current[0] = phaseA_I_Out;
			last_current[1] = phaseB_I_Out;
			last_current[2] = phaseC_I_Out;

		}

		return TS_NEVER;
	}
	else
	{
		if (number_of_phases_out == 4)
		{
			*pLine12 += last_current[3];
		}
		else
		{
			pLine_I[0] += last_current[0];
			pLine_I[1] += last_current[1];
			pLine_I[2] += last_current[2];
		}
		return TS_NEVER;
	}
}
Пример #5
0
int LatentRelevance::line_search(float gradient_w0, float gradient_wcos, float* gradient_weights, float& loss)
{
	const float C1 = 1e-4;
	const float backtrack_ratio = 0.2f;

	// Save original variable values
	float orig_w0 = w0;
	float orig_wcos = w_cos;
	float orig_loss = loss;

	// Save original weights
	float* orig_weights = NULL;
	float* weights = NULL;
	int weights_size = 0;
	if (factorize_mode == 0) {
		weights_size = vector_size * vector_size;
		weights = w;
	}
	else {
		weights_size = 2 * vector_size * k;
		weights = v;
	}	
	orig_weights = new float[weights_size];
	memcpy(orig_weights, weights, weights_size * sizeof(float));

	const float GRAD_SHRINK_FACTOR = 0.1f;
	gradient_w0 /= data_num * GRAD_SHRINK_FACTOR;
	gradient_wcos /= data_num * GRAD_SHRINK_FACTOR;

	// For steepest gradient descent, descent direction is -gradient
	float dir_grad = 0.0f;
	dir_grad -= gradient_w0 * gradient_w0 + gradient_wcos * gradient_wcos;
	for (int i = 0; i < weights_size; ++i) {
		dir_grad -= gradient_weights[i] * gradient_weights[i];
	}

	// Set initial step_size
	float step_size = learn_rate;
//	if (pre_loss_step < -1e-6) {
//		step_size = pre_loss_step / dir_grad;
//	}

	// Backtracking Line Search
	while (step_size > 1e-9) {
		// Get next point
		w0 = orig_w0 - step_size * gradient_w0;
		w_cos = orig_wcos - step_size * gradient_wcos;
//        printf("%f\t%f\t%f\t%f\n", w0, w_cos, gradient_w0, gradient_wcos);
//        getchar();
		
        for (int i = 0; i < weights_size; ++i) {
			weights[i] = orig_weights[i] - step_size * gradient_weights[i];
		}

		loss = calculate_loss();
//		printf("%f\t%f\t%F\t%f\t%f\n", loss, orig_loss, orig_loss + C1 * step_size * dir_grad, step_size, dir_grad);
//      getchar();
        
		// Wolfe condition
		if (loss <= orig_loss + C1 * step_size * dir_grad) {
			pre_loss_step = step_size * dir_grad;
			break;
		}

		step_size *= backtrack_ratio;
	}

	// Back to original point if no avaliable step_size
	if (loss >= orig_loss) {
		w0 = orig_w0;
		w_cos = orig_wcos;
		memcpy(weights, orig_weights, weights_size * sizeof(float));
		loss = orig_loss;
	}

	delete orig_weights;
	orig_weights = NULL;

	return 0;
}
Пример #6
0
int LatentRelevance::run_gd()
{
	int iter_num = 1;
	float loss = 0.0f;
	loss = calculate_loss();

	// Save the gradients of weights for line search
	float* gradient_weights = NULL;
	if (factorize_mode == 0) {
		gradient_weights = new float[vector_size*vector_size];
	}
	else {
		gradient_weights = new float[2*vector_size*k];
	}

	printf("------------------------------------------------------------------------\n");
	printf("Iteration Process... [%d iterations in total]\n", MAX_STOP_ITER_NUM);
	printf("------------------------------------------------------------------------\n");
	
	// Iteration
	while (loss > MIN_STOP_LOSS * data_num && iter_num <= MAX_STOP_ITER_NUM) {
		// Calculate gradients of w0 and w_cos
		float gradient_w0 = calculate_gradient_w0();
		float gradient_wcos = calculate_gradient_wcos();

		// calculate gradients w or v
		if (factorize_mode == 0) {
			for (int i = 0; i < vector_size; ++i) {
				for (int j = 0; j < vector_size; ++j) {
					int index = i * vector_size + j;
					gradient_weights[index] = calculate_gradient_w(i, j);
				}
			}
		}
		else if (factorize_mode == 1) {
            // Store intermediate gradient variables
            float* temp_gd1 = new float[data_num*k];
            float* temp_gd2 = new float[data_num*k];
            
			for (int j = 0; j < k; ++j) {
                // Pre-calculate temp_gd
                for (int m = 0; m < data_num; ++m) {
                    int index_temp_gd = j * data_num + m;
                    temp_gd1[index_temp_gd] = 0;
                    temp_gd2[index_temp_gd] = 0;
					
                    for (int i = 0; i < vector_size; ++i) {
						temp_gd1[index_temp_gd] += v[i*k+j] * m_data[m].x[i];
					}

					for (int i = vector_size; i < 2 * vector_size; ++i) {
						temp_gd2[index_temp_gd] += v[i*k+j] * m_data[m].x[i];
					}
				}     

                for (int i = 0; i < 2 * vector_size; ++i) {
                    int index = i * k + j;
                    if (i < vector_size) {
                        gradient_weights[index] = calculate_gradient_v(i, j, temp_gd2);
                    }
                    else {
                        gradient_weights[index] = calculate_gradient_v(i, j, temp_gd1);
                    }
                }
			}

			delete temp_gd1;
			temp_gd1 = NULL;
			delete temp_gd2;
			temp_gd2 = NULL;
		}
		
		printf("Iter[%d]\tLoss[%f]\tW0[%f]\tWCos[%f]\n", iter_num, loss, w0, w_cos);

		// Find next point by line search
		line_search(gradient_w0, gradient_wcos, gradient_weights, loss);
		++iter_num;
	}

	delete gradient_weights;
	gradient_weights = NULL;

	return 0;
}
Пример #7
0
void optimize_picture(network *net, image orig, int max_layer, float scale, float rate, float thresh, int norm)
{
    //scale_image(orig, 2);
    //translate_image(orig, -1);
    net->n = max_layer + 1;

    int dx = rand()%16 - 8;
    int dy = rand()%16 - 8;
    int flip = rand()%2;

    image crop = crop_image(orig, dx, dy, orig.w, orig.h);
    image im = resize_image(crop, (int)(orig.w * scale), (int)(orig.h * scale));
    if(flip) flip_image(im);

    resize_network(net, im.w, im.h);
    layer_t last = net->layers[net->n-1];
    //net->layers[net->n - 1].activation = LINEAR;

    image delta = make_image(im.w, im.h, im.c);

    NETWORK_STATE(state);

#ifdef GPU
    state.input = cuda_make_array(im.data, im.w*im.h*im.c);
    state.delta = cuda_make_array(im.data, im.w*im.h*im.c);

    forward_network_gpu(*net, state);
    copy_ongpu(last.outputs, last.output_gpu, 1, last.delta_gpu, 1);

    cuda_pull_array(last.delta_gpu, last.delta, last.outputs);
    calculate_loss(last.delta, last.delta, last.outputs, thresh);
    cuda_push_array(last.delta_gpu, last.delta, last.outputs);

    backward_network_gpu(*net, state);

    cuda_pull_array(state.delta, delta.data, im.w*im.h*im.c);
    cuda_free(state.input);
    cuda_free(state.delta);
#else
    state.input = im.data;
    state.delta = delta.data;
    forward_network(*net, state);
    fltcpy(last.delta, last.output, last.outputs);
    calculate_loss(last.output, last.delta, last.outputs, thresh);
    backward_network(*net, state);
#endif

    if(flip) flip_image(delta);
    //normalize_array(delta.data, delta.w*delta.h*delta.c);
    image resized = resize_image(delta, orig.w, orig.h);
    image out = crop_image(resized, -dx, -dy, orig.w, orig.h);

    /*
       image g = grayscale_image(out);
       free_image(out);
       out = g;
     */

    //rate = rate / abs_mean(out.data, out.w*out.h*out.c);

    if(norm) normalize_array(out.data, out.w*out.h*out.c);
    fltaddmul(orig.data, out.data, orig.w * orig.h * orig.c, rate);

    /*
       normalize_array(orig.data, orig.w*orig.h*orig.c);
       scale_image(orig, sqrt(var));
       translate_image(orig, mean);
     */

    //translate_image(orig, 1);
    //scale_image(orig, .5);
    //normalize_image(orig);

    constrain_image(orig);

    free_image(crop);
    free_image(im);
    free_image(delta);
    free_image(resized);
    free_image(out);

}
/* Sync is called when the clock needs to advance on the bottom-up pass */
TIMESTAMP dc_dc_converter::sync(TIMESTAMP t0, TIMESTAMP t1) 
{
	
	V_Out = pCircuit_V[0];
	
	gl_verbose("dc_dc_c sync: got voltage from parent, is: %f", V_Out);
	
	internal_losses = 1 - calculate_loss(Rtotal, Ltotal, Ctotal, DC, AC);
		
	//TODO: consider installing duty or on-ratio limits
	//switch(dc_dc_converter_type_v){
	//		case BUCK:
	//			if(V_Out.Re() > V_In.Re()){
	//				throw("Buck converters can not increase the voltage level!");
	//			}else{
	//				duty_ratio = V_Out.Re() / V_In.Re();
	//			}
	//			break;
	//		case BOOST:
	//			if(V_Out.Re() < V_In.Re()){
	//				throw("Boost converters can not decrease the voltage level!");
	//			}else{
	//				on_ratio = (V_Out.Re()/V_In.Re()) - 1;
	//			}
	//			break;
	//		case BUCK_BOOST:
	//			on_ratio = - V_Out.Re() / V_In.Re();
	//			break;
	//		default:
	//			break;
	//}


	switch(gen_mode_v){
		case SUPPLY_DRIVEN:
			gl_verbose("dc_dc_c sync: supply driven");
			{//TODO
			//set V_Out for each phase
			//set V_In from line
			//set I_In from line
			//set RLoad for this time step if it isn't constant
			
			VA_In = V_In * ~ I_In; //DC
		

			VA_Out = VA_In * efficiency * internal_losses;
			losses = VA_Out * Rtotal / (Rtotal + Rload);
			VA_Out = VA_Out * Rload / (Rtotal + Rload);
			/*
			if (number_of_phases_out == 3){
				power_A = power_B = power_C = VA_Out /3;
				phaseA_I_Out = (power_A / phaseA_V_Out)/sqrt(2.0);
				phaseB_I_Out = (power_B / phaseB_V_Out)/sqrt(2.0);
				phaseC_I_Out = (power_C / phaseC_V_Out)/sqrt(2.0);
			}else if(number_of_phases_out == 1){
				if(phaseA_V_Out != 0){
					power_A = VA_Out;
					phaseA_I_Out = (power_A / phaseA_V_Out) / sqrt(2.0);
				}else if(phaseB_V_Out != 0){
					power_B = VA_Out;
					phaseB_I_Out = (power_B / phaseB_V_Out) / sqrt(2.0);
				}else if(phaseC_V_Out != 0){
					power_C = VA_Out;
					phaseC_I_Out = (power_C / phaseC_V_Out) / sqrt(2.0);
				}else{
					throw ("none of the phases have voltages!");
				}
			}else{
				throw ("unsupported number of phases");
			}
			*/

			I_Out = VA_Out / V_Out;
			I_Out = ~ I_Out;

			pLine_I[0] = I_Out;

			return TS_NEVER;
			}
			break;
		case CONSTANT_PQ:
			gl_verbose("dc_dc_c sync: constant pq");
			{//TODO
			//gather V_Out for each phase
			//gather V_In (DC) from line
			//P_Out is either set or input from elsewhere
			//Q_Out is either set or input from elsewhere
			//Gather Rload

			//VA_Out = sqrt(pow(P_Out, 2) + pow(Q_Out, 2));
			//pf_out = P_Out/VA_Out.Mag();
			
			//VA_Out = VA_In * efficiency * internal_losses;
/*
			if (number_of_phases_out == 3){
				power_A = power_B = power_C = VA_Out /3;
				phaseA_I_Out = (power_A / phaseA_V_Out)/sqrt(2.0);
				phaseB_I_Out = (power_B / phaseB_V_Out)/sqrt(2.0);
				phaseC_I_Out = (power_C / phaseC_V_Out)/sqrt(2.0);
			}else if(number_of_phases_out == 1){
				if(phaseAOut){
					power_A = VA_Out;
					phaseA_I_Out = (power_A / phaseA_V_Out) / sqrt(2.0);
				}else if(phaseBOut){
					power_B = VA_Out;
					phaseB_I_Out = (power_B / phaseB_V_Out) / sqrt(2.0);
				}else if(phaseCOut){
					power_C = VA_Out;
					phaseC_I_Out = (power_C / phaseC_V_Out) / sqrt(2.0);
				}else{
					throw ("none of the phases have voltages!");
				}
			}else{
				throw ("unsupported number of phases");
			}
*/
			
			if(parent_string = "meter"){
				VA_Out = complex(P_Out,Q_Out);
				gl_verbose("dc_dc_c sync: VA_Out set is: (%f , %f)", VA_Out.Re(), VA_Out.Im());
			}else{
				I_Out = pLine_I[0];
				gl_verbose("dc_dc_c sync: V_In requested is: (%f , %f)", V_In.Re(), V_In.Im());
				VA_Out = V_Out * ~ I_Out;
				gl_verbose("dc_dc_c sync: VA_Out set is: (%f , %f)", VA_Out.Re(), VA_Out.Im());
			}

			V_In = V_Out / service_ratio;

			VA_In = VA_Out / (efficiency * internal_losses);

			gl_verbose("dc_dc_c sync: VA_In requested is: (%f , %f)", VA_In.Re(), VA_In.Im());

			losses = VA_Out * (1 - (efficiency * internal_losses));

			if(V_In == 0){
				I_In = 0;
			}else{
				I_In = VA_In / V_In;
				I_In = ~I_In;
			}

			gl_verbose("dc_dc_c sync: I_In requested is: (%f , %f)", I_In.Re(), I_In.Im());

			return TS_NEVER;
			}
			break;
		case CONSTANT_V:
			gl_verbose("dc_dc_c sync: constant v");
			{
			bool changed = false;
			
			//TODO
			//Gather V_Out
			//Gather VA_Out
			//Gather Rload
			I_Out = pLine_I[0];

			gl_verbose("dc_dc_c sync: I from parent is: (%f , %f)", I_Out.Re(), I_Out.Im());
			
			V_In = V_Out / service_ratio;


			gl_verbose("dc_dc_c sync: V_Out from parent is: (%f , %f)", V_Out.Re(), V_Out.Im());
			gl_verbose("dc_dc_c sync: V_In calculated: (%f , %f)", V_In.Re(), V_In.Im());
			
			if (V_Out < (V_Set - margin)){
				I_Out = I_out_prev + I_step_max / 2;
				changed = true;
			}else if (V_Out > (V_Set + margin)){
				I_Out = I_out_prev - I_step_max / 2;
				changed = true;
			}else{
				changed = false;
			}

			gl_verbose("dc_dc_c sync: I_In after adjustment is: (%f , %f)", I_Out.Re(), I_Out.Im());
	

			VA_Out = (~I_Out) * V_Out;

			gl_verbose("dc_dc_c sync: VA_Out pre-limits is: (%f , %f)", VA_Out.Re(), VA_Out.Im());

			if(VA_Out > Rated_kVA){
				VA_Out = Rated_kVA;
				I_Out = VA_Out / V_Out;
				I_Out = ~I_Out;
				changed = false;
			}
			
			if(VA_Out < 0){
				VA_Out = 0;
				I_Out = 0;
				changed = false;
			}



			/*
			
			if(phaseAOut){
				if (phaseA_V_Out < (V_Set_A - margin)){
					phaseA_I_Out = phaseA_I_Out_prev + I_step_max/2;
					changed = true;
				}else if (phaseA_V_Out > (V_Set_A + margin)){
					phaseA_I_Out = phaseA_I_Out_prev - I_step_max/2;
					changed = true;
				}else{changed = false;}
			}if (phaseBOut){
				if (phaseB_V_Out < (V_Set_B - margin)){
					phaseB_I_Out = phaseB_I_Out_prev + I_step_max/2;
					changed = true;
				}else if (phaseB_V_Out > (V_Set_B + margin)){
					phaseB_I_Out = phaseB_I_Out_prev - I_step_max/2;
					changed = true;
				}else{changed = false;}
			}if (phaseCOut){
				if (phaseC_V_Out < (V_Set_C - margin)){
					phaseC_I_Out = phaseC_I_Out_prev + I_step_max/2;
					changed = true;
				}else if (phaseC_V_Out > (V_Set_C + margin)){
					phaseC_I_Out = phaseC_I_Out_prev - I_step_max/2;
					changed = true;
				}else{changed = false;}
			}
			
			power_A = phaseA_I_Out * phaseA_V_Out;
			power_B = phaseB_I_Out * phaseB_V_Out;
			power_C = phaseC_I_Out * phaseC_V_Out;

			//check if dc_dc_converter is overloaded -- if so, cap at max power
			if (((power_A + power_B + power_C) > Rated_kVA) ||
				((power_A.Re() + power_B.Re() + power_C.Re()) > Max_P) ||
				((power_A.Im() + power_B.Im() + power_C.Im()) > Max_Q))
			{
				VA_Out = Rated_kVA / number_of_phases_out;
				if(phaseAOut){
					phaseA_I_Out = VA_Out / phaseA_V_Out;
				}
				if(phaseBOut){
					phaseB_I_Out = VA_Out / phaseB_V_Out;
				}
				if(phaseCOut){
					phaseC_I_Out = VA_Out / phaseC_V_Out;
				}
			}
			
			//check if power is negative for some reason, should never be
			if(power_A < 0){
				power_A = 0;
				phaseA_I_Out = 0;
				throw("phaseA power is negative!");
			}
			if(power_B < 0){
				power_B = 0;
				phaseB_I_Out = 0;
				throw("phaseB power is negative!");
			}
			if(power_C < 0){
				power_C = 0;
				phaseC_I_Out = 0;
				throw("phaseC power is negative!");

			}

			*/

			gl_verbose("dc_dc_c sync: VA_In requested pre-losses is: (%f , %f)", VA_Out.Re(), VA_Out.Im());

			VA_In = VA_Out / (efficiency * internal_losses);
			losses = VA_Out * (1 - (efficiency * internal_losses));

			gl_verbose("dc_dc_c sync: VA_In requested with losses is: (%f , %f)", VA_In.Re(), VA_In.Im());


			if(V_In == 0){
				I_In = 0;
			}else{
				I_In = VA_In / V_In;
				I_In = ~I_In;
			}

			gl_verbose("dc_dc_c sync: I_In requested is: (%f , %f)", I_In.Re(), I_In.Im());

			//TODO: check P and Q components to see if within bounds

			if(changed){
				TIMESTAMP t2 = t1 + 10 * 60 * TS_SECOND;
				return t2;
			}else{
				return TS_NEVER;
			}
			}
			break;
		default:
			break;

	return TS_NEVER;

	}
return TS_NEVER;
}
Пример #9
0
/* Sync is called when the clock needs to advance on the bottom-up pass */
TIMESTAMP rectifier::sync(TIMESTAMP t0, TIMESTAMP t1) 
{
	
	V_Out = pCircuit_V[0];
	I_Out = pLine_I[0];

	gl_verbose("rectifier sync: V_Out from parent is: (%f , %f)", V_Out.Re(), V_Out.Im());
	gl_verbose("rectifier sync: I_Out from parent is: (%f , %f)", I_Out.Re(), I_Out.Im());

	internal_losses = 1 - calculate_loss(Rtotal, Ltotal, Ctotal, DC, AC);
	frequency_losses = 1 - calculate_frequency_loss(input_frequency, Rtotal,Ltotal, Ctotal);
		
	//TODO: consider installing duty or on-ratio limits
	


	switch(gen_mode_v){
		case SUPPLY_DRIVEN:
			{//TODO
			//set V_Out for each phase
			//set V_In from line
			//set I_In from line
			//set RLoad for this time step if it isn't constant
			gl_verbose("rectifier sync: supply driven rectifier");

				power_A_In = phaseA_V_In * phaseA_I_In; //AC
				power_B_In = phaseB_V_In * phaseB_I_In;
				power_C_In = phaseC_V_In * phaseC_I_In;

				VA_In = power_A_In + power_B_In + power_C_In;

		

			VA_Out = VA_In * efficiency * internal_losses * frequency_losses;
			losses = VA_Out * Rtotal / (Rtotal + Rload);
			VA_Out = VA_Out * Rload / (Rtotal + Rload);
			/*
			if (number_of_phases_out == 3){
				power_A = power_B = power_C = VA_Out /3;
				phaseA_I_Out = (power_A / phaseA_V_Out)/sqrt(2.0);
				phaseB_I_Out = (power_B / phaseB_V_Out)/sqrt(2.0);
				phaseC_I_Out = (power_C / phaseC_V_Out)/sqrt(2.0);
			}else if(number_of_phases_out == 1){
				if(phaseA_V_Out != 0){
					power_A = VA_Out;
					phaseA_I_Out = (power_A / phaseA_V_Out) / sqrt(2.0);
				}else if(phaseB_V_Out != 0){
					power_B = VA_Out;
					phaseB_I_Out = (power_B / phaseB_V_Out) / sqrt(2.0);
				}else if(phaseC_V_Out != 0){
					power_C = VA_Out;
					phaseC_I_Out = (power_C / phaseC_V_Out) / sqrt(2.0);
				}else{
					throw ("none of the phases have voltages!");
				}
			}else{
				throw ("unsupported number of phases");
			}
			*/

			I_Out = VA_Out / V_Out;
			I_Out = ~ I_Out;


			gl_verbose("rectifier sync: VA_Out is: (%f , %f)", VA_Out.Re(), VA_Out.Im());
			gl_verbose("rectifier sync: V_Out is: (%f , %f)", V_Out.Re(), V_Out.Im());
			gl_verbose("rectifier sync: I_Out is: (%f , %f)", I_Out.Re(), I_Out.Im());
			gl_verbose("rectifier sync: losses is: (%f , %f)", losses.Re(), losses.Im());


			gl_verbose("rectifier sync: supply driven about to exit");
			return TS_NEVER;
			}
			break;
		case CONSTANT_PQ:
			{//TODO
			gl_verbose("rectifier sync: constant pq rectifier");
			//P_Out is either set or input from elsewhere
			//Q_Out is either set or input from elsewhere

			//if the device is connected directly to the meter, then it is the lead node and must set the
			//power output for the rest of the branches
			if(parent_string == "meter"){
				VA_Out = complex(P_Out,Q_Out);
			}else{
				VA_Out = V_Out * (~I_Out);
			}

			gl_verbose("rectifier sync: VA_Out calculated from parent is: (%f , %f)", VA_Out.Re(), VA_Out.Im());
			
			VA_In = VA_Out / (efficiency * internal_losses);
			losses = VA_Out * (1 - (efficiency * internal_losses));

			//I_In = VA_In / V_In;

			gl_verbose("rectifier sync: VA_In calculated after losses is: (%f , %f)", VA_In.Re(), VA_In.Im());

			if(number_of_phases_in == 3){
				iterative_IV(VA_In, "D");
			}
			
			if(number_of_phases_in == 1){
				//divide up power by the number of phases, then assign that power to each phase
				VA_In = VA_In / number_of_phases_in;

				//assume balanced system:


				//TODO: I'm not sure if the sqrt(2) is necessary... it is supposed to account for RMS current but 
				//it may already be in RMS current after dividing by voltage
				if(phaseAIn){
					power_A_In = VA_In;
					iterative_IV(power_A_In,"A");

				//	phaseA_V_In = ((power_A_In * Xphase) + V_In_Set_A)^0.5;
				//	phaseA_I_In = (power_A_In / phaseA_V_In); 
				//	
				}

				if(phaseBIn){
					power_B_In = VA_In;
					iterative_IV(power_B_In,"B");
				//		phaseB_V_In = ((power_B_In * Xphase) + V_In_Set_B)^0.5;
				//		phaseB_I_In = (power_B_In / phaseB_V_In);
				//	
				}

				if(phaseCIn){
					power_C_In = VA_In;
					iterative_IV(power_C_In,"C");

				//	phaseC_V_In = ((power_C_In * Xphase) + V_In_Set_C)^0.5;
				//	phaseC_I_In = (power_C_In / phaseC_V_In);
				//	
				}

			}


			gl_verbose("rectifier sync: VA_In is: (%f , %f)", VA_In.Re(), VA_In.Im());
			gl_verbose("rectifier sync: power_A_In is: (%f , %f)", power_A_In.Re(), power_A_In.Im());
			gl_verbose("rectifier sync: power_B_In is: (%f , %f)", power_B_In.Re(), power_B_In.Im());
			gl_verbose("rectifier sync: power_C_In is: (%f , %f)", power_C_In.Re(), power_C_In.Im());
			gl_verbose("rectifier sync: phaseA_V_In is: (%f , %f)", phaseA_V_In.Re(), phaseA_V_In.Im());
			gl_verbose("rectifier sync: phaseB_V_In is: (%f , %f)", phaseB_V_In.Re(), phaseB_V_In.Im());
			gl_verbose("rectifier sync: phaseC_V_In is: (%f , %f)", phaseC_V_In.Re(), phaseC_V_In.Im());
			gl_verbose("rectifier sync: phaseA_I_In is: (%f , %f)", phaseA_I_In.Re(), phaseA_I_In.Im());
			gl_verbose("rectifier sync: phaseB_I_In is: (%f , %f)", phaseB_I_In.Re(), phaseB_I_In.Im());
			gl_verbose("rectifier sync: phaseC_I_In is: (%f , %f)", phaseC_I_In.Re(), phaseC_I_In.Im());

			gl_verbose("rectifier sync: constant pq about to exit");
			return TS_NEVER;
			}
			break;
		case CONSTANT_V:
			{
			gl_verbose("rectifier sync: constant v rectifier");
			bool changed = false;


			//phaseA_V_In = V_Out;
			//phaseB_V_In = V_Out;
			//phaseC_V_In = V_Out;


			//TODO
			//Gather V_Out
			//Gather VA_Out
			//Gather Rload
			
			
			if (V_Out < (V_Set - margin)){
				I_Out = I_out_prev + I_step_max / 2;
				changed = true;
			}else if (V_Out > (V_Set + margin)){
				I_Out = I_out_prev - I_step_max / 2;
				changed = true;
			}else{
				changed = false;
			}

			VA_Out = (~I_Out) * V_Out;

			if(VA_Out > Rated_kVA){
				VA_Out = Rated_kVA;
				I_Out = VA_Out / V_Out;
				changed = false;
			}
			
			if(VA_Out < 0){
				VA_Out = 0;
				I_Out = 0;
				changed = false;
			}



			/*
			
			if(phaseAOut){
				if (phaseA_V_Out < (V_Set_A - margin)){
					phaseA_I_Out = phaseA_I_Out_prev + I_step_max/2;
					changed = true;
				}else if (phaseA_V_Out > (V_Set_A + margin)){
					phaseA_I_Out = phaseA_I_Out_prev - I_step_max/2;
					changed = true;
				}else{changed = false;}
			}if (phaseBOut){
				if (phaseB_V_Out < (V_Set_B - margin)){
					phaseB_I_Out = phaseB_I_Out_prev + I_step_max/2;
					changed = true;
				}else if (phaseB_V_Out > (V_Set_B + margin)){
					phaseB_I_Out = phaseB_I_Out_prev - I_step_max/2;
					changed = true;
				}else{changed = false;}
			}if (phaseCOut){
				if (phaseC_V_Out < (V_Set_C - margin)){
					phaseC_I_Out = phaseC_I_Out_prev + I_step_max/2;
					changed = true;
				}else if (phaseC_V_Out > (V_Set_C + margin)){
					phaseC_I_Out = phaseC_I_Out_prev - I_step_max/2;
					changed = true;
				}else{changed = false;}
			}
			
			power_A = phaseA_I_Out * phaseA_V_Out;
			power_B = phaseB_I_Out * phaseB_V_Out;
			power_C = phaseC_I_Out * phaseC_V_Out;

			//check if rectifier is overloaded -- if so, cap at max power
			if (((power_A + power_B + power_C) > Rated_kVA) ||
				((power_A.Re() + power_B.Re() + power_C.Re()) > Max_P) ||
				((power_A.Im() + power_B.Im() + power_C.Im()) > Max_Q))
			{
				VA_Out = Rated_kVA / number_of_phases_out;
				if(phaseAOut){
					phaseA_I_Out = VA_Out / phaseA_V_Out;
				}
				if(phaseBOut){
					phaseB_I_Out = VA_Out / phaseB_V_Out;
				}
				if(phaseCOut){
					phaseC_I_Out = VA_Out / phaseC_V_Out;
				}
			}
			
			//check if power is negative for some reason, should never be
			if(power_A < 0){
				power_A = 0;
				phaseA_I_Out = 0;
				throw("phaseA power is negative!");
			}
			if(power_B < 0){
				power_B = 0;
				phaseB_I_Out = 0;
				throw("phaseB power is negative!");
			}
			if(power_C < 0){
				power_C = 0;
				phaseC_I_Out = 0;
				throw("phaseC power is negative!");

			}

			*/

			gl_verbose("rectifier sync: VA_Out requested from parent is: (%f , %f)", VA_Out.Re(), VA_Out.Im());


			VA_In = VA_Out / (efficiency * internal_losses);
			losses = VA_Out * (1 - (efficiency * internal_losses));
		
			gl_verbose("rectifier sync: VA_In after losses is: (%f , %f)", VA_In.Re(), VA_In.Im());

			//after we export this current to the generator that is hooked up on the input, 
			//we will expect that during the next timestep, the generator will give us a
			//new frequency of input power that it has adopted in order to meet the 
			//requested current

			if(number_of_phases_in == 3){
				iterative_IV(VA_In, "D");
			}

			if(number_of_phases_in == 1){
				VA_In = VA_In / number_of_phases_in;

				if(phaseAIn){
					power_A_In = VA_In;
					iterative_IV(power_A_In,"A");
				}

				if(phaseBIn){
					power_B_In = VA_In;
					iterative_IV(power_B_In,"B");
				}

				if(phaseCIn){
					power_C_In = VA_In;
					iterative_IV(power_C_In,"C");
				}

			}
			//TODO: check P and Q components to see if within bounds


			gl_verbose("rectifier sync: VA_In is: (%f , %f)", VA_In.Re(), VA_In.Im());
			gl_verbose("rectifier sync: power_A_In is: (%f , %f)", power_A_In.Re(), power_A_In.Im());
			gl_verbose("rectifier sync: power_B_In is: (%f , %f)", power_B_In.Re(), power_B_In.Im());
			gl_verbose("rectifier sync: power_C_In is: (%f , %f)", power_C_In.Re(), power_C_In.Im());
			gl_verbose("rectifier sync: phaseA_V_In is: (%f , %f)", phaseA_V_In.Re(), phaseA_V_In.Im());
			gl_verbose("rectifier sync: phaseB_V_In is: (%f , %f)", phaseB_V_In.Re(), phaseB_V_In.Im());
			gl_verbose("rectifier sync: phaseC_V_In is: (%f , %f)", phaseC_V_In.Re(), phaseC_V_In.Im());
			gl_verbose("rectifier sync: phaseA_I_In is: (%f , %f)", phaseA_I_In.Re(), phaseA_I_In.Im());
			gl_verbose("rectifier sync: phaseB_I_In is: (%f , %f)", phaseB_I_In.Re(), phaseB_I_In.Im());
			gl_verbose("rectifier sync: phaseC_I_In is: (%f , %f)", phaseC_I_In.Re(), phaseC_I_In.Im());


			gl_verbose("rectifier sync: constant v rectifier about to exit");
			if(changed){
				TIMESTAMP t2 = TS_NEVER;
				return t2;
			}else{
				return TS_NEVER;
			}
			}
			break;
		default:
			break;

	return TS_NEVER;

	}
return TS_NEVER;
}
Пример #10
0
/* Sync is called when the clock needs to advance on the bottom-up pass */
TIMESTAMP rectifier::sync(TIMESTAMP t0, TIMESTAMP t1) 
{

	V_Out = (*pCircuit_V).Re();
	I_Out = (*pLine_I).Re();

	gl_verbose("rectifier sync: V_Out from parent is: (%f , %f)", V_Out.Re(), V_Out.Im());
	gl_verbose("rectifier sync: I_Out from parent is: (%f , %f)", I_Out.Re(), I_Out.Im());

	internal_losses = 1 - calculate_loss(Rtotal, Ltotal, Ctotal, DC, AC);
	frequency_losses = 1 - calculate_frequency_loss(input_frequency, Rtotal,Ltotal, Ctotal);

	//TODO: consider installing duty or on-ratio limits
	//AC-DC voltage magnitude ratio rule
	double VInMag = V_Out.Mag() * PI / (3 * sqrt(6.0));
	voltage_A = complex(VInMag,0);
	voltage_B = complex(-VInMag/2, VInMag * sqrt(3.0) / 2);
	voltage_C = complex(-VInMag/2, -VInMag * sqrt(3.0) / 2);


	switch(gen_mode_v){
		case SUPPLY_DRIVEN:
			{

				complex S_A_In, S_B_In, S_C_In;

				//DC Voltage, controlled by parent object determines DC Voltage.
				S_A_In = voltage_A*(~(current_A));
				S_B_In = voltage_B*(~(current_B));
				S_C_In = voltage_C*(~(current_C));

				power_A_In = S_A_In.Re();
				power_B_In = S_B_In.Re();
				power_C_In = S_C_In.Re();
				/*
				power_A_In = voltage_A.Mag() * current_A.Mag(); //AC
				power_B_In = voltage_B.Mag() * current_B.Mag();
				power_C_In = voltage_C.Mag() * current_C.Mag();
				*/
				VA_In = power_A_In + power_B_In + power_C_In;



				VA_Out = VA_In * efficiency * internal_losses * frequency_losses;
				losses = VA_Out * Rtotal / (Rtotal + Rload);

				I_Out = ~(VA_Out / V_Out); //These values are completely real, but since parent object uses complex, use here as well and follow rule for complex conjugate
				*pLine_I=I_Out;

				gl_verbose("rectifier sync: VA_Out is: (%f , %f)", VA_Out.Re(), VA_Out.Im());
				gl_verbose("rectifier sync: V_Out is: (%f , %f)", V_Out.Re(), V_Out.Im());
				gl_verbose("rectifier sync: I_Out is: (%f , %f)", I_Out.Re(), I_Out.Im());
				gl_verbose("rectifier sync: losses is: (%f , %f)", losses.Re(), losses.Im());


				gl_verbose("rectifier sync: supply driven about to exit");
				return TS_NEVER;
			}
			break;
			/*
			case CONSTANT_PQ:
			{//TODO
			gl_verbose("rectifier sync: constant pq rectifier");
			//P_Out is either set or input from elsewhere
			//Q_Out is either set or input from elsewhere

			//if the device is connected directly to the meter, then it is the lead node and must set the
			//power output for the rest of the branches
			if(parent_string == "meter"){
			VA_Out = complex(P_Out,Q_Out);
			}else{
			VA_Out = V_Out * (~I_Out);
			}

			gl_verbose("rectifier sync: VA_Out calculated from parent is: (%f , %f)", VA_Out.Re(), VA_Out.Im());

			VA_In = VA_Out / (efficiency * internal_losses);
			losses = VA_Out * (1 - (efficiency * internal_losses));

			//I_In = VA_In / V_In;

			gl_verbose("rectifier sync: VA_In calculated after losses is: (%f , %f)", VA_In.Re(), VA_In.Im());

			if(number_of_phases_in == 3){
			iterative_IV(VA_In, "D");
			}

			if(number_of_phases_in == 1){
			//divide up power by the number of phases, then assign that power to each phase
			VA_In = VA_In / number_of_phases_in;

			//assume balanced system:


			//TODO: I'm not sure if the sqrt(2) is necessary... it is supposed to account for RMS current but 
			//it may already be in RMS current after dividing by voltage
			if(phaseAIn){
			power_A_In = VA_In;
			iterative_IV(power_A_In,"A");

			//	voltage_A = ((power_A_In * Xphase) + V_In_Set_A)^0.5;
			//	current_A = (power_A_In / voltage_A); 
			//	
			}

			if(phaseBIn){
			power_B_In = VA_In;
			iterative_IV(power_B_In,"B");
			//		voltage_B = ((power_B_In * Xphase) + V_In_Set_B)^0.5;
			//		current_B = (power_B_In / voltage_B);
			//	
			}

			if(phaseCIn){
			power_C_In = VA_In;
			iterative_IV(power_C_In,"C");

			//	voltage_C = ((power_C_In * Xphase) + V_In_Set_C)^0.5;
			//	current_C = (power_C_In / voltage_C);
			//	
			}

			}


			gl_verbose("rectifier sync: VA_In is: (%f , %f)", VA_In.Re(), VA_In.Im());
			gl_verbose("rectifier sync: power_A_In is: (%f , %f)", power_A_In.Re(), power_A_In.Im());
			gl_verbose("rectifier sync: power_B_In is: (%f , %f)", power_B_In.Re(), power_B_In.Im());
			gl_verbose("rectifier sync: power_C_In is: (%f , %f)", power_C_In.Re(), power_C_In.Im());
			gl_verbose("rectifier sync: voltage_A is: (%f , %f)", voltage_A.Re(), voltage_A.Im());
			gl_verbose("rectifier sync: voltage_B is: (%f , %f)", voltage_B.Re(), voltage_B.Im());
			gl_verbose("rectifier sync: voltage_C is: (%f , %f)", voltage_C.Re(), voltage_C.Im());
			gl_verbose("rectifier sync: current_A is: (%f , %f)", current_A.Re(), current_A.Im());
			gl_verbose("rectifier sync: current_B is: (%f , %f)", current_B.Re(), current_B.Im());
			gl_verbose("rectifier sync: current_C is: (%f , %f)", current_C.Re(), current_C.Im());

			gl_verbose("rectifier sync: constant pq about to exit");
			return TS_NEVER;
			}
			break;
			case CONSTANT_V:
			{
			gl_verbose("rectifier sync: constant v rectifier");
			bool changed = false;


			//voltage_A = V_Out;
			//voltage_B = V_Out;
			//voltage_C = V_Out;


			//TODO
			//Gather V_Out
			//Gather VA_Out
			//Gather Rload


			if (V_Out < (V_Rated - margin)){
			I_Out = I_out_prev + I_step_max / 2;
			changed = true;
			}else if (V_Out > (V_Rated + margin)){
			I_Out = I_out_prev - I_step_max / 2;
			changed = true;
			}else{
			changed = false;
			}

			VA_Out = (~I_Out) * V_Out;

			if(VA_Out > Rated_kVA){
			VA_Out = Rated_kVA;
			I_Out = VA_Out / V_Out;
			changed = false;
			}

			if(VA_Out < 0){
			VA_Out = 0;
			I_Out = 0;
			changed = false;
			}





			if(phaseAOut){
			if (phaseA_V_Out < (V_Rated_A - margin)){
			phaseA_I_Out = phaseA_I_Out_prev + I_step_max/2;
			changed = true;
			}else if (phaseA_V_Out > (V_Rated_A + margin)){
			phaseA_I_Out = phaseA_I_Out_prev - I_step_max/2;
			changed = true;
			}else{changed = false;}
			}if (phaseBOut){
			if (phaseB_V_Out < (V_Rated_B - margin)){
			phaseB_I_Out = phaseB_I_Out_prev + I_step_max/2;
			changed = true;
			}else if (phaseB_V_Out > (V_Rated_B + margin)){
			phaseB_I_Out = phaseB_I_Out_prev - I_step_max/2;
			changed = true;
			}else{changed = false;}
			}if (phaseCOut){
			if (phaseC_V_Out < (V_Rated_C - margin)){
			phaseC_I_Out = phaseC_I_Out_prev + I_step_max/2;
			changed = true;
			}else if (phaseC_V_Out > (V_Rated_C + margin)){
			phaseC_I_Out = phaseC_I_Out_prev - I_step_max/2;
			changed = true;
			}else{changed = false;}
			}

			power_A = phaseA_I_Out * phaseA_V_Out;
			power_B = phaseB_I_Out * phaseB_V_Out;
			power_C = phaseC_I_Out * phaseC_V_Out;

			//check if rectifier is overloaded -- if so, cap at max power
			if (((power_A + power_B + power_C) > Rated_kVA) ||
			((power_A.Re() + power_B.Re() + power_C.Re()) > Max_P) ||
			((power_A.Im() + power_B.Im() + power_C.Im()) > Max_Q))
			{
			VA_Out = Rated_kVA / number_of_phases_out;
			if(phaseAOut){
			phaseA_I_Out = VA_Out / phaseA_V_Out;
			}
			if(phaseBOut){
			phaseB_I_Out = VA_Out / phaseB_V_Out;
			}
			if(phaseCOut){
			phaseC_I_Out = VA_Out / phaseC_V_Out;
			}
			}

			//check if power is negative for some reason, should never be
			if(power_A < 0){
			power_A = 0;
			phaseA_I_Out = 0;
			throw("phaseA power is negative!");
			}
			if(power_B < 0){
			power_B = 0;
			phaseB_I_Out = 0;
			throw("phaseB power is negative!");
			}
			if(power_C < 0){
			power_C = 0;
			phaseC_I_Out = 0;
			throw("phaseC power is negative!");

			}



			gl_verbose("rectifier sync: VA_Out requested from parent is: (%f , %f)", VA_Out.Re(), VA_Out.Im());


			VA_In = VA_Out / (efficiency * internal_losses);
			losses = VA_Out * (1 - (efficiency * internal_losses));

			gl_verbose("rectifier sync: VA_In after losses is: (%f , %f)", VA_In.Re(), VA_In.Im());

			//after we export this current to the generator that is hooked up on the input, 
			//we will expect that during the next timestep, the generator will give us a
			//new frequency of input power that it has adopted in order to meet the 
			//requested current

			if(number_of_phases_in == 3){
			iterative_IV(VA_In, "D");
			}

			if(number_of_phases_in == 1){
			VA_In = VA_In / number_of_phases_in;

			if(phaseAIn){
			power_A_In = VA_In;
			iterative_IV(power_A_In,"A");
			}

			if(phaseBIn){
			power_B_In = VA_In;
			iterative_IV(power_B_In,"B");
			}

			if(phaseCIn){
			power_C_In = VA_In;
			iterative_IV(power_C_In,"C");
			}

			}
			//TODO: check P and Q components to see if within bounds


			gl_verbose("rectifier sync: VA_In is: (%f , %f)", VA_In.Re(), VA_In.Im());
			gl_verbose("rectifier sync: power_A_In is: (%f , %f)", power_A_In.Re(), power_A_In.Im());
			gl_verbose("rectifier sync: power_B_In is: (%f , %f)", power_B_In.Re(), power_B_In.Im());
			gl_verbose("rectifier sync: power_C_In is: (%f , %f)", power_C_In.Re(), power_C_In.Im());
			gl_verbose("rectifier sync: voltage_A is: (%f , %f)", voltage_A.Re(), voltage_A.Im());
			gl_verbose("rectifier sync: voltage_B is: (%f , %f)", voltage_B.Re(), voltage_B.Im());
			gl_verbose("rectifier sync: voltage_C is: (%f , %f)", voltage_C.Re(), voltage_C.Im());
			gl_verbose("rectifier sync: current_A is: (%f , %f)", current_A.Re(), current_A.Im());
			gl_verbose("rectifier sync: current_B is: (%f , %f)", current_B.Re(), current_B.Im());
			gl_verbose("rectifier sync: current_C is: (%f , %f)", current_C.Re(), current_C.Im());


			gl_verbose("rectifier sync: constant v rectifier about to exit");
			if(changed){
			TIMESTAMP t2 = TS_NEVER;
			return t2;
			}else{
			return TS_NEVER;
			}
			}
			break;*/
		default:
			break;

			return TS_NEVER;

	}
	return TS_NEVER;
}