1import sys,petsc4py 2petsc4py.init(sys.argv) 3from petsc4py import PETSc 4import numpy as np 5 6OptDB = PETSc.Options() 7 8dim = OptDB.getInt('dim', 2) 9plex = PETSc.DMPlex().createBoxMesh([4]*dim, simplex=True) 10plex.distribute() 11plex.view() 12 13# Create two metric tensor fields corresponding to uniform mesh sizes of 0.1 and 0.2 14metric1 = plex.metricCreateUniform(100.0) 15metric2 = plex.metricCreateUniform(25.0) 16 17# The metrics can be combined using intersection, the result of which corresponds to 18# the maximum ellipsoid at each point 19metric = plex.metricCreate() 20plex.metricIntersection2(metric1, metric2, metric) 21metric1.axpy(-1, metric) 22assert np.isclose(metric1.norm(), 0.0) 23 24# Call adapt routine - boundary label None by default 25newplex = plex.adaptMetric(metric) 26newplex.view() 27 28# Write to VTK file 29viewer = PETSc.Viewer().createVTK('base_mesh.vtk', 'w') 30viewer(plex) 31viewer = PETSc.Viewer().createVTK('isotropic_mesh.vtk', 'w') 32viewer(newplex) 33