


obtain crystal definition from database
Syntax: cdef = obtainCrystal(name)
Return a crystal definition that is stored in the local database. All
crystals used during the elk-project can be found here.
obtainCrystal('init') is used to initialize the database. You should use
this whenever you added an example to this library.
obtainCrystal without any argument returns the possible options,
including the information, whether it is a generic example. Information
is returned as a struct array: cdef(i).crystalName cdef(i).isGeneric.
If no output variable is specified, the possible names are printed on
the screen.
obtainCrystal(..., par) is used for generic crystal definitions for which,
based on a required set of facets that fixes the crystal position, the
additional set is specified. Par may be a logic index vector, of a
integer number, which decodes this vector. The correct length can be
determined by the common informations, obtained by getCrystal(name).
For templates to generate a new crystal definition, use 'database/cuboid'
or 'database/generic_potash_alum' as templates.
See also: viewCrystal

0001 function cdef = obtainCrystal(crystalName, genericCode, flagEnforce) 0002 % obtain crystal definition from database 0003 % 0004 % Syntax: cdef = obtainCrystal(name) 0005 % 0006 % Return a crystal definition that is stored in the local database. All 0007 % crystals used during the elk-project can be found here. 0008 % 0009 % obtainCrystal('init') is used to initialize the database. You should use 0010 % this whenever you added an example to this library. 0011 % 0012 % obtainCrystal without any argument returns the possible options, 0013 % including the information, whether it is a generic example. Information 0014 % is returned as a struct array: cdef(i).crystalName cdef(i).isGeneric. 0015 % If no output variable is specified, the possible names are printed on 0016 % the screen. 0017 % 0018 % obtainCrystal(..., par) is used for generic crystal definitions for which, 0019 % based on a required set of facets that fixes the crystal position, the 0020 % additional set is specified. Par may be a logic index vector, of a 0021 % integer number, which decodes this vector. The correct length can be 0022 % determined by the common informations, obtained by getCrystal(name). 0023 % 0024 % For templates to generate a new crystal definition, use 'database/cuboid' 0025 % or 'database/generic_potash_alum' as templates. 0026 % 0027 % See also: viewCrystal 0028 0029 % The elk-library: convex geometry applied to crystallization modeling. 0030 % Copyright (C) 2012 Alexander Reinhold 0031 % 0032 % This program is free software: you can redistribute it and/or modify it 0033 % under the terms of the GNU General Public License as published by the 0034 % Free Software Foundation, either version 3 of the License, or (at your 0035 % option) any later version. 0036 % 0037 % This program is distributed in the hope that it will be useful, but 0038 % WITHOUT ANY WARRANTY; without even the implied warranty of 0039 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0040 % General Public License for more details. 0041 % 0042 % You should have received a copy of the GNU General Public License along 0043 % with this program. If not, see <http://www.gnu.org/licenses/>. 0044 0045 %% input handling 0046 if ~exist('crystalName', 'var') 0047 crystalName = ''; 0048 end 0049 if ~exist('genericCode', 'var') 0050 genericCode = []; 0051 end 0052 if ~exist('flagEnforce', 'var') 0053 flagEnforce = false; 0054 end 0055 0056 %% handle coding 0057 if islogical(genericCode) 0058 % cast [1 0 1 1] to [1 3 4] - 1 = [0 2 3] 0059 genericCode = uint32(find(genericCode) - 1); 0060 end 0061 if numel(genericCode) > 1 && ~isa(genericCode, 'double') 0062 % cast [0 2 3] to [1 4 8] (binary encoding) and sum up 0063 genericCode = sum(2.^genericCode); 0064 end 0065 % create file name (even though, we not yet know if the case is generic) 0066 if isempty(genericCode) 0067 fileName = [crystalName '.mat']; 0068 else 0069 fileName = [crystalName '_' num2str(genericCode) '.mat']; 0070 end 0071 0072 %% database handling 0073 % try to load crystal from previous calculations (speedup) 0074 if ~flagEnforce && ~isempty(crystalName) && ~strcmpi(crystalName, 'init') 0075 try 0076 cdef = obtainData(fileName(1:end-4), ... 0077 'database', ['.' filesep 'data-crystal'], 'type', 'mat'); 0078 % if the above is not an error, we already have what we wanted 0079 return 0080 catch 0081 flagEnforce = true; 0082 end 0083 else 0084 % if init command, enforce second call for database 0085 flagEnforce = true; 0086 end 0087 % use database definition files 0088 if flagEnforce 0089 cdef = obtainData(crystalName, 'prompt', nargout == 0); 0090 end 0091 0092 %% evaluate output 0093 if ~exist('crystalName', 'var') || isempty(crystalName) || ... 0094 strcmpi(crystalName, 'init') 0095 % case - no cdef requested 0096 if nargout == 0 0097 clear cdef 0098 end 0099 return 0100 elseif cdef.isGeneric && ~isempty(genericCode) 0101 0102 for iMiller = 1:size(cdef.millerGeneric, 1) 0103 if bitand(genericCode, 2^(iMiller - 1) ) ~= 0 0104 cdef.millerDefined(end+1,:) = cdef.millerGeneric(iMiller, :); 0105 end 0106 end 0107 end 0108 cdef = extendCdef(cdef); 0109 0110 if ~isempty(genericCode) && ~cdef.isGeneric 0111 warning('elk:crystal:notGeneric', ['Non-generic crystal definition ' ... 0112 'called with generic code. To obtain speedup in loading cystal ' ... 0113 'data, call obtainCrystal(..) or obtainCrystal(.., []) for ' ... 0114 'non-generic cases.']); 0115 % enforce proper file name 0116 fileName = [crystalName '.mat']; 0117 cdef.genericCode = []; 0118 else 0119 cdef.genericCode = genericCode; 0120 end 0121 0122 %% save data 0123 save([fileparts(which('obtainCrystal')) filesep 'data' filesep fileName], ... 0124 'cdef') 0125 obtainData('init', 'database', ['.' filesep 'data'], ... 0126 'type', 'mat', 'prompt', -1); 0127 0128 end