-
Notifications
You must be signed in to change notification settings - Fork 187
/
picmodel.py
185 lines (150 loc) · 6.06 KB
/
picmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from __future__ import annotations
from enum import Enum
from typing import Any
import yaml
from pydantic import AnyUrl, BaseModel, ConfigDict, Field, RootModel
import gdsfactory as gf
SCHEMA_VERSION = 1
class CrossSection(RootModel):
root: str = Field(
..., description="A cross section to use for waveguides or traces."
)
class RouteSettings(BaseModel):
cross_section: CrossSection | None = None
separation: float | None = Field(
5.0, description="The minimum separation between routes in the bundle [um]."
)
model_config = ConfigDict(extra="allow")
class RoutingStrategy(RootModel):
root: str = Field(..., description="The type of routing to use")
class Links(BaseModel):
pass
model_config = ConfigDict(extra="allow")
class PortEnum(Enum):
ne = "ne"
nc = "nc"
nw = "nw"
se = "se"
sc = "sc"
sw = "sw"
ce = "ce"
cw = "cw"
cc = "cc"
center = "center"
class Placement(BaseModel):
model_config = ConfigDict(extra="forbid")
x: str | float | None = Field(
None,
description="The x location at which to place the component.\nThis can either be a number or an other_inst,port definition, meaning it will be placed relative to the port specified on the other instance. \nIf port keyword is defined, this will be relative to the specified port. Otherwise, it will be relative to the cell origin.",
)
y: str | float | None = Field(
None,
description="The y location at which to place the component.\nThis can either be a number or an other_inst,port definition, meaning it will be placed relative to the port specified on the other instance. \nIf port keyword is defined, this will be relative to the specified port. Otherwise, it will be relative to the cell origin.",
)
port: str | PortEnum | None = Field(
None,
description="The port or keyword used to anchor the component. Either specify any port on the instance or one of these special keywords:\nne, nc, nw, se, sc, sw, ce, cw, cc for the northeast, north-center, northwest, etc. coordinates of the cell",
)
rotation: float | None = Field(
0,
description="The rotation of the cell about the origin, or port if defined.",
)
dx: float | None = Field(
None,
description="An additional displacement in the x direction. Useful if x is defined using other_inst,port syntax",
)
dy: float | None = Field(
None,
description="An additional displacement in the y direction. Useful if y is defined using other_inst,port syntax",
)
mirror: bool | None = Field(
None,
description="true/false value indicating whether we should flip horizontally.",
)
class Instance(BaseModel):
model_config = ConfigDict(extra="forbid")
component: str
settings: dict[str, Any] | None = Field(
None, description="Settings for the component"
)
class Route(BaseModel):
model_config = ConfigDict(extra="forbid")
routing_strategy: RoutingStrategy | None = None
settings: RouteSettings | None = None
links: dict[str, str]
class PicYamlConfiguration(BaseModel):
schema_version: str = Field(
default=SCHEMA_VERSION, description="The version of the YAML syntax used."
)
schema: AnyUrl | None = Field(None, alias="$schema")
instances: dict[str, Instance] | None = None
placements: dict[str, Placement] | None = None
routes: dict[str, Route] | None = None
ports: dict[str, str] | None = None
def add_instance(
self, name: str, component: gf.Component, placement: Placement | None = None
) -> None:
component_name = component.function_name
component_settings = component.settings.model_dump()
self.instances[name] = Instance(
component=component_name, settings=component_settings
)
if not placement:
placement = Placement()
self.placements[name] = placement
def move_instance_to(self, name: str, x: float, y: float) -> None:
self.placements[name].x = x
self.placements[name].y = y
def move_instance(self, name: str, dx: float, dy: float) -> None:
if not self.placements[name].dx:
self.placements[name].dx = 0
self.placements[name].dx += dx
if not self.placements[name].dy:
self.placements[name].dy = 0
self.placements[name].dy += dy
def to_yaml(self, filename) -> None:
netlist = self.model_dump()
with open(filename, mode="w") as f:
yaml.dump(netlist, f, default_flow_style=None, sort_keys=False)
class SchematicConfiguration(BaseModel):
schema: AnyUrl | None = Field(None, alias="$schema")
instances: dict[str, Instance] | None = None
schematic_placements: dict[str, Placement] | None = None
nets: list[list[str]] | None = None
ports: dict[str, str] | None = None
schema_version: int = Field(
default=SCHEMA_VERSION, description="The version of the YAML syntax used."
)
@property
def placements(self):
return self.schematic_placements
def add_instance(
self,
name: str,
component: str | gf.Component,
placement: Placement | None = None,
settings: dict[str, Any] | None = None,
) -> None:
if isinstance(component, gf.Component):
component_name = component.function_name
component_settings = component.settings.model_dump()
if settings:
component_settings = component_settings | settings
self.instances[name] = Instance(
component=component_name, settings=component_settings
)
else:
if not settings:
settings = {}
self.instances[name] = Instance(component=component, settings=settings)
if name not in self.placements:
if not placement:
placement = Placement()
self.placements[name] = placement
def create_pic():
return PicYamlConfiguration(
instances={},
placements={},
routes={},
ports={},
)