# Wolfram Language™

## 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:= ```\[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:= ```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]]*heaterLoad```

The PDE models heat diffusion through air while generating heat inside a circle and losing heat through the glass window.

In:= ```\[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 changed, the heater is switched on/off.

In:= ```triggerLow = 18; triggerHigh = 20; events = {a == 1, eventT == 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:= ```eqn = {pde, u[0, x, y] == outsideTemp, 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= 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:= ```hp = Plot[ 25 res[][t], {t, 0, 2*24}, Filling -> Bottom, PlotStyle -> None]; Show[ Plot[{res[][t, 1.25, .25], outsideTemp[t], 18, 20}, {t, 0, 2*24}], hp]```
Out= 