Skip to content

Conversation

jtran
Copy link
Contributor

@jtran jtran commented Oct 3, 2025

This implements basic sketch block execution and fixed constraints, calling the ezpz solver for the first time.

@settings(experimentalFeatures = allow)

myParam = 4

s = sketch() {
  x = var 0
  y = var 10
  x == 1 + 2
  y == max([5 * myParam, 0])
}

assert(s.x, isEqualTo = 3)
assert(s.y, isEqualTo = 20)

Implementation

  1. Execute the block, like any other KCL code block. Disallow sketch blocks within sketch blocks.
    1. When you execute a sketch var, generate a fresh ID with its initial value and push that into a Vec.
    2. When you execute a constraint, like e1 == e2, build a Constraint and push that into a Vec.
  2. We now have a list of all sketch vars with their initial guesses and a list of all constraints.
  3. Translate the above to solver inputs, normalizing units, and run the solver. We use the units of module's default length unit. This will likely need to be updated when we add support for angles.
  4. Collect variables defined in the block. Since we push a new stack env to execute the sketch block, similar to how we do when we call a function and need to execute the function's body, we can find all the variables defined in the block.
  5. Traverse each variable's value, recursively substituting the solved value for any sketch var.
  6. Use the collected variables as the keys and values of a KCL Object/Record for the return value of the sketch block.

I considered converting units "back" based on the units of the initial values of vars. I don't think it makes sense because in general, sketch vars can be passed around like any other values and it may not be intuitive where a sketch var originated. In the future, I could imagine having a parameter or sketch block annotation that configures which units to solve in. But it seems rare to care about it.

Future Work

  1. Disallow engine calls in sketch blocks
  2. Make solver warnings prettier
  3. Change solver config? Using Default now.
  4. Send the sketch to the engine
  5. Implement more constraints
  6. Expose all this to the frontend

@jtran jtran requested review from a team as code owners October 3, 2025 19:12
Copy link

vercel bot commented Oct 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
modeling-app Ready Ready Preview Comment Oct 4, 2025 2:30pm

Copy link

codspeed-hq bot commented Oct 3, 2025

CodSpeed Instrumentation Performance Report

Merging #8499 will improve performances by 11.42%

Comparing jtran/sketch-block-exec (03058c7) with main (00e2719)1

Summary

⚡ 1 improvement
✅ 138 untouched

Benchmarks breakdown

Benchmark BASE HEAD Change
mock_execute_mike_stress_test_program 341.2 ms 306.2 ms +11.42%

Footnotes

  1. No successful run was found on main (5ee4569) during the generation of this report, so 00e2719 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@jtran jtran changed the title Sketch 2.0: KCL sketch block execution Sketch 2.0: KCL sketch block execution using ezpz solve Oct 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant