function tf = globalHeatSource(self,varargin)
% globalHeatSource  True if consistent across the geometric domain

% Copyright 2016-2020 The MathWorks, Inc.

narginchk(1,2); 
tf = true;

if ~self.heatSourceSpecified()   
    tf = false;
    return;
end


if self.nRegion==1
    % 3-D geometry with one cell or 2-D geometry with one face
    return;
end

if nargin == 2
   coefname =  varargin{1};
   iscoef = false;
   if ischar(coefname)
       iscoef = (strcmp(coefname, 'HeatSource'));
   end
   if ~iscoef
      error(message('pde:heatSourceSpecification:invalidHeatSrcName')); 
   end
end

[location, state] = self.ParentModel.getSampleLocState(1);
thiscoef  = self.findHeatSource(self.qregiontype, 1);
thisQVal  = evaluateFcnHdlVal(thiscoef.HeatSource,location,state);

for nsd = 2:self.nRegion
    othercoef = self.findHeatSource(self.qregiontype, nsd);
    if (~self.allHeatSourceNumeric)
        [location, state] = self.ParentModel.getSampleLocState(nsd);
        otherQVal  = evaluateFcnHdlVal(othercoef.HeatSource,location,state);
        if ( any(otherQVal(:) ~= thisQVal(:)))  
            tf = false;
            return;
        end
    else
        if ~thiscoef.sameCoefficients(othercoef,varargin{:})
            tf = false;
            return;
        end
    end
end
 

end


% Helper function to find values of coefficients returned from user defined
% function.
function res = evaluateFcnHdlVal(coef, location, state)
if ~isa(coef, 'function_handle')
    res = 0;
    return
end
res = coef(location, state);
end

