0001 function gmsh_write_mesh(mdl, data, outfile)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 if ischar(mdl) && strcmp(mdl,'UNIT_TEST'); do_unit_test; return; end
0015
0016 is_img = (isfield(mdl, 'type') && strcmp(mdl.type, 'image')) || isfield(mdl, 'elem_data');
0017 is_imdl = (isfield(mdl, 'type') && strcmp(mdl.type, 'inv_model')) || isfield(mdl, 'fwd_model');
0018
0019 if nargin < 3
0020 outfile = data;
0021 data = [];
0022 end
0023
0024 if is_img && isempty(data)
0025 data = mdl.elem_data(:,1);
0026 end
0027 assert(isempty(data) || size(data,2) == 1, 'error: expect a single frame of data in gmsh_write_mesh');
0028
0029 if is_imdl || is_img
0030 if isfield(mdl, 'rec_model')
0031 mdl = mdl.rec_model;
0032 else
0033 mdl = mdl.fwd_model;
0034 end
0035 end
0036 assert(isempty(data) || size(data,1) == size(mdl.elems,1), 'error: expect a data to match number of elements in gmsh_write_mesh');
0037
0038 nn = size(mdl.nodes,1);
0039 ne = size(mdl.elems,1);
0040 ndim = size(mdl.nodes,2);
0041 assert((ndim >= 2) && (ndim <= 3), 'not 2D or 3D?!');
0042
0043 fid = fopen(outfile, 'w');
0044 fprintf(fid, '$MeshFormat\n');
0045 fprintf(fid, '4.1 0 8\n');
0046 fprintf(fid, '$EndMeshFormat\n');
0047 fprintf(fid, '$Nodes\n');
0048 fprintf(fid, '%d %d %d %d\n', 1, nn, 1, nn);
0049 fprintf(fid, '%d %d %d %d\n', ndim, 0, 0, nn);
0050 fprintf(fid, '%d\n', 1:nn);
0051 nodes = [ mdl.nodes zeros(nn, 3 - ndim) ];
0052 fprintf(fid, '%f %f %f\n', nodes');
0053 fprintf(fid, '$EndNodes\n');
0054 fprintf(fid, '$Elements\n');
0055 if ndim == 2
0056 type = 2;
0057 else
0058 type = 4;
0059 end
0060 fprintf(fid, '%d %d %d %d\n', 1, ne, 1, ne);
0061 fprintf(fid, '%d %d %d %d\n', ndim, 0, type, ne);
0062 elems = [[1:ne]' mdl.elems];
0063 if ndim == 2
0064 fprintf(fid, '%d %d %d %d\n', elems');
0065 else
0066 fprintf(fid, '%d %d %d %d %d\n', elems');
0067 end
0068 fprintf(fid, '$EndElements\n');
0069 if ~isempty(data)
0070 fprintf(fid, '$ElementData\n');
0071 fprintf(fid, '%d\n', 1);
0072 name = 'gmsh_write_mesh output';
0073 if isfield(mdl, 'name')
0074 name = mdl.name;
0075 end
0076 fprintf(fid, '%s\n', name);
0077 fprintf(fid, '%d\n', 1);
0078 fprintf(fid, '%f\n', 0);
0079 fprintf(fid, '%d\n', 3);
0080 fprintf(fid, '%d\n', 0);
0081 fprintf(fid, '%d\n', 1);
0082 fprintf(fid, '%d\n', ne);
0083 data = [[1:ne]' data];
0084 fprintf(fid, '%d %f\n', data');
0085 fprintf(fid, '$EndElementData\n');
0086 end
0087 fclose(fid);
0088
0089 function do_unit_test()
0090
0091
0092 outfile = tempname();
0093 selfdir = fileparts(which('gmsh_read_mesh'));
0094 mdl1 = gmsh_mk_fwd_model(fullfile(selfdir, 'cube-4.1.msh'));
0095 mdl2 = gmsh_mk_fwd_model(fullfile(selfdir, 'box-4.1.msh'));
0096 data1 = rand(size(mdl1.elems,1), 1);
0097 data2 = rand(size(mdl1.elems,1), 1);
0098 data3 = rand(size(mdl2.elems,1), 1);
0099
0100 gmsh_write_mesh(mdl1, data1, outfile);
0101 mdl = gmsh_mk_fwd_model(outfile);
0102 delete(outfile);
0103 unit_test_cmp('fmdl+data (nn)', mdl1.nodes, mdl.nodes);
0104 unit_test_cmp('fmdl+data (el)', mdl1.elems, mdl.elems);
0105
0106
0107 gmsh_write_mesh(mdl1, outfile);
0108 mdl = gmsh_mk_fwd_model(outfile);
0109 delete(outfile);
0110 unit_test_cmp('fmdl (nn)', mdl1.nodes, mdl.nodes);
0111 unit_test_cmp('fmdl (ee)', mdl1.elems, mdl.elems);
0112
0113
0114 img = mk_image(mdl1, data2);
0115 gmsh_write_mesh(img, data1, outfile);
0116 mdl = gmsh_mk_fwd_model(outfile);
0117 delete(outfile);
0118 unit_test_cmp('img+data (nn)', mdl1.nodes, mdl.nodes);
0119 unit_test_cmp('img+data (ee)', mdl1.elems, mdl.elems);
0120
0121
0122 img = mk_image(mdl1, data2);
0123 gmsh_write_mesh(img, outfile);
0124 mdl = gmsh_mk_fwd_model(outfile);
0125 delete(outfile);
0126 unit_test_cmp('img (nn)', mdl1.nodes, mdl.nodes);
0127 unit_test_cmp('img (ee)', mdl1.elems, mdl.elems);
0128
0129
0130 imdl = select_imdl(mdl1, {'Basic GN dif'});
0131
0132 gmsh_write_mesh(imdl, outfile);
0133 mdl = gmsh_mk_fwd_model(outfile);
0134 delete(outfile);
0135 unit_test_cmp('imdl+fmdl (nn)', mdl1.nodes, mdl.nodes);
0136 unit_test_cmp('imdl+fmdl (ee)', mdl1.elems, mdl.elems);
0137
0138
0139 gmsh_write_mesh(imdl, data1, outfile);
0140 mdl = gmsh_mk_fwd_model(outfile);
0141 delete(outfile);
0142 unit_test_cmp('imdl+fmdl+data (nn)', mdl1.nodes, mdl.nodes);
0143 unit_test_cmp('imdl+fmdl+data (ee)', mdl1.elems, mdl.elems);
0144
0145
0146 imdl.rec_model = mdl2;
0147
0148 gmsh_write_mesh(imdl, outfile);
0149 mdl = gmsh_mk_fwd_model(outfile);
0150 delete(outfile);
0151 unit_test_cmp('imdl+rmdl (nn)', mdl2.nodes, mdl.nodes);
0152 unit_test_cmp('imdl+rmdl (ee)', mdl2.elems, mdl.elems);
0153
0154
0155 gmsh_write_mesh(imdl, data3, outfile);
0156 mdl = gmsh_mk_fwd_model(outfile);
0157 delete(outfile);
0158 unit_test_cmp('imdl+rmdl+data (nn)', mdl2.nodes, mdl.nodes);
0159 unit_test_cmp('imdl+rmdl+data (ee)', mdl2.elems, mdl.elems);
0160