function mtl = electromagneticProperties(self,mtlPropsInput)
%electromagneticProperties - Assign material properties to ElectromagneticModel
%   mtl = electromagneticProperties(emagmodel,PROPERTY,VALUE) assigns
%   material properties to an electromagnetic model. Specify VALUE as a
%   number for constant material properties or a MATLAB function for a
%   nonconstant material.
%
%    PROPERTY                  VALUE
%-------------------------------------------------------------------------
%   'RelativePermittivity'    Relative permittivity of the material.
%   'RelativePermeability'    Relative permeability of the material.
%   'Conductivity'            Conductivity of the material.
%
%  mtl = electromagneticProperties(__,'Face',FACEID) assigns properties for
%  the specified face of a 2-D geometry. FACEID is a positive integer not
%  exceeding the number of faces in the geometry.
%
%  mtl = electromagneticProperties(__,'Cell',CELLID) assigns properties for
%  the specified cell of a 3-D geometry. CELLID is a positive integer not
%  exceeding the number of cells in the geometry.
%
%   Example 1: Specify relative permittivity for an electrostatic analysis.
%
%       emagmodel = createpde('electromagnetic','electrostatic');
%       gm = importGeometry(emagmodel,'PlateHolePlanar.stl');
%       mtl = electromagneticProperties(emagmodel,'RelativePermittivity',1);
%
%    See also pde.ElectromagneticModel, pde.ElectromagneticModel/electromagneticSource
%             pde.ElectromagneticModel/electromagneticBC, pde.ElectromagneticModel/solve

%    Copyright 2020-2021 The MathWorks, Inc.

arguments
    self pde.ElectromagneticModel
    mtlPropsInput.Face
    mtlPropsInput.Cell
    mtlPropsInput.RelativePermittivity
    mtlPropsInput.RelativePermeability
    mtlPropsInput.Conductivity
end

if isempty(self.Geometry)
    error(message('pde:pdeModel:noGeometry'));
end

validDomain = self.validDomain;
invalidDomain = setdiff(self.possibleDomainRegions,validDomain);
maxDomainID = self.maxDomainID;

% Error if properties are specified for cells of 2-D or faces of 3-D geometry.
if isfield(mtlPropsInput,invalidDomain) && ~isempty(mtlPropsInput.(invalidDomain))
    error(message('pde:ElectromagneticModel:invalidRegionForMaterial'))
end

% Global material assignment.
if ~isfield(mtlPropsInput,validDomain)
    mtlPropsInput.(validDomain) = 1:maxDomainID;
end

self.isValidEntityID(mtlPropsInput.(validDomain));

if any(mtlPropsInput.(validDomain) > maxDomainID)
    error(message(['pde:pdeModel:invalid',validDomain,'Index']));
end

if isempty(self.MaterialProperties)
    self.MaterialProperties  = pde.ElectromagneticMaterialAssignmentRecords(self);
    self.MaterialProperties.MaterialAssignments = pde.ElectromagneticMaterialAssignment.empty;
end

mtl = pde.ElectromagneticMaterialAssignment(self.MaterialProperties,mtlPropsInput);
self.MaterialProperties.MaterialAssignments(end+1) = mtl;
end
