# internalHeatSource

Specify internal heat source for a thermal model

## Syntax

``internalHeatSource(thermalmodel,heatSourceValue)``
``internalHeatSource(thermalmodel,heatSourceValue,RegionType,RegionID)``
``heatSource = internalHeatSource(___)``

## Description

example

````internalHeatSource(thermalmodel,heatSourceValue)` specifies an internal heat source for the thermal model. This syntax declares that the entire geometry is a heat source. NoteUse `internalHeatSource` for specifying internal heat generators, that is, for specifying heat sources that belong to the geometry of the model. To specify a heat influx from an external source, use the `thermalBC` function with the `HeatFlux` parameter. ```

example

````internalHeatSource(thermalmodel,heatSourceValue,RegionType,RegionID)` specifies geometry regions of type `RegionType` with ID numbers in `RegionID` as heat sources. Always specify `heatSourceValue` first, then specify `RegionType` and `RegionID`.```
````heatSource = internalHeatSource(___)` returns the heat source object.```

## Examples

collapse all

Create a transient thermal model.

`thermalmodel = createpde('thermal','transient');`

Import the geometry.

`gm = importGeometry(thermalmodel,'SquareBeam.STL');`

Set thermal conductivity to `0.2`, mass density to `2700e-9`, and specific heat to `920`.

```thermalProperties(thermalmodel,'ThermalConductivity',0.2, ... 'MassDensity',2700e-9, ... 'SpecificHeat',920)```
```ans = ThermalMaterialAssignment with properties: RegionType: 'cell' RegionID: 1 ThermalConductivity: 0.2000 MassDensity: 2.7000e-06 SpecificHeat: 920 ```

Specify that the entire geometry generates heat at the rate `2e-4`.

`internalHeatSource(thermalmodel,2e-4)`
```ans = HeatSourceAssignment with properties: RegionType: 'cell' RegionID: 1 HeatSource: 2.0000e-04 ```

Create a steady-state thermal model.

`thermalModel = createpde('thermal','transient');`

Create the geometry.

```SQ1 = [3; 4; 0; 3; 3; 0; 0; 0; 3; 3]; D1 = [2; 4; 0.5; 1.5; 2.5; 1.5; 1.5; 0.5; 1.5; 2.5]; gd = [SQ1 D1]; sf = 'SQ1+D1'; ns = char('SQ1','D1'); ns = ns'; dl = decsg(gd,sf,ns); geometryFromEdges(thermalModel,dl);```

Set thermal conductivity to 50, mass density to 2500, and specific heat to 600.

```thermalProperties(thermalModel,'ThermalConductivity',50, ... 'MassDensity',2500, ... 'SpecificHeat',600);```

Specify that face 1 generates heat at 25.

`internalHeatSource(thermalModel,25,'Face',1)`
```ans = HeatSourceAssignment with properties: RegionType: 'face' RegionID: 1 HeatSource: 25 ```

Use a function handle to specify an internal heat source that depends on coordinates.

Create a thermal model for transient analysis and include the geometry. The geometry is a rod with a circular cross section. The 2-D model is a rectangular strip whose y-dimension extends from the axis of symmetry to the outer surface, and whose x-dimension extends over the actual length of the rod.

```thermalmodel = createpde('thermal','transient'); g = decsg([3 4 -1.5 1.5 1.5 -1.5 0 0 .2 .2]'); geometryFromEdges(thermalmodel,g);```

The heat is generated within the rod due to the radioactive decay. Therefore, the entire geometry is an internal nonlinear heat source and can be represented by a function of the y-coordinate, for example, $\mathit{q}=2000\mathit{y}$.

`q = @(location,state)2000*location.y;`

Specify the internal heat source for the transient model.

`internalHeatSource(thermalmodel,q)`
```ans = HeatSourceAssignment with properties: RegionType: 'face' RegionID: 1 HeatSource: @(location,state)2000*location.y ```

Use a function handle to specify an internal heat source that depends on time.

Create a thermal model for transient analysis and include the geometry. The geometry is a rectangular strip.

```thermalmodel = createpde('thermal','transient'); g = decsg([3 4 -1.5 1.5 1.5 -1.5 0 0 .2 .2]'); geometryFromEdges(thermalmodel,g);```

Specify the thermal properties of the rod.

```thermalProperties(thermalmodel,'ThermalConductivity',40,... 'MassDensity',7800,... 'SpecificHeat',500);```

Specify the boundary conditions and initial temperature.

```thermalBC(thermalmodel,'Edge',2,'Temperature',100); thermalBC(thermalmodel,'Edge',3,... 'ConvectionCoefficient',50,... 'AmbientTemperature',100); thermalIC(thermalmodel,0);```

Specify that the entire geometry generates heat at the rate 20000t during the first 500 seconds, and then the heat source turns off. For details, see Time-Dependent Heat Source Function.

`internalHeatSource(thermalmodel,@heatSource);`

Generate the mesh, solve the model using the solution times from 0 to 50000 seconds, and plot the results.

```generateMesh(thermalmodel); tfinal = 50000; tlist = 0:100:tfinal; result = solve(thermalmodel,tlist); T = result.Temperature; figure subplot(2,1,1) pdeplot(thermalmodel,'XYData',T(:,6),'Contour','on') axis equal title(sprintf('Temperature at %g s',tlist(6))) subplot(2,1,2) pdeplot(thermalmodel,'XYData',T(:,end),'Contour','on') axis equal title(sprintf('Temperature at %g s',tfinal))```

Always ensure that your function returns a matrix of `NaN` of the correct size when `state.time` is `NaN`. The solver properly recognizes a time-dependent problem by passing `NaN` state values and looking for returned `NaN` values. Without this condition, the solver might fail or return incorrect results.

```internalHeatSource(thermalmodel,@heatSourceInvalid); result = solve(thermalmodel,tlist); T = result.Temperature; figure subplot(2,1,1) pdeplot(thermalmodel,'XYData',T(:,6),'Contour','on') axis equal title(sprintf('Temperature at %g s',tlist(6))) subplot(2,1,2) pdeplot(thermalmodel,'XYData',T(:,end),'Contour','on') axis equal title(sprintf('Temperature at %g s',tfinal))```

Time-Dependent Heat Source Function

```function Q = heatSource(location,state) Q = zeros(1,numel(location.x)); if(isnan(state.time)) % Returning a NaN when time=NaN tells the solver that the heat source is a function of time. Q(1,:) = NaN; return end if state.time < 500 Q(1,:) = 20000*state.time; end end function Q = heatSourceInvalid(location,state) % No checks for NaN Q = zeros(1,numel(location.x)); if state.time < 500 Q(1,:) = 20000*state.time; end end```

## Input Arguments

collapse all

Thermal model, specified as a `ThermalModel` object. The model contains the geometry, mesh, thermal properties of the material, internal heat source, boundary conditions, and initial conditions.

Example: `thermalmodel = createpde('thermal','steadystate')`

Geometric region type, specified as `'Face'` for a 2-D model or `'Cell'` for a 3-D model.

Example: `internalHeatSource(thermalmodel,25,'Cell',1)`

Data Types: `char` | `string`

Geometric region ID, specified as a vector of positive integers. Find the region IDs by using `pdegplot`.

Example: `internalHeatSource(thermalmodel,25,'Cell',1:3)`

Data Types: `double`

Heat source value, specified as a number or a function handle. Use a function handle to specify the internal heat source that depends on space, time, or temperature. For details, see More About.

Example: `internalHeatSource(thermalmodel,25)`

Data Types: `double` | `function_handle`

### Output Arguments

collapse all

Handle to heat source, returned as an object. `heatSourceValue` associates the heat source value with the geometric region.

## More About

collapse all

### Specifying Nonconstant Parameters of a Thermal Model

Use a function handle to specify these thermal parameters when they depend on space, temperature, and time:

• Thermal conductivity of the material

• Mass density of the material

• Specific heat of the material

• Internal heat source

• Temperature on the boundary

• Heat flux through the boundary

• Convection coefficient on the boundary

• Radiation emissivity coefficient on the boundary

• Initial temperature

For example, use function handles to specify the thermal conductivity, internal heat source, convection coefficient, and initial temperature for this model.

```thermalProperties(model,'ThermalConductivity',@myfun) internalHeatSource(model,'Face',2,@myfun) thermalBC(model,'Edge',[3,4], ... 'ConvectionCoefficient',@myfun, ... 'AmbientTemperature',27) thermalIC(model,@myfun)```

The function must be of the form:

`function thermalVal = myfun(location,state)`

The solver passes the `location` and `state` data to your function:

• `location` — A structure containing these fields:

• `location.x` — The x-coordinate of the point or points

• `location.y` — The y-coordinate of the point or points

• `location.z` — For a 3-D or an axisymmetric geometry, the z-coordinate of the point or points

• `location.r` — For an axisymmetric geometry, the r-coordinate of the point or points

Furthermore, for boundary conditions, the solver passes these data in the `location` structure:

• `location.nx`x-component of the normal vector at the evaluation point or points

• `location.ny`y-component of the normal vector at the evaluation point or points

• `location.nz` — For a 3-D or an axisymmetric geometry, z-component of the normal vector at the evaluation point or points

• `location.nz` — For an axisymmetric geometry, z-component of the normal vector at the evaluation point or points

• `state` — A structure containing these fields for transient or nonlinear problems:

• `state.u` — Temperatures at the corresponding points of the location structure

• `state.ux` — Estimates of the x-component of temperature gradients at the corresponding points of the location structure

• `state.uy` — Estimates of the y-component of temperature gradients at the corresponding points of the location structure

• `state.uz` — For a 3-D or an axisymmetric geometry, estimates of the z-component of temperature gradients at the corresponding points of the location structure

• `state.ur` — For an axisymmetric geometry, estimates of the r-component of temperature gradients at the corresponding points of the location structure

• `state.time` — Time at evaluation points

Thermal material properties (thermal conductivity, mass density, and specific heat) and internal heat source get these data from the solver:

• `location.x`, `location.y`, `location.z`, `location.r`

• Subdomain ID

• `state.u`, `state.ux`, `state.uy`, `state.uz`, `state.r`, `state.time`

Boundary conditions (temperature on the boundary, heat flux, convection coefficient, and radiation emissivity coefficient) get these data from the solver:

• `location.x`, `location.y`, `location.z`, `location.r`

• `location.nx`, `location.ny`, `location.nz`, `location.nr`

• `state.u`, `state.time`

Initial temperature gets the following data from the solver:

• `location.x`, `location.y`, `location.z`, `location.r`

• Subdomain ID

For all thermal parameters, except for thermal conductivity, your function must return a row vector `thermalVal` with the number of columns equal to the number of evaluation points, for example, ```M = length(location.y)```.

For thermal conductivity, your function must return a matrix `thermalVal` with number of rows equal to 1, `Ndim`, `Ndim*(Ndim+1)/2`, or `Ndim*Ndim`, where `Ndim` is 2 for 2-D problems and 3 for 3-D problems. The number of columns must equal the number of evaluation points, for example, ```M = length(location.y)```. For details about dimensions of the matrix, see c Coefficient for specifyCoefficients.

If properties depend on the time or temperature, ensure that your function returns a matrix of `NaN` of the correct size when `state.u` or `state.time` are `NaN`. Solvers check whether a problem is time dependent by passing `NaN` state values and looking for returned `NaN` values.

## See Also

Introduced in R2017a