function mp = findHeatSource(self,varargin)
% findHeatSource - Find heat source assigned to a geometric region 
% HSA = findHeatSource(HSR,REGIONTYPE, REGIONID) returns the
% HeatSourceAssignment object, HSA, that defines the heat source assigned
% to the given REGIONTYPE and REGIONID. Here HSR is the HeatSources
% 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 Heat Sources 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)
%
% % Specify different heat source for each cell.
% internalHeatSource(thermalmodel,10,'Cell',1);
% internalHeatSource(thermalmodel,20,'Cell',2);
% internalHeatSource(thermalmodel,30,'Cell',3);
%
% % Check the heat source specification for cell 1.
% hsaCell1 = findHeatSource(thermalmodel.HeatSources,'Cell',1)
%
%   See also pde.ThermalModel/internalHeatSource

% Copyright 2016-2021 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.addParameter('label', [], @pde.EquationModel.ValidateLabelVal);
parser.parse(varargin{:});

if ~isempty(parser.Results.label)
    queryLabel = string(parser.Results.label);
    allExistingLabels={self.HeatSourceAssignments.Label};
    allExistingLabels(cellfun(@isempty,allExistingLabels))=[];
    allExistingLabels = string(allExistingLabels);
    
    if ~ismember(queryLabel,allExistingLabels)
    error(message('pde:linearize:NonExistingLabel',queryLabel));
    end
    numassigns = numel(self.HeatSourceAssignments);
    for j = numassigns:-1:1
       thisbc =  self.HeatSourceAssignments(j);       
       if queryLabel == string(thisbc.Label)       
          mp = thisbc;
          break
       end
    end
    return;
end

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


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

numassigns = numel(self.HeatSourceAssignments);
numqueries = numel(qregionid);

if ~heatSourceSpecified(self,qregiontype,qregionid)
mp = [];
return;
end
       
mp = repmat(self.HeatSourceAssignments(1),size(qregionid));
for i = 1:numqueries
    rid = qregionid(i);
    %mpfound = false;
    for j = numassigns:-1:1
       thisca =  self.HeatSourceAssignments(j);   
       if ~heatSourceSpecified(self,qregiontype,rid)
           continue
       end
       if strcmp(qregiontype,thisca.RegionType) && ismember(rid, thisca.RegionID)        
          mp(i) = thisca;
     %     mpfound = true;
          break
       end
    end
% It is not necessary to have heat source assignment to all region.
%     if ~mpfound
%       error(message('pde:heatSourceSpecification:entitiesWithoutCoefficients'));
%     end   
end

end

% LocalWords:  HSA HSR REGIONTYPE REGIONID multicylinder ZOffset thermalmodel
% LocalWords:  createpde hsa mpfound
