function mp = findElectromagneticProperties(self,regionInfo)
% findElectromagneticProperties - Find electromagnetic material properties assigned to a geometric region
% EMA = findElectromagneticProperties(MPR,REGIONTYPE, REGIONID) returns the
% ElectromagneticMaterialAssignment object, EMA, assigned to the given
% REGIONTYPE and REGIONID. Here, MPR is the MaterialProperties property of
% the ElectromagneticModel object, REGIONTYPE is 'Face' for a 2-D model or
% 'Cell' for a 3-D model, and REGIONID is the ID of the geometric entity.
%
% % Example: Find electromagnetic properties assigned to a 2-D geometry
%
% % Create an ElectromagneticModel object and include a geometry
% model = createpde('electromagnetic','magnetostatic');
% geometryFromEdges(model,@lshapeg);
% pdegplot(model,'EdgeLabels','on')
%
% % Assign different values of relative permeability to different faces.
% electromagneticProperties(model,'Face',1,'RelativePermeability',1.00);
% electromagneticProperties(model,'Face',2,'RelativePermeability',1.01);
% electromagneticProperties(model,'Face',3,'RelativePermeability',1.05);
%
% % Check the structural properties specification for face 2.
% mF2= findElectromagneticProperties(model.MaterialProperties,'Face',2)
%
%    See also pde.ElectromagneticModel/electromagneticProperties

% Copyright 2020-2021 The MathWorks, Inc.

arguments
    self pde.ElectromagneticMaterialAssignmentRecords
    regionInfo.Face
    regionInfo.Cell
end

narginchk(3,3)

if isempty(self.ParentModel.Geometry)
    mp = pde.ElectromagneticMaterialAssignment.empty;
    return;
end
validDomain = self.ParentModel.validDomain;
maxDomainID = self.ParentModel.maxDomainID;

if ~isfield(regionInfo,validDomain)
    inputRegionType = fields(regionInfo);
    error(message(['pde:materialPropertySpecification:invalidQueryRegionType',inputRegionType{1}]));
end

queryRegionID = regionInfo.(validDomain);
if any(queryRegionID > maxDomainID)
    error(message('pde:materialPropertySpecification:invalidRegionID'));
end

numassigns = numel(self.MaterialAssignments);
numqueries = numel(queryRegionID);
mp = pde.ElectromagneticMaterialAssignment.empty(0,numel(queryRegionID));
for i = 1:numqueries
    rid = queryRegionID(i);
    mpfound = false;
    for j = numassigns:-1:1
        thisca =  self.MaterialAssignments(j);
        if strcmp(validDomain,thisca.RegionType) && ismember(rid, thisca.RegionID)
            mp(i) = thisca;
            mpfound = true;
            break
        end
    end
    if ~mpfound && ~self.EmptyAllowed
        error(message('pde:materialPropertySpecification:entitiesWithoutMtlProps'));
    end
end
end

