From b70ff262a69b0e90223ebcc47eafad7c9d6eb324 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 8 Dec 2025 14:56:49 +0100 Subject: [PATCH] Improve cofactor calculation --- src/kicoil/cli.py | 7 +++---- src/kicoil/geometry.py | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/kicoil/cli.py b/src/kicoil/cli.py index d9dd55f..6027a03 100644 --- a/src/kicoil/cli.py +++ b/src/kicoil/cli.py @@ -11,7 +11,7 @@ import warnings import click from gerbonara.layers import LayerStack -from .geometry import PlanarInductor +from .geometry import PlanarInductor, divisors from .kicad import footprint_to_board from .svg import make_transparent_svg @@ -20,11 +20,10 @@ def print_valid_twists(ctx, param, value): if not value or ctx.resilient_parsing: return - print(f'Valid twist counts for {value} turns:', file=sys.stderr) + click.echo(f'Valid twist counts for {value} turns:', file=sys.stderr) for d in divisors(value, value): - print(f' {d}', file=sys.stderr) + click.echo(f' {d}', file=sys.stderr) - click.echo() ctx.exit() diff --git a/src/kicoil/geometry.py b/src/kicoil/geometry.py index d691dff..874d30e 100644 --- a/src/kicoil/geometry.py +++ b/src/kicoil/geometry.py @@ -56,9 +56,9 @@ def farey_sequence(n: int, descending: bool = False) -> None: def divisors(n, max_b=10): for a, b in farey_sequence(n): - if a == n and b < max_b: + if a != 0 and a == n and b < max_b: yield b - if b == n and a < max_b: + if b != 1 and b == n and a < max_b: yield a @@ -161,7 +161,7 @@ class PlanarInductor(): if gcd(self.twists, self.turns) != 1: raise ValueError(f'For the geometry to work out, the twists parameter must be co-prime to turns, i.e. the two must have 1 as their greatest common divisor. You can print valid values for twists by running the kicoil CLI with --show-twists [turns number].\n\n' f'Right now, both are divisible by {gcd(self.twists, self.turns)}.\n' - f'Valid twist counts for n={self.turns} turns are: {list(divisors(self.turns, max(self.turns, 25)))}' + f'Valid twist counts for n={self.turns} turns are: {list(divisors(self.turns, max(self.turns, 25)))}\n' f'Valid turn counts for k={self.twists} twists are: {list(divisors(self.twists, max(self.twists, 25)))}') if (self.stagger_inner_vias or self.stagger_outer_vias) and self.twists%2 != 0: