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 values 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.
  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. Allow forward references?
  7. 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 8, 2025 6:41pm

Copy link

codspeed-hq bot commented Oct 3, 2025

CodSpeed Performance Report

Merging #8499 will not alter performance

Comparing jtran/sketch-block-exec (a98ae26) with main (b1b2d9b)1

Summary

✅ 139 untouched
⏩ 92 skipped2

Footnotes

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

  2. 92 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@jtran jtran force-pushed the jtran/sketch-block-exec branch from 5735f20 to 48f06a2 Compare October 3, 2025 19:50
@jtran jtran force-pushed the jtran/sketch-block-exec branch from 3a74821 to a1ef8df Compare October 4, 2025 03:22
@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