# Path: blond/toolbox/next_regular.py
# def next_regular(target):
#     """
#     Find the next regular number greater than or equal to target.
#     Regular numbers are composites of the prime factors 2, 3, and 5.
#     Also known as 5-smooth numbers or Hamming numbers, these are the optimal
#     size for inputs to FFTPACK.
# 
#     Target must be a positive integer.
#     """
#     if target <= 6:
#         return target
# 
#     # Quickly check if it's already a power of 2
#     if not (target & (target-1)):
#         return target
#     
#     target = -(-target // 2)
#     
#     match = float('inf') # Anything found will be smaller
#     p5 = 1
#     while p5 < target:
#         p35 = p5
#         while p35 < target:
#             # Ceiling integer division, avoiding conversion to float
#             # (quotient = ceil(target / p35))
#             quotient = -(-target // p35)
# 
#             # Quickly find next power of 2 >= quotient
#             try:
#                 p2 = 2**((quotient - 1).bit_length())
#             except AttributeError:
#                 # Fallback for Python <2.7
#                 p2 = 2**(len(bin(quotient - 1)) - 2)
# 
#             N = p2 * p35
#             if N == target:
#                 return N * 2
#             elif N < match:
#                 match = N
#             p35 *= 3
#             if p35 == target:
#                 return p35 * 2
#         if p35 < match:
#             match = p35
#         p5 *= 5
#         if p5 == target:
#             return p5 * 2
#     if p5 < match:
#         match = p5
#     
#     return match * 2
# 
# Path: blond/utils/bmath.py
# def use_mpi():
# def mpiMode():
# def use_fftw():
# def use_precision(_precision='double'):
# def update_active_dict(new_dict):


from builtins import range, object
from ctypes import c_uint, c_double, c_void_p
from scipy.constants import e
from ..toolbox.next_regular import next_regular
from ..utils import bmath as bm
import numpy as np

        # Impedance sources list (e.g. list of Resonator objects)
        self.impedance_source_list = impedance_source_list

        # Total impedance array of all sources in* :math:`\Omega`
        self.total_impedance = 0

        # Lenght in s of the front wake (if any) for multi-turn wake mode.
        # If the impedance calculation is performed in frequency domain, an
        # artificial front wake may appear. With this option, it is possible to
        # set to zero a portion at the end of the induced voltage array.*
        self.front_wake_length = front_wake_length

        # Call the __init__ method of the parent class
        _InducedVoltage.__init__(self, Beam, Profile, wake_length=None,
                                 frequency_resolution=frequency_resolution,
                                 multi_turn_wake=multi_turn_wake, RFParams=RFParams,
                                 mtw_mode=mtw_mode, use_regular_fft=use_regular_fft)

    def process(self):
        """
        Reprocess the impedance contributions. To be run when profile change
        """

        _InducedVoltage.process(self)

        # Number of points for the FFT. The next regular number is used for
        # speed, therefore the frequency resolution is always equal or finer
        # than the input value
        if self.use_regular_fft:

# Path: blond/toolbox/next_regular.py
# def next_regular(target):
#     """
#     Find the next regular number greater than or equal to target.
#     Regular numbers are composites of the prime factors 2, 3, and 5.
#     Also known as 5-smooth numbers or Hamming numbers, these are the optimal
#     size for inputs to FFTPACK.
# 
#     Target must be a positive integer.
#     """
#     if target <= 6:
#         return target
# 
#     # Quickly check if it's already a power of 2
#     if not (target & (target-1)):
#         return target
#     
#     target = -(-target // 2)
#     
#     match = float('inf') # Anything found will be smaller
#     p5 = 1
#     while p5 < target:
#         p35 = p5
#         while p35 < target:
#             # Ceiling integer division, avoiding conversion to float
#             # (quotient = ceil(target / p35))
#             quotient = -(-target // p35)
# 
#             # Quickly find next power of 2 >= quotient
#             try:
#                 p2 = 2**((quotient - 1).bit_length())
#             except AttributeError:
#                 # Fallback for Python <2.7
#                 p2 = 2**(len(bin(quotient - 1)) - 2)
# 
#             N = p2 * p35
#             if N == target:
#                 return N * 2
#             elif N < match:
#                 match = N
#             p35 *= 3
#             if p35 == target:
#                 return p35 * 2
#         if p35 < match:
#             match = p35
#         p5 *= 5
#         if p5 == target:
#             return p5 * 2
#     if p5 < match:
#         match = p5
#     
#     return match * 2
# 
# Path: blond/utils/bmath.py
# def use_mpi():
# def mpiMode():
# def use_fftw():
# def use_precision(_precision='double'):
# def update_active_dict(new_dict):


# Path: blond/impedances/impedance.py
from builtins import range, object
from ctypes import c_uint, c_double, c_void_p
from scipy.constants import e
from ..toolbox.next_regular import next_regular
from ..utils import bmath as bm
import numpy as np


        # Impedance sources list (e.g. list of Resonator objects)
        self.impedance_source_list = impedance_source_list

        # Total impedance array of all sources in* :math:`\Omega`
        self.total_impedance = 0

        # Lenght in s of the front wake (if any) for multi-turn wake mode.
        # If the impedance calculation is performed in frequency domain, an
        # artificial front wake may appear. With this option, it is possible to
        # set to zero a portion at the end of the induced voltage array.*
        self.front_wake_length = front_wake_length

        # Call the __init__ method of the parent class
        _InducedVoltage.__init__(self, Beam, Profile, wake_length=None,
                                 frequency_resolution=frequency_resolution,
                                 multi_turn_wake=multi_turn_wake, RFParams=RFParams,
                                 mtw_mode=mtw_mode, use_regular_fft=use_regular_fft)

    def process(self):
        """
        Reprocess the impedance contributions. To be run when profile change
        """

        _InducedVoltage.process(self)

        # Number of points for the FFT. The next regular number is used for
        # speed, therefore the frequency resolution is always equal or finer
        # than the input value
        if self.use_regular_fft:

            self.n_fft = next_regular(self.n_induced_voltage)