function export_case() %EXPORT_CASE 按 case_config.m 写出 bele3d.json(含 bele 块) [phy, cfg] = case_config(); root = fileparts(mfilename('fullpath')); jsonPath = fullfile(root, cfg.jsonFile); sbcIndex = phy.out(:)'; doc = struct(); doc.FemType = cfg.FemType; doc.EletricType = cfg.EletricType; doc.lambda = phy.lda0; doc.NbrBoundary = cfg.NbrBoundary; doc.BoundaryFlag = cfg.BoundaryFlag; doc.sbc = struct( ... 'Index', sbcIndex, ... 'SBCType', zeros(1, nSbc), ... 'E0x', {repmat({'0'}, 1, nSbc)}, ... 'E0y', {repmat({'0'}, 1, nSbc)}, ... 'E0z', {repmat({'0'}, 1, nSbc)}, ... 'kx', zeros(1, nSbc), ... 'ky', zeros(1, nSbc), ... 'kz', zeros(1, nSbc)); doc.bele = struct( ... 'index', phy.beleDomains, ... 'Ebx', 'exp(-2.5*i*pi*(z+0.5))', ... 'Eby', '0', ... 'Ebz', '0', ... 'curlEbx', '0', ... 'curlEby', '-2.5*i*pi*exp(-2.5*i*pi*(z+0.5))', ... 'curlEbz', '0'); nDom = numel(phy.eps); doc.NbrDomain = nDom; doc.domainType = repmat(2, 1, nDom); doc.domainIndex = 0:(nDom - 1); doc.matType = zeros(1, nDom); doc.epsilonrR = phy.eps(:)'; doc.epsilonrI = zeros(1, nDom); doc.murR = phy.mur(:)'; doc.murI = zeros(1, nDom); doc.chiheR = zeros(1, nDom); doc.chiehR = zeros(1, nDom); doc.chiheI = zeros(1, nDom); doc.chiehI = zeros(1, nDom); doc.sigma = zeros(1, nDom); doc.n = ones(1, nDom); doc.k = zeros(1, nDom); doc.MeshFile = cfg.MeshFile; doc.OutFile = cfg.OutFile; txt = write_bele_json(doc); fid = fopen(jsonPath, 'w'); if fid < 0 error('无法写入: %s', jsonPath); end fprintf(fid, '%s', txt); fclose(fid); fprintf('已写出 %s\n', jsonPath); end function txt = write_bele_json(doc) lines = {}; lines{end+1} = '{'; lines{end+1} = sprintf(' "FemType": %d,', doc.FemType); lines{end+1} = sprintf(' "EletricType": %d,', doc.EletricType); lines{end+1} = sprintf(' "lambda": %g,', doc.lambda); lines{end+1} = sprintf(' "NbrBoundary": %d,', doc.NbrBoundary); lines{end+1} = sprintf(' "BoundaryFlag": %s,', fmt_int_row(doc.BoundaryFlag)); lines{end+1} = ' "sbc": {'; lines{end+1} = sprintf(' "Index": %s,', fmt_int_row(doc.sbc.Index)); lines{end+1} = sprintf(' "SBCType": %s,', fmt_int_row(doc.sbc.SBCType)); lines{end+1} = sprintf(' "E0x": %s,', fmt_str_row(doc.sbc.E0x)); lines{end+1} = sprintf(' "E0y": %s,', fmt_str_row(doc.sbc.E0y)); lines{end+1} = sprintf(' "E0z": %s,', fmt_str_row(doc.sbc.E0z)); lines{end+1} = sprintf(' "kx": %s,', fmt_int_row(doc.sbc.kx)); lines{end+1} = sprintf(' "ky": %s,', fmt_int_row(doc.sbc.ky)); lines{end+1} = sprintf(' "kz": %s', fmt_int_row(doc.sbc.kz)); lines{end+1} = ' },'; lines{end+1} = ' "bele": {'; lines{end+1} = sprintf(' "index": %s,', fmt_int_row(doc.bele.index)); lines{end+1} = sprintf(' "Ebx": "%s",', doc.bele.Ebx); lines{end+1} = sprintf(' "Eby": "%s",', doc.bele.Eby); lines{end+1} = sprintf(' "Ebz": "%s",', doc.bele.Ebz); lines{end+1} = sprintf(' "curlEbx": "%s",', doc.bele.curlEbx); lines{end+1} = sprintf(' "curlEby": "%s",', doc.bele.curlEby); lines{end+1} = sprintf(' "curlEbz": "%s"', doc.bele.curlEbz); lines{end+1} = ' },'; lines{end+1} = sprintf(' "NbrDomain": %d,', doc.NbrDomain); lines{end+1} = sprintf(' "domainType": %s,', fmt_int_row(doc.domainType)); lines{end+1} = sprintf(' "domainIndex": %s,', fmt_int_row(doc.domainIndex)); lines{end+1} = sprintf(' "matType": %s,', fmt_int_row(doc.matType)); lines{end+1} = sprintf(' "epsilonrR": %s,', fmt_float_row(doc.epsilonrR)); lines{end+1} = sprintf(' "epsilonrI": %s,', fmt_float_row(doc.epsilonrI)); lines{end+1} = sprintf(' "murR": %s,', fmt_float_row(doc.murR)); lines{end+1} = sprintf(' "murI": %s,', fmt_float_row(doc.murI)); lines{end+1} = sprintf(' "chiheR": %s,', fmt_float_row(doc.chiheR)); lines{end+1} = sprintf(' "chiehR": %s,', fmt_float_row(doc.chiehR)); lines{end+1} = sprintf(' "chiheI": %s,', fmt_float_row(doc.chiheI)); lines{end+1} = sprintf(' "chiehI": %s,', fmt_float_row(doc.chiehI)); lines{end+1} = sprintf(' "sigma": %s,', fmt_float_row(doc.sigma)); lines{end+1} = sprintf(' "n": %s,', fmt_float_row(doc.n)); lines{end+1} = sprintf(' "k": %s,', fmt_float_row(doc.k)); lines{end+1} = sprintf(' "MeshFile": "%s",', doc.MeshFile); lines{end+1} = sprintf(' "OutFile": "%s"', doc.OutFile); lines{end+1} = '}'; txt = strjoin(lines, newline); end function s = fmt_int_row(v) s = ['[', strjoin(arrayfun(@num2str, v(:)', 'UniformOutput', false), ', '), ']']; end function s = fmt_float_row(v) s = ['[', strjoin(arrayfun(@(x) num2str(x, '%.16g'), v(:)', 'UniformOutput', false), ', '), ']']; end function s = fmt_str_row(c) parts = cell(1, numel(c)); for i = 1:numel(c) parts{i} = ['"', char(c{i}), '"']; end s = ['[', strjoin(parts, ', '), ']']; end