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

% Copyright 2016-2020 The MathWorks, Inc.

narginchk(1,2);
tf = true;

if ~self.materialPropertiesSpecified()
    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, 'SpecificHeat') ||  strcmp(coefname, 'MassDensity') || ...
            strcmp(coefname, 'ThermalConductivity') );
    end
    if ~iscoef
        error(message('pde:materialPropertySpecification:invalidMtlPropName'));
    end
end



[location, state] = self.ParentModel.getSampleLocState(1);
thiscoef  = self.findThermalProperties(self.qregiontype, 1);
thisKVal  = evaluateFcnHdlVal(thiscoef.ThermalConductivity,location,state);
thisMVal  = evaluateFcnHdlVal(thiscoef.MassDensity,location,state);
thisCPVal = evaluateFcnHdlVal(thiscoef.SpecificHeat,location,state);

for nsd = 2:self.nRegion
    othercoef = self.findThermalProperties(self.qregiontype, nsd);
    if ~thiscoef.sameCoefficients(othercoef, varargin{:})
        tf = false;
        return;
    end    
    [location, state] = self.ParentModel.getSampleLocState(nsd);
    if (~self.allMaterialPropertiesNumeric)
        location.subdomain = nsd;
        otherKVal  = evaluateFcnHdlVal(othercoef.ThermalConductivity,location,state);
        otherMVal  = evaluateFcnHdlVal(othercoef.MassDensity,location,state);
        otherCPVal = evaluateFcnHdlVal(othercoef.SpecificHeat,location,state);
        if (...
                ( any(thisKVal(:) ~= otherKVal(:))) ||...
                ( any(thisMVal(:) ~= otherMVal(:))) ||...
                ( any(otherCPVal(:) ~= thisCPVal(:))) ...
                )
            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

