problem with numba calling with dtype fit

4 days ago 13
ARTICLE AD BOX

I want to use numba to accelerate a physical simulation of water sphere layer, using the code for simulating: from numba import njit import numpy as np from consts import *

"""calculation parameters""" time_interval = 10.0 # Total time for simulation(s) num_steps = int(time_interval / cup_dt) water_layer_num = 500 playback_speed = 1 # Enable Numba parallel processing USE_PARALLEL = True # Pre-calculate frequently used values water_radius = 5.0 # radius of water sphere (cm) Ix, Iy, Iz = 4.0, 2.0, 2.0 # Ensure these are floats inertia_tensor = np.array([Ix, Iy, Iz], dtype=np.float64) layer_thickness = water_radius / water_layer_num radius_ratio = water_radius / water_layer_num water_angular_velocity_history = np.zeros((num_steps, water_layer_num, 3),dtype=np.float64) solid_angular_velocity_history = np.zeros((num_steps, 3), dtype=np.float64) """physical data""" Ix, Iy, Iz = 4, 2, 2 # initialMoments of inertia of main axis v0 = np.array([1.0, 2.0, 3.0],dtype=np.float64) # Initial angular velocity around main axis water_angular_velocity_initial = np.zeros((water_layer_num, 3)) # initial angular velocity of water at radius=0 water_radius = 5.0 # radius of water sphere (cm) real_save_frame_frequency_per_second = max(1,int(save_frame_frequency_per_second/playback_speed)) total_saved_frames = int(time_interval * save_frame_frequency_per_second) save_frame_frequency_per_frame = max(1, int(num_steps/total_saved_frames)) # Pre-allocate storage for animation liquid_angular_velocity_frame_showing_history = [] solid_angular_velocity_frame_showing_history = [] colour_magnitude_showing_history = [] # Initial conditions water_angular_velocity_current = water_angular_velocity_initial.copy() water_angular_velocity_next = water_angular_velocity_initial.copy() solid_angular_velocity_current = v0.copy() solid_angular_velocity_next = v0.copy() # Store initial state water_angular_velocity_history[0] = water_angular_velocity_current solid_angular_velocity_history[0] = solid_angular_velocity_current from utils import solid_liquid_layer_angular_velocity_change liquid_upper_layer_acceleration_term, solid_liquid_drag_acceleration_term = solid_liquid_layer_angular_velocity_change( viscosity=float(water_viscosity), radius = water_radius, thickness=layer_thickness, solid_inertia= inertia_tensor, solid_angular_velocity= solid_angular_velocity_current, liquid_layer_angular_velocity= water_angular_velocity_current[water_layer_num-1], second_liquid_layer_angular_velocity= water_angular_velocity_current[water_layer_num-2], kinetic_viscosity=float(water_kinetic_viscosity) )`

then it goes an error of dtype, which is:

Traceback (most recent call last): File "/Users/niuyingkai/PycharmProjects/PT-formula/test.py", line 54, in <module> liquid_upper_layer_acceleration_term, solid_liquid_drag_acceleration_term = solid_liquid_layer_angular_velocity_change( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ viscosity=float(water_viscosity), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<6 lines>... kinetic_viscosity=float(water_kinetic_viscosity) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 443, in _compile_for_args raise e File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 376, in _compile_for_args return_val = self.compile(tuple(argtypes)) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 908, in compile cres = self._compiler.compile(args, return_type) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 80, in compile status, retval = self._compile_cached(args, return_type) ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 94, in _compile_cached retval = self._compile_core(args, return_type) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 107, in _compile_core cres = compiler.compile_extra(self.targetdescr.typing_context, self.targetdescr.target_context, ...<2 lines>... flags=flags, locals=self.locals, pipeline_class=self.pipeline_class) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 739, in compile_extra return pipeline.compile_extra(func) ~~~~~~~~~~~~~~~~~~~~~~^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 439, in compile_extra return self._compile_bytecode() ~~~~~~~~~~~~~~~~~~~~~~^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 505, in _compile_bytecode return self._compile_core() ~~~~~~~~~~~~~~~~~~^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 481, in _compile_core raise e File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 473, in _compile_core pm.run(self.state) ~~~~~~^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_machinery.py", line 363, in run raise e File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_machinery.py", line 356, in run self._runPass(idx, pass_inst, state) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_lock.py", line 35, in _acquire_compile_lock return func(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_machinery.py", line 311, in _runPass mutated |= check(pss.run_pass, internal_state) ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_machinery.py", line 272, in check mangled = func(compiler_state) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typed_passes.py", line 114, in run_pass typemap, return_type, calltypes, errs = type_inference_stage( ~~~~~~~~~~~~~~~~~~~~^ state.typingctx, ^^^^^^^^^^^^^^^^ ...<4 lines>... state.locals, ^^^^^^^^^^^^^ raise_errors=self._raise_errors) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typed_passes.py", line 95, in type_inference_stage errs = infer.propagate(raise_errors=raise_errors) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 1075, in propagate errors = self.constraints.propagate(self) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 160, in propagate constraint(typeinfer) ~~~~~~~~~~^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 572, in __call__ self.resolve(typeinfer, typevars, fnty) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 595, in resolve sig = typeinfer.resolve_call(fnty, pos_args, kw_args) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 1569, in resolve_call return self.context.resolve_function_type(fnty, pos_args, kw_args) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typing/context.py", line 279, in resolve_function_type res = self._resolve_user_function_type(func, args, kws) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typing/context.py", line 335, in _resolve_user_function_type return func.get_call_type(self, args, kws) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/types/functions.py", line 541, in get_call_type self.dispatcher.get_call_template(args, kws) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 319, in get_call_template self.compile(tuple(args)) ~~~~~~~~~~~~^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 908, in compile cres = self._compiler.compile(args, return_type) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 80, in compile status, retval = self._compile_cached(args, return_type) ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 94, in _compile_cached retval = self._compile_core(args, return_type) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 107, in _compile_core cres = compiler.compile_extra(self.targetdescr.typing_context, self.targetdescr.target_context, ...<2 lines>... flags=flags, locals=self.locals, pipeline_class=self.pipeline_class) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 739, in compile_extra return pipeline.compile_extra(func) ~~~~~~~~~~~~~~~~~~~~~~^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 439, in compile_extra return self._compile_bytecode() ~~~~~~~~~~~~~~~~~~~~~~^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 505, in _compile_bytecode return self._compile_core() ~~~~~~~~~~~~~~~~~~^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 481, in _compile_core raise e File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler.py", line 473, in _compile_core pm.run(self.state) ~~~~~~^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_machinery.py", line 363, in run raise e File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_machinery.py", line 356, in run self._runPass(idx, pass_inst, state) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_lock.py", line 35, in _acquire_compile_lock return func(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_machinery.py", line 311, in _runPass mutated |= check(pss.run_pass, internal_state) ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/compiler_machinery.py", line 272, in check mangled = func(compiler_state) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typed_passes.py", line 114, in run_pass typemap, return_type, calltypes, errs = type_inference_stage( ~~~~~~~~~~~~~~~~~~~~^ state.typingctx, ^^^^^^^^^^^^^^^^ ...<4 lines>... state.locals, ^^^^^^^^^^^^^ raise_errors=self._raise_errors) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typed_passes.py", line 95, in type_inference_stage errs = infer.propagate(raise_errors=raise_errors) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 1075, in propagate errors = self.constraints.propagate(self) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 160, in propagate constraint(typeinfer) ~~~~~~~~~~^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 572, in __call__ self.resolve(typeinfer, typevars, fnty) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 595, in resolve sig = typeinfer.resolve_call(fnty, pos_args, kw_args) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typeinfer.py", line 1569, in resolve_call return self.context.resolve_function_type(fnty, pos_args, kw_args) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typing/context.py", line 279, in resolve_function_type res = self._resolve_user_function_type(func, args, kws) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typing/context.py", line 335, in _resolve_user_function_type return func.get_call_type(self, args, kws) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/types/functions.py", line 314, in get_call_type raise e File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/types/functions.py", line 311, in get_call_type sig = temp.apply(nolitargs, nolitkws) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typing/templates.py", line 358, in apply sig = generic(args, kws) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typing/templates.py", line 621, in generic disp, new_args = self._get_impl(args, kws) ~~~~~~~~~~~~~~^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typing/templates.py", line 720, in _get_impl impl, args = self._build_impl(cache_key, args, kws) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/typing/templates.py", line 793, in _build_impl ovf_result = self._overload_func(*args, **kws) File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/np/arraymath.py", line 4321, in np_asarray if is_nonelike(dtype) or a.dtype == dtype.dtype: ^^^^^^^^^^^ AttributeError: 'Function' object has no attribute 'dtype' Process finished with exit code 1

I checked the code that it runs without numba, and it actually works what is the problem? How can I solve it?

Read Entire Article