Creating a mesh with cells size depending on array values

I’m simulating a plasma formation in magnetic field. I think it is convenient to create a mesh with cell size depending on values of magnetic field (e.g. proportional to inverse gradient of magnetic field). Magnetic field is evaluated in a broader region - (-6000, 6000) x (-1500, 1500) in Z x R directions for the field, than the geometry of volume contained plasma - (-5494, 5494) x (-1000, 1000), so I am trying to use a callback function GDMT_SizeCallback to spatially match the array to cells giving them proper size. However, while using mesh.setSizeCallback produce an error

api_callback_ = api_callback_type_(lambda dim, tag, x, y, z, lc, _ : callback(dim, tag, x, y, z, lc))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: GDMT_SizeCallback() takes 5 positional arguments but 6 were given

I’m not sure if I’m using it correctly, haven’t found description to the setSizeCallback.

Below is the complete code.

import gmsh
import sys

import torch as tc


import matplotlib.pyplot as plt

device = tc.device("cuda" if tc.cuda.is_available() else "cpu")



Rmax = 1500
Zmax = 6000
N = 1#072
Rnum = int(Rmax*N)
Znum = int(2*Zmax*N)

nx = 100
ny = 100






# Reading into array
B_tot = tc.load('B_tot_sym_1(hq).pt', weights_only = True)

B_sym = tc.zeros((int(2*Rnum), Znum))

B_sym[: Rmax, :] = tc.fliplr(B_tot.T).T
B_sym[Rmax :, :] = B_tot

f, a = plt.subplots(1, 1, dpi = 200)
a.imshow(B_sym)


# Setting the callback
def GDMT_SizeCallback(entity_dim, entity_tag, x, y, z):
    
    print('\n\n kkkk \n\n')
    x_min, x_max = -Zmax, Zmax
    y_min, y_max = -Rmax, Rmax
    
    ix = int((x - x_min) / (x_max - x_min) * (nx - 1))
    iy = int((y - y_min) / (y_max - y_min) * (ny - 1))

    # Ensure indices are within bounds
    ix = max(0, min(ix, nx - 1))
    iy = max(0, min(iy, ny - 1))
    print('\n\n kkkk \n\n')
    
    return 1/B_sym[ix, iy]    




gmsh.initialize()





# Initialize gmsh:

MESH_SIZE = 1e-2


# The face geometry
GDMT_edges = tc.tensor([[-5494, -5278, -5001, -4384, -3654, -3250, -1060, -476, 476, 1060, 3250, 3654, 4384, 5001, 5278, 5494,
                          5494, 5278, 5001, 4384, 3654, 3250, 1060, 476, -476, -1060, -3250, -3654, -4384, -5001, -5278, -5494],
                        [189, 90, 90, 348, 348, 600, 600, 1000, 1000, 600, 600, 348, 348, 90, 90, 189,
                          -189, -90, -90, -348, -348, -600, -600, -1000, -1000, -600, -600, -348, -348, -90, -90, -189]]).T.tolist()







GDMT_points = []
for edge in GDMT_edges:
        
    GDMT_points.append(gmsh.model.occ.add_point(edge[0], edge[1], MESH_SIZE))#5*MESH_SIZE if abs(edge[0]) == GDMT_edges[0][0] else MESH_SIZE))
    
    


    
GDMT_walls = [
    gmsh.model.occ.addLine(GDMT_points[i], GDMT_points[i + 1]) for i in range(-1, len(GDMT_points) - 1)
    ]


GDMT_face = gmsh.model.occ.add_curve_loop(GDMT_walls)
GDMT_plane_surface = gmsh.model.occ.add_plane_surface([GDMT_face])



# Create the relevant Gmsh data structures from Gmsh model.
gmsh.model.occ.synchronize()


gmsh.model.addPhysicalGroup(2, [GDMT_plane_surface], 1, "Volume")
gmsh.model.addPhysicalGroup(1, [GDMT_walls[0], GDMT_walls[16]], 2, "End-walls")

# Setting the callback 
# gmsh.model.mesh.setSizeCallback(GDMT_SizeCallback)

gmsh.model.mesh.generate(2)


if 'close' not in sys.argv:
    gmsh.fltk.run()

gmsh.finalize()

I would suggest looking at the gmsh documentation: api/gmsh.py · master · Gmsh / Gmsh · GitLab
Which states

Set a mesh size callback for the current model. The callback function
should take six arguments as input (‘dim’, tag', x’, y', z’ and lc'). The first two integer arguments correspond to the dimension dim’ and tag
tag' of the entity being meshed. The next four double precision arguments correspond to the coordinates x’, y' and z’ around which to prescribe
the mesh size and to the mesh size lc' that would be prescribed if the callback had not been called. The callback function should return a double precision number specifying the desired mesh size; returning lc’ is
equivalent to a no-op.
Types:

Thanks for your suggestion!