DEFINE_SOURCE(heater_pow_PID,c,t,dS,eqn) /* PID controller */
{
Domain *domain1;
domain1 = Get_Domain(1);
int ID_heat=6; /* Get the ID for the heater */
int ID_emit=7; /* Get the ID for the emitter */
int axi1 = 1; /* 1 if axisymmetric, 0 if cartesian */
Thread *tc_heat=Lookup_Thread(domain1,ID_heat); /* Get the thread for cells in the heater */
Thread *tc_emit=Lookup_Thread(domain1,ID_emit); /* Get the thread for cells in the emitter */
real vol_tot_heat = 0;
real vol_tot_emit = 0;
real cell_vol = 0;
real temp_emit = 0;
real temp_emit_M1 = 0;
real dtemp_dt = 0;
real dt = CURRENT_TIMESTEP;
real temp_target = 473.15; /* 200 C */
real heater_power_max = 0.5; /* in Watt */
real heater_power_ss = 0;
real dheat_dT = 0;
real heater_power = 0;
/* Controller constants */
real kp = 20*2.857e-3;
real kd = 2*kp;
real ki = 0*kp;
real axi_k1 = 0;
real source = 0;
cell_t c1;
face_t f1;
axi_k1 = (axi1==1)? 2*M_PI : 1;
begin_c_loop(c1,tc_heat) /* Obtain total heater volume */
{
vol_tot_heat += C_VOLUME(c1,tc_heat)*axi_k1;
}
end_c_loop(c1,tc_heat)
begin_c_loop(c1,tc_emit) /* Obtain emitter temperature at current and previous timesteps */
{
cell_vol = C_VOLUME(c1,tc_emit)*axi_k1;
vol_tot_emit += cell_vol;
temp_emit += C_T(c1,tc_emit)*cell_vol;
temp_emit_M1 += C_T_M1(c1,tc_emit)*cell_vol;
}
end_c_loop(c1,tc_emit)
temp_emit = temp_emit/vol_tot_emit;
temp_emit_M1 = temp_emit_M1/vol_tot_emit;
dtemp_dt = (temp_emit - temp_emit_M1)/dt;
dheat_dT = 2*4.382e06*temp_emit-0.001093;
heater_power = -kp*(temp_emit - temp_target) - kd*dtemp_dt + ki*C_UDMI(c,t,1);
heater_power = (heater_power >= 0)? heater_power : 0;
heater_power = (heater_power<=heater_power_max)? heater_power: heater_power_max;
source = heater_power/vol_tot_heat;
dS[eqn] = 0;
/* Enter UDMI 1 for error integral of PID controller C_UDMI(1) = int(e dt,0,t-1) */
C_UDMI(c,t,1) = C_UDMI(c,t,1) + (temp_target-temp_emit)*dt;
/* Enter into UDMI for post_processing */
C_UDMI(c,t,0) = heater_power;
return source;
}
The subroutine calculates the average of the emitter temperature which in this case is the temperature to be controlled. Also, it requires the allocation of two user-defined memory. Other than that, it's all self-explanatory.
I dedicate you this song https://www.youtube.com/watch?v=u1xrNaTO1bI Great thanks, m8!
ReplyDelete