writeCdefToDatabase

PURPOSE ^

write crystal definition to database folder

SYNOPSIS ^

function writeCdefToDatabase(crystalName, cdef)

DESCRIPTION ^

 write crystal definition to database folder

 Syntax: writeCdefToDatabase(crystalName, cdef)

 See also: editCrystal, vieCrystal, loadCrystal

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function writeCdefToDatabase(crystalName, cdef)
0002 % write crystal definition to database folder
0003 %
0004 % Syntax: writeCdefToDatabase(crystalName, cdef)
0005 %
0006 % See also: editCrystal, vieCrystal, loadCrystal
0007 
0008 % The elk-library: convex geometry applied to crystallization modeling.
0009 %   Copyright (C) 2014 Alexander Reinhold
0010 %
0011 % This program is free software: you can redistribute it and/or modify it
0012 %   under the terms of the GNU General Public License as published by the
0013 %   Free Software Foundation, either version 3 of the License, or (at your
0014 %   option) any later version.
0015 %
0016 % This program is distributed in the hope that it will be useful, but
0017 %   WITHOUT ANY WARRANTY; without even the implied warranty of
0018 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0019 %   General Public License for more details.
0020 %
0021 % You should have received a copy of the GNU General Public License along
0022 %   with this program.  If not, see <http://www.gnu.org/licenses/>.
0023 
0024 %% ensure crystal does not exist
0025 myPath = fileparts(which('writeCdefToDatabase'));
0026 if exist([myPath filesep 'database' filesep crystalName '.m'], 'file')
0027     error('elk:crystal:wrongInput', ['The crystal definition already ' ...
0028         'exists in the database.']);
0029 end
0030 
0031 %% ensure cdef is complete
0032 if ~exist('cdef', 'var') || isempty(cdef)
0033     cdef = struct();
0034 end
0035 
0036 if ~isfield(cdef, 'name')
0037     cdef.name = crystalName;end
0038 if ~isfield(cdef, 'source')
0039     cdef.source = '';end
0040 if ~isfield(cdef, 'isGeneric')
0041     cdef.isGeneric = 0;end
0042 if ~isfield(cdef, 'sym')
0043     cdef.sym.defined = 0;end
0044 if ~isfield(cdef, 'lat')
0045     cdef.lat = [];end
0046 if ~isfield(cdef, 'millerDefined')
0047     cdef.millerDefined = [];end
0048 if ~isfield(cdef, 'millerGeneric')
0049     cdef.millerGeneric = [];end
0050 
0051 %% open file
0052 fileHandle = fopen([myPath filesep 'database' filesep crystalName '.m'], 'w');
0053 if fileHandle == -1
0054     error('elk:crystal:fileOpen', 'Cannot open file for writing');
0055 end
0056 
0057 %% write header
0058 fprintf(fileHandle, ['function cdef = ' crystalName '()\n\n']);
0059 
0060 fprintf(fileHandle, '%%%% common information\n');
0061 fprintf(fileHandle, 'cdef.name = ''%s'';\n', cdef.name);
0062 if ~isstruct(cdef.source)
0063     fprintf(fileHandle, 'cdef.source = ''%s'';\n', cdef.source);
0064 else
0065     fieldList = fieldnames(cdef.source);
0066     for iField = 1:length(fieldList)
0067         fprintf(fileHandle, ...
0068             ['cdef.source.' fieldList{iField} ' = ''%s'';\n'], ...
0069             cdef.source.(fieldList{iField}));
0070     end
0071 end
0072 fprintf(fileHandle, '%% isGeneric is 1 when Miller indices can be choosen from cdef.millerGeneric:\n');
0073 fprintf(fileHandle, 'cdef.isGeneric = %i;\n\n', cdef.isGeneric);
0074 
0075 fprintf(fileHandle, '%%%% symmetry information\n');
0076 sprintf('cdef.sym.defined = %i;\n', cdef.sym.defined);
0077 fprintf(fileHandle, 'cdef.sym.defined = %i;\n', cdef.sym.defined);
0078 if isfield(cdef.sym, 'system')
0079     fprintf(fileHandle, '%% system: %s\n', cdef.sym.system);end
0080 if isfield(cdef.sym, 'Schoenflies')
0081     fprintf(fileHandle, '%% Schoenflies: %s\n', cdef.sym.Schoenflies);end
0082 if isfield(cdef.sym, 'HermanMauguin')
0083     fprintf(fileHandle, '%% Herman-Mauguin: %s\n', cdef.sym.HermanMauguin);end
0084 if isfield(cdef.sym, 'Laue')
0085     fprintf(fileHandle, '%% Laue: %s\n', cdef.sym.Laue);end
0086 
0087 fprintf(fileHandle, '\n');
0088 fprintf(fileHandle, '%%%% lattice information\n');
0089 if ~isempty(cdef.lat)
0090     % enforce degree writing
0091     if strcmpi(cdef.lat.unit_angle, 'radian')
0092         angleFactor = 180/pi;
0093     else
0094         angleFactor = 1;
0095     end
0096     fprintf(fileHandle, 'cdef.lat.unit_angle = ''%s'';\n', 'degree');
0097     fprintf(fileHandle, 'cdef.lat.unit_length = ''%s'';\n', cdef.lat.unit_length);
0098     fprintf(fileHandle, 'cdef.lat.a = %.10g;\n', cdef.lat.a);
0099     fprintf(fileHandle, 'cdef.lat.b = %.10g;\n', cdef.lat.b);
0100     fprintf(fileHandle, 'cdef.lat.c = %.10g;\n', cdef.lat.c);
0101     fprintf(fileHandle, 'cdef.lat.alpha = %.10g;\n', angleFactor*cdef.lat.alpha);
0102     fprintf(fileHandle, 'cdef.lat.beta = %.10g;\n', angleFactor*cdef.lat.beta);
0103     fprintf(fileHandle, 'cdef.lat.gamma = %.10g;\n', angleFactor*cdef.lat.gamma);
0104 else
0105     fprintf(fileHandle, 'cdef.lat.unit_angle = ''%s'';\n', 'degree');
0106     fprintf(fileHandle, 'cdef.lat.unit_length = ''%s'';\n', 'angstroem');
0107     fprintf(fileHandle, 'cdef.lat.a     = %.10g;\n', 1);
0108     fprintf(fileHandle, 'cdef.lat.b     = %.10g;\n', 1);
0109     fprintf(fileHandle, 'cdef.lat.c     = %.10g;\n', 1);
0110     fprintf(fileHandle, 'cdef.lat.alpha = %.10g;\n', 90);
0111     fprintf(fileHandle, 'cdef.lat.beta  = %.10g;\n', 90);
0112     fprintf(fileHandle, 'cdef.lat.gamma = %.10g;\n', 90);
0113 end
0114 
0115 fprintf(fileHandle, '\n');
0116 fprintf(fileHandle, '%%%% Miller indices\n');
0117 
0118 fprintf(fileHandle, 'cdef.millerDefined = [');
0119 for iRow = 1:size(cdef.millerDefined, 1)
0120     % set pre string
0121     if iRow == 1
0122         pre = '';
0123     else
0124         pre = '                      ';
0125     end
0126     % set post
0127     if iRow == size(cdef.millerDefined, 1)
0128         post = '];\n';
0129     else
0130         post = '; ...\n';
0131     end
0132     % write line
0133     fprintf(fileHandle, [pre '%g, %g, %g' post]', ...
0134         cdef.millerDefined(iRow, 1), ...
0135         cdef.millerDefined(iRow, 2), ...
0136         cdef.millerDefined(iRow, 3));
0137 end
0138 if size(cdef.millerDefined, 1) == 0
0139     fprintf(fileHandle, '];\n');end
0140 
0141 if ~isempty(cdef.millerGeneric)
0142 fprintf(fileHandle, 'cdef.millerGeneric = [');
0143 for iRow = 1:size(cdef.millerGeneric, 1)
0144     % set pre string
0145     if iRow == 1
0146         pre = '';
0147     else
0148         pre = '                      ';
0149     end
0150     % set post
0151     if iRow == size(cdef.millerGeneric, 1)
0152         post = '];\n';
0153     else
0154         post = '; ...\n';
0155     end
0156     % write line
0157     fprintf(fileHandle, [pre '%g, %g, %g' post]', ...
0158         cdef.millerGeneric(iRow, 1), ...
0159         cdef.millerGeneric(iRow, 2), ...
0160         cdef.millerGeneric(iRow, 3));
0161 end
0162 end
0163 
0164 %% close file
0165 fclose(fileHandle);
0166 dump = obtainCrystal('init');

Generated on Sat 18-Jul-2015 16:45:31 by m2html © 2005