[Python] python numba calling problems

Discussão em 'Python' iniciado por Stack, Novembro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I was writing code for the simulation of the fluid dynamics of a layer of water. It has a function of solid_liquid_layer_angular_velocity_change:

    def solid_liquid_layer_angular_velocity_change(
    viscosity,
    radius,
    thickness,
    solid_inertia,
    solid_angular_velocity,
    liquid_layer_angular_velocity,
    second_liquid_layer_angular_velocity,
    ):
    """
    Calculate the angular velocity change rates for both solid and liquid layers due to viscous interaction.

    Formula:
    t = 8/3 * mu * r^4 * (omega_solid - omega_liquid) / thickness
    dOmega_solid/dt = -t / I_effective

    Args:
    viscosity: The dynamic viscosity of the liquid
    radius: The radius of the top layer of the fluid
    thickness: The thickness of one liquid layer
    solid_inertia: The principal moments of inertia of the solid object [I1, I2, I3]
    solid_angular_velocity: The current angular velocity of the solid object [ωx, ωy, ωz]
    liquid_layer_angular_velocity: The angular velocity of the top liquid layer
    second_liquid_layer_angular_velocity: The angular velocity of the second liquid layer

    Returns:
    tuple: (solid_angular_velocity_change, liquid_angular_velocity_change)
    """
    # Small value to avoid division by zero
    EPSILON = 1e-10

    # Calculate torque on solid due to liquid with numerical stability
    delta_omega = solid_angular_velocity - liquid_layer_angular_velocity
    torque_magnitude = (8/3) * np.pi * viscosity * (radius**4) / max(thickness, EPSILON)
    torque_solid = torque_magnitude * delta_omega

    # Calculate liquid layer's angular velocity change with numerical stability
    current_radius = max(radius - thickness, EPSILON)
    total_layers = max(radius / max(thickness, EPSILON), 1.0)

    # Initialize with zeros
    liquid_angular_velocity_change = np.zeros_like(liquid_layer_angular_velocity)

    # Only calculate if we have enough layers
    if total_layers > 1:
    liquid_angular_velocity_change = layer_angular_velocity_change(
    kinetic_viscosity=water_kinetic_viscosity,
    radius=current_radius,
    layer=current_radius/thickness,
    total_layers=total_layers,
    upper_angular_velocity=solid_angular_velocity,
    angular_velocity=liquid_layer_angular_velocity,
    lower_angular_velocity=second_liquid_layer_angular_velocity
    )

    # Calculate effective inertia with numerical stability
    effective_inertia = inertia_of_solid_object_at_nonmain_axis_rotation(
    solid_inertia,
    solid_angular_velocity
    )

    # Calculate solid's angular velocity change due to liquid with numerical stability
    solid_angular_velocity_change_due_to_liquid = np.zeros_like(torque_solid)
    if effective_inertia > EPSILON:
    solid_angular_velocity_change_due_to_liquid = -torque_solid / effective_inertia

    # Ensure we don't return NaN or inf
    solid_angular_velocity_change_due_to_liquid = np.nan_to_num(
    solid_angular_velocity_change_due_to_liquid,
    nan=0.0,
    posinf=0.0,
    neginf=0.0
    )

    return solid_angular_velocity_change_due_to_liquid, liquid_angular_velocity_change


    and then I used it in another code for calling:

    upper_layer_viscous_increment_term, solid_angular_velocity_viscous_increment_term = solid_liquid_layer_angular_velocity_change(
    viscosity=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],
    )


    then, I received an error which is caused by numba, which claims to be the parameters calling doesn't match the function was defined causing index out of range, following:

    /usr/local/bin/python3.13 /Users/niuyingkai/PycharmProjects/PT-formula/2026 prob 7 liquid solid interaction.py
    Traceback (most recent call last):
    File "/Users/niuyingkai/PycharmProjects/PT-formula/2026 prob 7 liquid solid interaction.py", line 174, in <module>
    upper_layer_viscous_increment_term, solid_angular_velocity_viscous_increment_term = solid_liquid_layer_angular_velocity_change(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    viscosity=water_viscosity,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<5 lines>...
    second_liquid_layer_angular_velocity=water_angular_velocity_current[water_layer_num-2],
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
    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 315, in get_call_template
    pysig, args = self._compiler.fold_argument_types(args, kws)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
    File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/numba/core/dispatcher.py", line 73, in fold_argument_types
    args = fold_arguments(self.pysig, args, kws,
    normal_handler,
    default_handler,
    stararg_handler)
    File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-fpackages/numba/core/typing/templates.py", line 217, in fold_arguments
    bind_kws[n] = args[len(kwonly) + idx]
    ~~~~^^^^^^^^^^^^^^^^^^^
    IndexError: tuple index out of range

    Process finished with exit code 1


    But I checked the calling and there was no problem at all. What is the problem? How to solve it?

    Continue reading...

Compartilhe esta Página