(desired_force,
                     min_radius=0.0045,
                     max_radius=0.005,
                     desired_places=6,
                     side='closest',
                     **duplo_kwargs)
return radius, force
Find a stud size that gives the desired separation force.
Find a stud size that gives the desired separation force.
Find a stud size that gives the desired separation force .
def tune_stud_radius(desired_force,
                     min_radius=0.0045,
                     max_radius=0.005,
                     desired_places=6,
                     side='closest',
                     **duplo_kwargs):
  """Find a stud size that gives the desired separation force."""

  @_KeepBracketingSolutions
  def func(radius):
    radius = round(radius, desired_places)  # Round radius for aesthetics (!)
    return (get_separation_force_for_radius(radius=radius, **duplo_kwargs)
            - desired_force)

  # Ensure that the min and max radii bracket the solution.
  while func(min_radius) > 0:
    min_radius = max(1e-3, min_radius - (max_radius - min_radius))
  while func(max_radius) < 0:
    max_radius += (max_radius - min_radius)

  tolerance = 10**-(desired_places)

  # Use bisection to refine the bounds on the optimal radius. Note: this assumes
  # that separation force is monotonic w.r.t. stud radius, but this isn't
  # exactly true in all cases.
  optimize.bisect(func, a=min_radius, b=max_radius, xtol=tolerance, disp=True)

  if side == 'below':
    solution = func.below
  elif side == 'above':
    solution = func.above
  else:
    solution = func.closest

  radius = round(solution.x, desired_places)
  force = get_separation_force_for_radius(radius, **duplo_kwargs)

  return radius, force
def tune_stud_radius ( desired_force , min_radius = 0.0045 , max_radius = 0.005 , desired_places = 6 , side = 'closest' , * * duplo_kwargs ) : @ _KeepBracketingSolutions def func ( radius ) : radius = round ( radius , desired_places ) # Round radius for aesthetics (!) return ( get_separation_force_for_radius ( radius = radius , * * duplo_kwargs ) - desired_force ) # Ensure that the min and max radii bracket the solution. while func ( min_radius ) > 0 : min_radius = max ( 1e-3 , min_radius - ( max_radius - min_radius ) ) while func ( max_radius ) < 0 : max_radius += ( max_radius - min_radius ) tolerance = 10 ** - ( desired_places ) # Use bisection to refine the bounds on the optimal radius. Note: this assumes # that separation force is monotonic w.r.t. stud radius, but this isn't # exactly true in all cases. optimize . bisect ( func , a = min_radius , b = max_radius , xtol = tolerance , disp = True ) if side == 'below' : solution = func . below elif side == 'above' : solution = func . above else : solution = func . closest radius = round ( solution . x , desired_places ) force = get_separation_force_for_radius ( radius , * * duplo_kwargs ) return radius , force