Coverage for klayout_pex/rcx25/extraction_results.py: 75%

163 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-03-31 19:36 +0000

1# 

2# -------------------------------------------------------------------------------- 

3# SPDX-FileCopyrightText: 2024 Martin Jan Köhler and Harald Pretl 

4# Johannes Kepler University, Institute for Integrated Circuits. 

5# 

6# This file is part of KPEX  

7# (see https://github.com/martinjankoehler/klayout-pex). 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21# SPDX-License-Identifier: GPL-3.0-or-later 

22# -------------------------------------------------------------------------------- 

23# 

24from __future__ import annotations 

25from collections import defaultdict 

26from dataclasses import dataclass, field 

27from typing import * 

28 

29from .r.resistor_network import MultiLayerResistanceNetwork, ViaJunction, DeviceTerminal 

30from .types import NetName, LayerName, CellName 

31import klayout_pex_protobuf.process_parasitics_pb2 as process_parasitics_pb2 

32from ..log import error 

33 

34 

35@dataclass 

36class NodeRegion: 

37 layer_name: LayerName 

38 net_name: NetName 

39 cap_to_gnd: float 

40 perimeter: float 

41 area: float 

42 

43 

44@dataclass(frozen=True) 

45class SidewallKey: 

46 layer: LayerName 

47 net1: NetName 

48 net2: NetName 

49 

50 

51@dataclass 

52class SidewallCap: # see Magic EdgeCap, extractInt.c L444 

53 key: SidewallKey 

54 cap_value: float # femto farad 

55 distance: float # distance in µm 

56 length: float # length in µm 

57 tech_spec: process_parasitics_pb2.CapacitanceInfo.SidewallCapacitance 

58 

59 

60@dataclass(frozen=True) 

61class OverlapKey: 

62 layer_top: LayerName 

63 net_top: NetName 

64 layer_bot: LayerName 

65 net_bot: NetName 

66 

67 

68@dataclass 

69class OverlapCap: 

70 key: OverlapKey 

71 cap_value: float # femto farad 

72 shielded_area: float # in µm^2 

73 unshielded_area: float # in µm^2 

74 tech_spec: process_parasitics_pb2.CapacitanceInfo.OverlapCapacitance 

75 

76 

77@dataclass(frozen=True) 

78class SideOverlapKey: 

79 layer_inside: LayerName 

80 net_inside: NetName 

81 layer_outside: LayerName 

82 net_outside: NetName 

83 

84 def __repr__(self) -> str: 

85 return f"{self.layer_inside}({self.net_inside})-"\ 

86 f"{self.layer_outside}({self.net_outside})" 

87 

88 def __post_init__(self): 

89 if self.layer_inside is None: 

90 raise ValueError("layer_inside cannot be None") 

91 if self.net_inside is None: 

92 raise ValueError("net_inside cannot be None") 

93 if self.layer_outside is None: 

94 raise ValueError("layer_outside cannot be None") 

95 if self.net_outside is None: 

96 raise ValueError("net_outside cannot be None") 

97 

98 

99@dataclass 

100class SideOverlapCap: 

101 key: SideOverlapKey 

102 cap_value: float # femto farad 

103 

104 def __str__(self) -> str: 

105 return f"(Side Overlap): {self.key} = {round(self.cap_value, 6)}fF" 

106 

107 

108@dataclass(frozen=True) 

109class NetCoupleKey: 

110 net1: NetName 

111 net2: NetName 

112 

113 def __repr__(self) -> str: 

114 return f"{self.net1}-{self.net2}" 

115 

116 def __post_init__(self): 

117 if self.net1 is None: 

118 raise ValueError("net1 cannot be None") 

119 if self.net2 is None: 

120 raise ValueError("net2 cannot be None") 

121 

122 # NOTE: we norm net names alphabetically 

123 def normed(self) -> NetCoupleKey: 

124 if self.net1 < self.net2: 

125 return self 

126 else: 

127 return NetCoupleKey(self.net2, self.net1) 

128 

129 

130@dataclass 

131class ExtractionSummary: 

132 capacitances: Dict[NetCoupleKey, float] 

133 resistances: Dict[NetCoupleKey, float] 

134 

135 @classmethod 

136 def merged(cls, summaries: List[ExtractionSummary]) -> ExtractionSummary: 

137 merged_capacitances = defaultdict(float) 

138 merged_resistances = defaultdict(float) 

139 for s in summaries: 

140 for couple_key, cap in s.capacitances.items(): 

141 merged_capacitances[couple_key.normed()] += cap 

142 for couple_key, res in s.resistances.items(): 

143 merged_resistances[couple_key.normed()] += res 

144 return ExtractionSummary(capacitances=merged_capacitances, 

145 resistances=merged_resistances) 

146 

147 

148@dataclass 

149class CellExtractionResults: 

150 cell_name: CellName 

151 

152 overlap_table: Dict[OverlapKey, List[OverlapCap]] = field(default_factory=lambda: defaultdict(list)) 

153 sidewall_table: Dict[SidewallKey, List[SidewallCap]] = field(default_factory=lambda: defaultdict(list)) 

154 sideoverlap_table: Dict[SideOverlapKey, List[SideOverlapCap]] = field(default_factory=lambda: defaultdict(list)) 

155 

156 resistor_network: MultiLayerResistanceNetwork = \ 

157 field(default_factory=lambda: MultiLayerResistanceNetwork(resistor_networks_by_layer={}, via_resistors=[])) 

158 

159 def add_overlap_cap(self, cap: OverlapCap): 

160 self.overlap_table[cap.key].append(cap) 

161 

162 def add_sidewall_cap(self, cap: SidewallCap): 

163 self.sidewall_table[cap.key].append(cap) 

164 

165 def add_sideoverlap_cap(self, cap: SideOverlapCap): 

166 self.sideoverlap_table[cap.key].append(cap) 

167 

168 def summarize(self) -> ExtractionSummary: 

169 normalized_overlap_table: Dict[NetCoupleKey, float] = defaultdict(float) 

170 for key, entries in self.overlap_table.items(): 

171 normalized_key = NetCoupleKey(key.net_bot, key.net_top).normed() 

172 normalized_overlap_table[normalized_key] += sum((e.cap_value for e in entries)) 

173 overlap_summary = ExtractionSummary(capacitances=normalized_overlap_table, 

174 resistances={}) 

175 

176 normalized_sidewall_table: Dict[NetCoupleKey, float] = defaultdict(float) 

177 for key, entries in self.sidewall_table.items(): 

178 normalized_key = NetCoupleKey(key.net1, key.net2).normed() 

179 normalized_sidewall_table[normalized_key] += sum((e.cap_value for e in entries)) 

180 sidewall_summary = ExtractionSummary(capacitances=normalized_sidewall_table, 

181 resistances={}) 

182 

183 normalized_sideoverlap_table: Dict[NetCoupleKey, float] = defaultdict(float) 

184 for key, entries in self.sideoverlap_table.items(): 

185 normalized_key = NetCoupleKey(key.net_inside, key.net_outside).normed() 

186 normalized_sideoverlap_table[normalized_key] += sum((e.cap_value for e in entries)) 

187 sideoverlap_summary = ExtractionSummary(capacitances=normalized_sideoverlap_table, 

188 resistances={}) 

189 

190 normalized_resistance_table: Dict[NetCoupleKey, float] = defaultdict(float) 

191 for via_resistor in self.resistor_network.via_resistors: 

192 key1: str = '' 

193 match via_resistor.bottom: 

194 case None: 

195 key1 = '__UNKNOWN__' 

196 case ViaJunction(): 

197 if via_resistor.bottom.network is None: # TODO: happened for ptap cell (VSS)! 

198 error(f"Bottom net is None: {via_resistor}") 

199 continue 

200 key1 = via_resistor.bottom.network.node_names[via_resistor.bottom.node_id] 

201 case DeviceTerminal(): 

202 key1 = via_resistor.bottom.device_terminal.net_name 

203 case _: 

204 raise NotImplementedError("unexpected type") 

205 if via_resistor.top.network is None: 

206 error(f"Top net is None: {via_resistor}") 

207 continue 

208 key2 = via_resistor.top.network.node_names[via_resistor.top.node_id] 

209 normalized_key = NetCoupleKey(key1, key2).normed() 

210 normalized_resistance_table[normalized_key] += via_resistor.resistance 

211 for layer_name, networks in self.resistor_network.resistor_networks_by_layer.items(): 

212 for network in networks.networks: 

213 visited_resistors: Set[Conductance] = set() 

214 for node_id, resistors in network.node_to_s.items(): 

215 node_name = network.node_names[node_id] 

216 for conductance, other_node_id in resistors: 

217 if conductance in visited_resistors: 

218 continue # we don't want to add it twice, only once per direction! 

219 visited_resistors.add(conductance) 

220 

221 other_node_name = network.node_names[other_node_id] 

222 ohm = networks.layer_sheet_resistance / 1000.0 / conductance.cond 

223 normalized_key = NetCoupleKey(node_name, other_node_name).normed() 

224 normalized_resistance_table[normalized_key] += ohm 

225 

226 resistance_summary = ExtractionSummary(capacitances={}, 

227 resistances=normalized_resistance_table) 

228 

229 return ExtractionSummary.merged([ 

230 overlap_summary, sidewall_summary, sideoverlap_summary, 

231 resistance_summary 

232 ]) 

233 

234 

235@dataclass 

236class ExtractionResults: 

237 cell_extraction_results: Dict[CellName, CellExtractionResults] = field(default_factory=dict) 

238 

239 def summarize(self) -> ExtractionSummary: 

240 subsummaries = [s.summarize() for s in self.cell_extraction_results.values()] 

241 return ExtractionSummary.merged(subsummaries)