ubermag / discretisedfield

Python package for the analysis and visualisation of finite-difference fields.
http://ubermag.github.io
BSD 3-Clause "New" or "Revised" License
19 stars 13 forks source link

Exception on discretisation cell size as float #78

Closed fgr1986 closed 3 years ago

fgr1986 commented 3 years ago

Hi, the following code

import numpy as np
import oommfc as oc
import discretisedfield as df
import micromagneticmodel as mm

# Define a macrospin mesh (i.e. one discretisation cell).
p1 = (0, 0, 0)  # first point of the mesh domain (m)
p2 = (10e-9, 10e-9, 1e-9)  # second point of the mesh domain (m)
n = (1e-9, 1e-9, 1e-9)  # discretisation cell size (m)

region = df.Region(p1=p1, p2=p2)
mesh = df.Mesh(region=region, n=n)

produces the following exception. I believe, from the documentation tutorials that the discretisation cell size should be specified in meters.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-7dcd50ae0a13> in <module>
     19 
     20 region = df.Region(p1=p1, p2=p2)
---> 21 mesh = df.Mesh(region=region, n=n)
     22 mesh.k3d()

~/anaconda3/envs/esmram/lib/python3.8/site-packages/discretisedfield/mesh.py in __init__(self, region, p1, p2, n, cell, bc, subregions)
    184             self.n = dfu.array2tuple(n)
    185         elif n is not None and cell is None:
--> 186             self.n = tuple(n)
    187             cell = np.divide(self.region.edges, self.n).astype(float)
    188             self.cell = dfu.array2tuple(cell)

~/anaconda3/envs/esmram/lib/python3.8/site-packages/ubermagutil/typesystem/descriptors.py in __set__(self, instance, value)
    405             if not all(isinstance(i, self.component_type) for i in value):
    406                 msg = f'Allowed only type(value[i]) == {self.component_type}.'
--> 407                 raise TypeError(msg)
    408         if hasattr(self, 'unsigned'):
    409             if self.unsigned and not all(i >= 0 for i in value):

TypeError: Allowed only type(value[i]) == <class 'int'>.
marijanbeg commented 3 years ago

Hi @fgr1986, thank you for your question. n is the number of discretisation cells in mesh and it must be an integer. It should be:

import numpy as np
import oommfc as oc
import discretisedfield as df
import micromagneticmodel as mm

# Define a macrospin mesh (i.e. one discretisation cell).
p1 = (0, 0, 0)  # first point of the mesh domain (m)
p2 = (10e-9, 10e-9, 1e-9)  # second point of the mesh domain (m)
cell = (1e-9, 1e-9, 1e-9)  # discretisation cell size (m)

region = df.Region(p1=p1, p2=p2)
mesh = df.Mesh(region=region, cell=cell)

or

import numpy as np
import oommfc as oc
import discretisedfield as df
import micromagneticmodel as mm

# Define a macrospin mesh (i.e. one discretisation cell).
p1 = (0, 0, 0)  # first point of the mesh domain (m)
p2 = (10e-9, 10e-9, 1e-9)  # second point of the mesh domain (m)
n = (10, 10, 1)  # discretisation cell size (m)

region = df.Region(p1=p1, p2=p2)
mesh = df.Mesh(region=region, n=n)

You can have a look at this tutorial: https://github.com/ubermag/discretisedfield/blob/master/docs/ipynb/mesh-basics.ipynb

fgr1986 commented 3 years ago

Thanks, that was a bad copy paste :P