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();

}
void transmissioncom::sendToTransmissionSolver(complex value)
{
  uint8_t data[sizeof(double)*2];
  double realPart=value.Re();
  double imagineryPart=value.Im();
  memcpy(&data,&realPart,sizeof(double));
  memcpy(&data[sizeof(double)],&imagineryPart,sizeof(double));
  
  OBJECT *hdr = OBJECTHDR(this);
  Message *msg=new Message(hdr->name,this->connectedBus,gl_globalclock,(uint8_t*)data,sizeof(double)*2);
  msg->setDelayThroughComm(false);
  myinterface->send(msg);
}
complex microturbine::determine_source_voltage(complex voltage_out, double r_internal, double r_load){
	//placeholder, probably need to replace with iterative function
	complex Vs = complex(((r_internal + r_load)/(r_load)) * voltage_out.Re(), ((r_internal + r_load)/(r_load)) * voltage_out.Im());
	return Vs;

}
void rectifier::iterative_IV(complex VA, char* phase_designation){
	complex Vdet = complex(0,0);
	complex increment = complex(0,0);
	complex Idet = complex(0,0);
	complex Vs = complex(0,0);
	complex s = complex(0,0);
	complex incrementA;
	complex incrementB;
	complex incrementC;
	complex VdetA = V_In_Set_A;
	complex VdetB = V_In_Set_B;
	complex VdetC = V_In_Set_C;
	complex IdetA = complex(0,0);
	complex IdetB = complex(0,0);
	complex IdetC = complex(0,0);
	int i = 0;
	
	if(VA.Re() > 3000){
		incrementA = complex(1 * 1,0);
		incrementB = complex(1 * -0.5, 1 * 0.866);
		incrementC = complex(1 * -0.5, 1 * -0.866);
	}else if (VA.Re() > 1000){
		incrementA = complex(0.5 * 1,0);
		incrementB = complex(0.5 * -0.5, 0.5 * 0.866);
		incrementC = complex(0.5 * -0.5, 0.5 * -0.866);
	}else{
		incrementA = complex(0.25 * 1,0);
		incrementB = complex(0.25 * -0.5, 0.25 * 0.866);
		incrementC = complex(0.25 * -0.5, 0.25 * -0.866);
	}


	if(phase_designation == "D"){
		while(true){
			if((abs(VA.Re() - s.Re()) < margin)){
				if(true){//abs(VA.Im() - s.Im()) < margin){
					break;
				}
			}

			if(i > 150){
				throw("rectifier sync: rectifier failed to converge!  The power asked for was too high or too low!");
				break;
			}
			VdetA = VdetA + incrementA;
			VdetB = VdetB + incrementB;
			VdetC = VdetC + incrementC;
			IdetA = (VdetA - V_In_Set_A)/XphaseA;
			IdetB = (VdetB - V_In_Set_B)/XphaseB;
			IdetC = (VdetC - V_In_Set_C)/XphaseC;
			s = ((~IdetA) * VdetA) + ((~IdetB) * VdetB) + ((~IdetC) * VdetC);
			i++;
		}

		phaseA_V_In = VdetA;
		phaseA_I_In = IdetA;
		power_A_In = (~IdetA) * VdetA;
		phaseB_V_In = VdetB;
		phaseB_I_In = IdetB;
		power_B_In = (~IdetB) * VdetB;
		phaseC_V_In = VdetC;
		phaseC_I_In = IdetC;
		power_C_In = (~IdetC) * VdetC;


		gl_verbose("rectifier sync: iterative solver: Total VA delivered is: (%f , %f)", s.Re(), s.Im());

		gl_verbose("rectifier sync: iterative solver: power_A_In delivered is: (%f , %f)", power_A_In.Re(), power_A_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseA_V_In is: (%f , %f)", phaseA_V_In.Re(), phaseA_V_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseA_I_In is: (%f , %f)", phaseA_I_In.Re(), phaseA_I_In.Im());

		gl_verbose("rectifier sync: iterative solver: power_B_In delivered is: (%f , %f)", power_B_In.Re(), power_B_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseB_V_In is: (%f , %f)", phaseB_V_In.Re(), phaseB_V_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseB_I_In is: (%f , %f)", phaseB_I_In.Re(), phaseB_I_In.Im());

		gl_verbose("rectifier sync: iterative solver: power_C_In delivered is: (%f , %f)", power_C_In.Re(), power_C_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseC_V_In is: (%f , %f)", phaseC_V_In.Re(), phaseC_V_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseC_I_In is: (%f , %f)", phaseC_I_In.Re(), phaseC_I_In.Im());

		return;
	}
	
	complex Xphase = complex(0,0);
	
	
	if (phase_designation == "A"){
		Vdet = V_In_Set_A;
		Vs = V_In_Set_A;
		increment = complex(0.5 * 1,0);
		Xphase = XphaseA;
	}
	else if(phase_designation == "B"){
		Vdet = V_In_Set_B;
		Vs = V_In_Set_B;
		increment = complex(0.5 * -0.5, 0.5 * 0.866);
		Xphase = XphaseB;
	}
	else if(phase_designation == "C"){
		Vdet = V_In_Set_C;
		Vs = V_In_Set_C;
		increment = complex(0.5 * -0.5, 0.5 * -0.866);
		Xphase = XphaseC;
	}else{
		throw("no phase designated!");
	}

	Idet = (Vdet - Vs)/Xphase;
	s = (~Idet) * Vdet;
	if(s.Mag() >= VA.Mag()){
		if(phase_designation == "A"){
			phaseA_V_In = Vdet;
			phaseA_I_In = Idet;
		}
		else if(phase_designation == "B"){
			phaseB_V_In = Vdet;
			phaseB_I_In = Idet;
		}
		else if(phase_designation == "C"){
			phaseC_V_In = Vdet;
			phaseC_I_In = Idet;
		}
		return;
	}

	
	while(((abs(VA.Re() - s.Re()) > margin) || (abs(VA.Im() - s.Im()) > margin))&& i < 50){
		Vdet = Vdet + increment; 
		Idet = (Vdet - Vs)/Xphase;
		s = Vdet * ~ Idet;
		i++;
	}

	if(phase_designation == "A"){
		phaseA_V_In = Vdet;
		phaseA_I_In = Idet;
		gl_verbose("rectifier sync: iterative solver: power_A_In asked for was: (%f , %f)", VA.Re(), VA.Im());
		gl_verbose("rectifier sync: iterative solver: power_A_In delivered is: (%f , %f)", s.Re(), s.Im());
		gl_verbose("rectifier sync: iterative solver: phaseA_V_In is: (%f , %f)", phaseA_V_In.Re(), phaseA_V_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseA_I_In is: (%f , %f)", phaseA_I_In.Re(), phaseA_I_In.Im());

	}
	else if(phase_designation == "B"){
		phaseB_V_In = Vdet;
		phaseB_I_In = Idet;
		gl_verbose("rectifier sync: iterative solver: power_B_In asked for was: (%f , %f)", VA.Re(), VA.Im());
		gl_verbose("rectifier sync: iterative solver: power_B_In delivered is: (%f , %f)", s.Re(), s.Im());
		gl_verbose("rectifier sync: iterative solver: phaseB_V_In is: (%f , %f)", phaseB_V_In.Re(), phaseB_V_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseB_I_In is: (%f , %f)", phaseB_I_In.Re(), phaseB_I_In.Im());
	}
	else if(phase_designation == "C"){
		phaseC_V_In = Vdet;
		phaseC_I_In = Idet;
		gl_verbose("rectifier sync: iterative solver: power_C_In asked for was: (%f , %f)", VA.Re(), VA.Im());
		gl_verbose("rectifier sync: iterative solver: power_C_In delivered is: (%f , %f)", s.Re(), s.Im());
		gl_verbose("rectifier sync: iterative solver: phaseC_V_In is: (%f , %f)", phaseC_V_In.Re(), phaseC_V_In.Im());
		gl_verbose("rectifier sync: iterative solver: phaseC_I_In is: (%f , %f)", phaseC_I_In.Re(), phaseC_I_In.Im());
	}


	return;
}
void setObjectValue_Complex(OBJECT* obj, char* Property, complex val)
{
	setObjectValue_Double2Complex(obj,Property,val.Re(),val.Im());
}