|
| | Map (i, j) to vertex number vertex_count=0 |
| |
| | x (i) |
| |
| | y (j)] |
| |
| | vertex_map (i, j) |
| |
| end end Generate | cells (quadrilaterals) cells |
| |
| id | cells () |
| |
| end Write vertices section | fprintf (fid, 'Vertices\n') |
| |
| | fprintf (fid, '%8d\n', size(vertices, 1)) |
| |
| end Write cells section | fprintf (fid, 'cells\n') |
| |
| | fprintf (fid, '%8d\n', size(cells, 1)) |
| |
| end Write centers section | fprintf (fid, 'centers\n') |
| |
| end | fclose (fid) |
| |
| end Plot vertices as red dots | plot (vertices(:, 1), vertices(:, 2), 'ro', 'MarkerSize', 4, 'MarkerFaceColor', 'red') |
| |
| Plot cell centers as blue dots | plot (cell_centers(:, 1), cell_centers(:, 2), 'bo', 'MarkerSize', 3, 'MarkerFaceColor', 'blue') |
| |
| | xlabel ('X') |
| |
| | ylabel ('Y') |
| |
| | title (sprintf('Cartesian Mesh %dx%d on Domain[%.2f,%.2f] x[%.2f,%.2f]',... N, M, xmin, xmax, ymin, ymax)) |
| |
| | legend ('Mesh lines', 'Vertices', 'Cell centers', 'Location', 'best') |
| |
| | xlim ([xmin - margin_x, xmax+margin_x]) |
| |
| | ylim ([ymin - margin_y, ymax+margin_y]) |
| |
| | fprintf ('Mesh successfully generated and saved as %s\n', filename) |
| |
| | fprintf ('Number of vertices:%d\n', size(vertices, 1)) |
| |
| | fprintf ('Number of cells:%d\n', size(cells, 1)) |
| |
| end Example Unit mesh | generate_cartesian_mesh ([0, 2, 0, 1], 10, 5) |
| |
| Rectangle[0, 2] mesh | generate_cartesian_mesh ([-1, 1, -1, 1], 4, 4) |
| |
|
| function generate_cartesian_mesh(domain, N, M) % GENERATE_CARTESIAN_MESH - Generate cartesian mesh for rectangular domain % % INPUTS | xmax = domain(2) |
| |
| | ymin = domain(3) |
| |
| | ymax = domain(4) |
| |
| Create grid points | x = linspace(xmin, xmax, N+1) |
| |
| | y = linspace(ymin, ymax, M+1) |
| |
| Generate vertices | vertices = [] |
| |
| | vertex_map = zeros(N+1, M+1) |
| |
| for | j |
| |
| | cell_centers = [] |
| |
| | v1 |
| |
| | v2 |
| |
| | v3 |
| |
| | v4 |
| |
| Cell center | center_x = (x(i) + x(i+1)) / 2 |
| |
| | center_y = (y(j) + y(j+1)) / 2 |
| |
| end end Create filename | filename = sprintf('cart%dx%d.typ2', N, M) |
| |
| Write to typ2 file | fid = fopen(filename, 'w') |
| |
| for | i |
| |
| Plot the mesh | figure |
| |
| hold | on |
| |
| Set axis properties axis | equal |
| |
| Set axis limits with small margin | margin_x = (xmax - xmin) * 0.05 |
| |
| | margin_y = (ymax - ymin) * 0.05 |
| |
| hold | off |
| |
| end Example | usage |
| |
| end Example Unit | square |
| |