# electromagneticSource

Specify current density or charge density for electromagnetic model

## Syntax

``electromagneticSource(emagmodel,'CurrentDensity',J)``
``electromagneticSource(emagmodel,'ChargeDensity',rho)``
``electromagneticSource(___,RegionType,RegionID)``
``emagSource = electromagneticSource(___)``

## Description

example

````electromagneticSource(emagmodel,'CurrentDensity',J)` specifies current density for a magnetostatic model.```

example

````electromagneticSource(emagmodel,'ChargeDensity',rho)` specifies charge density for an electrostatic model.```

example

````electromagneticSource(___,RegionType,RegionID)` specifies the current or charge density for the specified geometry region. Use this syntax with any of the input arguments from previous syntaxes.```

example

````emagSource = electromagneticSource(___)` returns the electromagnetic source object.```

## Examples

collapse all

Specify current density on the entire geometry for a magnetostatic analysis.

```emagmodel = createpde('electromagnetic','magnetostatic'); importGeometry(emagmodel,'PlateHolePlanar.stl'); electromagneticSource(emagmodel,'CurrentDensity',10)```
```ans = ElectromagneticSourceAssignment with properties: RegionType: 'Face' RegionID: 1 ChargeDensity: [] CurrentDensity: 10 ```

Specify charge density on individual faces in an electrostatic analysis.

Create an electromagnetic model for an electrostatic analysis.

`emagmodel = createpde('electromagnetic','electrostatic');`

Create a 2-D geometry with two faces. First, import and plot a 2-D geometry representing a plate with a hole.

```gm = importGeometry(emagmodel,'PlateHolePlanar.stl'); pdegplot(gm,'EdgeLabels','on','FaceLabels','on')```

Then, fill the hole by adding a face and plot the resulting geometry.

```gm = addFace(gm,5); pdegplot(gm,'FaceLabels','on')```

Specify charge density values separately for faces 1 and 2.

`sc1 = electromagneticSource(emagmodel,'Face',1,'ChargeDensity',0.3)`
```sc1 = ElectromagneticSourceAssignment with properties: RegionType: 'Face' RegionID: 1 ChargeDensity: 0.3000 CurrentDensity: [] ```
`sc2 = electromagneticSource(emagmodel,'Face',2,'ChargeDensity',0.28)`
```sc2 = ElectromagneticSourceAssignment with properties: RegionType: 'Face' RegionID: 2 ChargeDensity: 0.2800 CurrentDensity: [] ```

Use a function handle to specify a charge density that depends on the coordinates.

Create an electromagnetic model for electrostatic analysis.

`emagmodel = createpde('electromagnetic','electrostatic');`

Create a unit circle geometry and include it in the model.

`geometryFromEdges(emagmodel,@circleg);`

Specify the charge density as a function of the x- and y-coordinates, $\rho =0.3\sqrt{{\mathit{x}}^{2}+{\mathit{y}}^{2}}$.

```rho = @(region,state)0.3.*sqrt(region.x.^2 + region.y.^2); electromagneticSource(emagmodel,'ChargeDensity',rho)```
```ans = ElectromagneticSourceAssignment with properties: RegionType: 'Face' RegionID: 1 ChargeDensity: @(region,state)0.3.*sqrt(region.x.^2+region.y.^2) CurrentDensity: [] ```

## Input Arguments

collapse all

Electromagnetic model, specified as an `ElectromagneticModel` object. The model contains a 2-D geometry, a mesh, the electromagnetic properties of the material, the electromagnetic sources, and the boundary conditions.

Example: ```emagmodel = createpde('electromagnetic','electrostatic')```

Current density, specified as a real number or a function handle. Use a function handle to specify a current density that depends on the coordinates or on the solution. For details, see Specifying Nonconstant Parameters of Electromagnetic Model.

Data Types: `double` | `function_handle`

Charge density, specified as a real number or a function handle. Use a function handle to specify a charge density that depends on the coordinates or on the solution. For details, see Specifying Nonconstant Parameters of Electromagnetic Model.

Data Types: `double` | `function_handle`

Geometric region type, specified as `'Face'`.

Data Types: `char` | `string`

Face ID, specified as a vector of positive integers. Find the face IDs by using `pdegplot` with the `'FaceLabels'` name-value argument set to `'on'`.

Example: `electromagneticSource(emagmodel,'CurrentDensity',10,'Face',1:3)`

Data Types: `double`

## Output Arguments

collapse all

Handle to the electromagnetic source, returned as an `ElectromagneticSourceAssignment` object. For more information, see ElectromagneticSourceAssignment Properties.

collapse all

### Specifying Nonconstant Parameters of Electromagnetic Model

In Partial Differential Equation Toolbox™, use a function handle to specify these electromagnetic parameters when they depend on the coordinates or solution:

• Relative permittivity of the material

• Relative permeability of the material

• Charge density as source

• Current density as source

• Voltage at the boundary

• Magnetic potential at the boundary

For example, use function handles to specify the relative permittivity, charge density, and voltage at the boundary for this model.

```electromagneticProperties(emagmodel, ... 'RelativePermittivity', ... @myfunPermittivity) electromagneticSource(emagmodel, ... 'ChargeDensity',@myfunCharge, ... 'Face',2) electromagneticBC(emagmodel, ... 'Voltage',@myfunBC, ... 'Edge',2)```

The function must be of the form:

`function emagVal = 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 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

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

• `state.u` — Electric or magnetic potential at the corresponding points of the location structure

• `state.ux` — Estimates of the x-component of the electric or magnetic field at the corresponding points of the location structure

• `state.uy` — Estimates of the y-component of the electric or magnetic field at the corresponding points of the location structure

• `state.uz` — For an axisymmetric geometry, estimates of the z-component of the electric or magnetic field at the corresponding points of the location structure

• `state.ur` — For an axisymmetric geometry, estimates of the r-component of the electric or magnetic field at the corresponding points of the location structure

Electromagnetic material properties (relative permittivity or relative permeability) and electromagnetic source (charge density or current density) 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.ur`

Boundary conditions (voltage or magnetic potential on the boundary) get these data from the solver:

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

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

• `state.u`

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

Introduced in R2021a

## Support

#### Try MATLAB, Simulink, and Other Products

Get trial now