Solve PDEs with Events over Regions
Model thermostat-controlled heat generation in a room with three insulated walls and a glass front subject to the outside temperature.
In[1]:=

\[CapitalOmega] = Rectangle[{0, 0}, {3/2, 1}];
outsideTemp[t_] := 15 + 10*Sin[2 \[Pi] t/24];
kd = 0.78; Ld = 0.05;
\[CapitalGamma] = 
  NeumannValue[Ld/kd*(outsideTemp[t] - u[t, x, y]), {x == 0}];A heater load is ramped up or down at an event.
In[2]:=

heaterLoad = 26;
heater[upQ_, t_, tEvent_] := 
 If[upQ == 1, Min[20*Max[(t - tEvent), 0], 1], 
   1 - Min[20*Max[(t - tEvent - 1/8), 0], 1]]*heaterLoadThe PDE models heat diffusion through air while generating heat inside a circle  and losing heat through the glass window.
 and losing heat through the glass window.
In[3]:=

\[Rho] = 1.225; Cp = 1005.4;
With[{heating = heater[a[t], t, eventT[t]]},
  pde = D[u[t, x, y], t] - \[Rho]*Cp*Laplacian[u[t, x, y], {x, y}] == 
    If[(x - 1/2)^2 + (y - 1/2)^2 <= (2/10)^2, heating, 
      0] + \[CapitalGamma]];If the thermostat at position  measures a temperature below/above a trigger, and if the discrete variable
 measures a temperature below/above a trigger, and if the discrete variable  changed, the heater is switched on/off.
 changed, the heater is switched on/off.
In[4]:=

triggerLow = 18; triggerHigh = 20;
events = {a[0] == 1, eventT[0] == 0, 
   WhenEvent[
    u[t, 1.25, .25] < 
     triggerLow, {eventT[t], a[t]} -> {If[a[t] == 0, t, eventT[t]], 
      1}], WhenEvent[
    u[t, 1.25, .25] > 
     triggerHigh, {eventT[t], a[t]} -> {If[a[t] == 1, t, eventT[t]], 
      0}]};Monitor the time integration of the PDE with initial condition equal to the outside temperature.
In[5]:=

eqn = {pde, u[0, x, y] == outsideTemp[0], events};
res = Monitor[
  NDSolveValue[
   eqn, {u, a}, {t, 0, 2*24}, {x, y} \[Element] \[CapitalOmega], 
   DiscreteVariables -> {eventT[t], a[t]}, 
   EvaluationMonitor :> (monitor = Row[{"t = ", CForm[t]}])], monitor]Out[5]=

Visualize the temperature measured at the thermostat, the outside temperature, and the triggers for the heater. A blue background is shown where the heater is on.
In[6]:=

hp = Plot[ 25 res[[2]][t], {t, 0, 2*24}, Filling -> Bottom, 
  PlotStyle -> None]; Show[
 Plot[{res[[1]][t, 1.25, .25], outsideTemp[t], 18, 20}, {t, 0, 
   2*24}], hp]Out[6]=

























 
  
  
  
  
  
  
 