function mp = findStructuralProperties(self,varargin)
% findStructuralProperties - Find structural material properties assigned to a geometric region
% SMA = findStructuralProperties(MPR,REGIONTYPE, REGIONID) returns the
% StructuralMaterialAssignment object, SMA, that defines the Young's
% modulus, Poisson's ratio, and mass density assigned to the given
% REGIONTYPE and REGIONID. Here MPR is the MaterialProperties property of
% the structural 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 Young's modulus and Poisson's ratio assigned to Cells of 3-D Geometry
% 
% % Create a geometry that consists of three stacked cylinders and include
% % the geometry in a structural model.
% gm = multicylinder(10,[1 2 3],'ZOffset',[0 1 3]);
% structuralmodel = createpde('structural','static-solid');
% structuralmodel.Geometry = gm;
% pdegplot(structuralmodel,'CellLabels','on','FaceAlpha',0.5)
% 
% % Assign different values of Young's modulus and Poisson's ratio to each cell.
% structuralProperties(structuralmodel,'Cell',1,'YoungsModulus',200E9,...
%                                                     'PoissonsRatio',0.3);
% structuralProperties(structuralmodel,'Cell',2,'YoungsModulus',210E9,...
%                                                     'PoissonsRatio',0.3);
% structuralProperties(structuralmodel,'Cell',3,'YoungsModulus',110E9,...
%                                                    'PoissonsRatio',0.35);
% 
% % Check the structural properties specification for cell 1.
% mC1 = findStructuralProperties(structuralmodel.MaterialProperties,'Cell',1)
% 
%    See also pde.StructuralModel/structuralProperties


% Copyright 2017 The MathWorks, Inc.

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

