def write(filename, mesh, write_binary=True):
if mesh.points.shape[<NUM_LIT:1>] == <NUM_LIT:2>:<EOL><INDENT>logging.warning(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL>mesh.points = numpy.column_stack(<EOL>[mesh.points[:, <NUM_LIT:0>], mesh.points[:, <NUM_LIT:1>], numpy.zeros(mesh.points.shape[<NUM_LIT:0>])]<EOL>)<EOL><DEDENT>if write_binary:<EOL><INDENT>for key, value in mesh.cells.items():<EOL><INDENT>if value.dtype != c_int:<EOL><INDENT>logging.warning(<EOL>"<STR_LIT>",<EOL>value.dtype,<EOL>)<EOL>mesh.cells[key] = numpy.array(value, dtype=c_int)<EOL><DEDENT><DEDENT><DEDENT>cells = mesh.cells.copy()<EOL>if "<STR_LIT>" in cells:<EOL><INDENT>cells["<STR_LIT>"] = cells["<STR_LIT>"][:, [<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>, <NUM_LIT:5>, <NUM_LIT:6>, <NUM_LIT:7>, <NUM_LIT:9>, <NUM_LIT:8>]]<EOL><DEDENT>if "<STR_LIT>" in cells:<EOL><INDENT>cells["<STR_LIT>"] = cells["<STR_LIT>"][<EOL>:, [<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>, <NUM_LIT:5>, <NUM_LIT:6>, <NUM_LIT:7>, <NUM_LIT:8>, <NUM_LIT:11>, <NUM_LIT>, <NUM_LIT:9>, <NUM_LIT:16>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT:10>, <NUM_LIT:12>, <NUM_LIT>, <NUM_LIT:15>]<EOL>]<EOL><DEDENT>with open(filename, "<STR_LIT:wb>") as fh:<EOL><INDENT>mode_idx = <NUM_LIT:1> if write_binary else <NUM_LIT:0><EOL>size_of_double = <NUM_LIT:8><EOL>fh.write(<EOL>("<STR_LIT>".format(mode_idx, size_of_double)).encode(<EOL>"<STR_LIT:utf-8>"<EOL>)<EOL>)<EOL>if write_binary:<EOL><INDENT>fh.write(struct.pack("<STR_LIT:i>", <NUM_LIT:1>))<EOL>fh.write("<STR_LIT:\n>".encode("<STR_LIT:utf-8>"))<EOL><DEDENT>fh.write("<STR_LIT>".encode("<STR_LIT:utf-8>"))<EOL>if mesh.field_data:<EOL><INDENT>_write_physical_names(fh, mesh.field_data)<EOL><DEDENT>tag_data = {}<EOL>other_data = {}<EOL>for cell_type, a in mesh.cell_data.items():<EOL><INDENT>tag_data[cell_type] = {}<EOL>other_data[cell_type] = {}<EOL>for key, data in a.items():<EOL><INDENT>if key in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>tag_data[cell_type][key] = data.astype(c_int)<EOL><DEDENT>else:<EOL><INDENT>other_data[cell_type][key] = data<EOL><DEDENT><DEDENT><DEDENT>_write_nodes(fh, mesh.points, write_binary)<EOL>_write_elements(fh, cells, tag_data, write_binary)<EOL>if mesh.gmsh_periodic is not None:<EOL><INDENT>_write_periodic(fh, mesh.gmsh_periodic)<EOL><DEDENT>for name, dat in mesh.point_data.items():<EOL><INDENT>_write_data(fh, "<STR_LIT>", name, dat, write_binary)<EOL><DEDENT>cell_data_raw = raw_from_cell_data(other_data)<EOL>for name, dat in cell_data_raw.items():<EOL><INDENT>_write_data(fh, "<STR_LIT>", name, dat, write_binary)<EOL><DEDENT><DEDENT>return<EOL>
Writes msh files, cf.
    <http://gmsh.info//doc/texinfo/gmsh.html#MSH-ASCII-file-format>.