function mp = findThermalProperties(self,varargin)
% findThermalProperties - Find thermal properties assigned to a geometric region
% TMA = findThermalProperties(MPR,REGIONTYPE, REGIONID) returns the
% ThermalMaterialAssignment object, TMA, that defines the thermal
% conductivity, mass density, and specific heat assigned to the given
% REGIONTYPE and REGIONID. Here MPR is the MaterialProperties property of
% the thermal model object, REGIONTYPE is 'Cell' for a 3-D model or 'Face'
% for a 2-D model, and REGIONID is the ID of the geometric entity.
%
% % Example: Find Thermal Conductivity for Cells of 3-D Geometry
%
% % Create a geometry that consists of three stacked cylinders and include
% % the geometry in a thermal model.
% gm = multicylinder(10,[1 2 3],'ZOffset',[0 1 3]);
% thermalmodel = createpde('thermal');
% thermalmodel.Geometry = gm;
% pdegplot(thermalmodel,'CellLabels','on','FaceAlpha',0.5)
%
% % Assign different thermal conductivity to each cell.  
% thermalProperties(thermalmodel,'ThermalConductivity',10,'Cell',1);
% thermalProperties(thermalmodel,'ThermalConductivity',20,'Cell',2);
% thermalProperties(thermalmodel,'ThermalConductivity',30,'Cell',3);
%
% % Check the thermal properties specification for cell 1.
% tmaCell1 = findThermalProperties(thermalmodel.MaterialProperties,'Cell',1)
%
%    See also pde.ThermalModel/thermalProperties

% Copyright 2016-2017 The MathWorks, Inc.

if nargin > 1
    [varargin{:}] = convertStringsToChars(varargin{:});
end

narginchk(3,3);
if isempty(self.ParentModel.Geometry)
    return;
end

parser = inputParser;
parser.addParameter('cell', [], @pde.EquationModel.isValidEntityID);
parser.addParameter('face', [], @pde.EquationModel.isValidEntityID);
parser.parse(varargin{:});

if ~ismember('face', parser.UsingDefaults)
    if ~self.ParentModel.IsTwoD
        error(message('pde:materialPropertySpecification:invalidQueryRegionTypeFace'));
    end
    qregiontype = 'face';
    qregionid = parser.Results.face;
    maxid = self.ParentModel.Geometry.NumFaces;
else
    if self.ParentModel.IsTwoD
        error(message('pde:materialPropertySpecification:invalidQueryRegionTypeCell'));
    end
    qregiontype = 'cell';
    qregionid = parser.Results.cell;
    maxid = self.ParentModel.Geometry.NumCells;
end

if any(qregionid > maxid)
    error(message('pde:materialPropertySpecification:invalidRegionID'));
end

numassigns = numel(self.MaterialAssignments);
numqueries = numel(qregionid);
mp = repmat(self.MaterialAssignments(1),size(qregionid));
for i = 1:numqueries
    rid = qregionid(i);
    mpfound = false;
    for j = numassigns:-1:1
        thisca =  self.MaterialAssignments(j);
        if strcmp(qregiontype,thisca.RegionType) && ismember(rid, thisca.RegionID)
            mp(i) = thisca;
            mpfound = true;
            break
        end
    end
    if ~mpfound
        error(message('pde:materialPropertySpecification:entitiesWithoutMtlProps'));
    end
end

end

% LocalWords:  TMA REGIONTYPE REGIONID multicylinder ZOffset thermalmodel
% LocalWords:  createpde tma Mtl
