Coverage for klayout_pex/klayout/netlist_expander.py: 97%

70 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-17 17:24 +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 

25 

26import re 

27from typing import * 

28 

29import klayout.db as kdb 

30 

31from ..log import ( 

32 info, 

33 warning, 

34) 

35from ..common.capacitance_matrix import CapacitanceMatrix 

36 

37 

38class NetlistExpander: 

39 @staticmethod 

40 def expand(extracted_netlist: kdb.Netlist, 

41 top_cell_name: str, 

42 cap_matrix: CapacitanceMatrix, 

43 blackbox_devices: bool) -> kdb.Netlist: 

44 expanded_netlist: kdb.Netlist = extracted_netlist.dup() 

45 top_circuit: kdb.Circuit = expanded_netlist.circuit_by_name(top_cell_name) 

46 

47 if not blackbox_devices: 

48 for d in top_circuit.each_device(): 

49 name = d.name or d.expanded_name() 

50 info(f"Removing whiteboxed device {name}") 

51 top_circuit.remove_device(d) 

52 

53 # create capacitor class 

54 cap = kdb.DeviceClassCapacitor() 

55 cap.name = 'PEX_CAP' 

56 cap.description = "Extracted by kpex/FasterCap PEX" 

57 expanded_netlist.add(cap) 

58 

59 fc_gnd_net = top_circuit.create_net('FC_GND') # create GROUND net 

60 vsubs_net = top_circuit.create_net("VSUBS") 

61 nets: List[kdb.Net] = [] 

62 

63 # build table: name -> net 

64 name2net: Dict[str, kdb.Net] = {n.expanded_name(): n for n in top_circuit.each_net()} 

65 

66 # find nets for the matrix axes 

67 pattern = re.compile(r'^g\d+_(.*)$') 

68 for idx, nn in enumerate(cap_matrix.conductor_names): 

69 m = pattern.match(nn) 

70 nn = m.group(1) 

71 if nn not in name2net: 

72 raise Exception(f"No net found with name {nn}, net names are: {list(name2net.keys())}") 

73 n = name2net[nn] 

74 nets.append(n) 

75 

76 cap_threshold = 0.0 

77 

78 def add_parasitic_cap(i: int, 

79 j: int, 

80 net1: kdb.Net, 

81 net2: kdb.Net, 

82 cap_value: float): 

83 if cap_value > cap_threshold: 

84 c: kdb.Device = top_circuit.create_device(cap, f"Cext_{i}_{j}") 

85 c.connect_terminal('A', net1) 

86 c.connect_terminal('B', net2) 

87 c.set_parameter('C', cap_value) 

88 if net1 == net2: 

89 raise Exception(f"Invalid attempt to create cap {c.name} between " 

90 f"same net {net1} with value {'%.12g' % cap_value}") 

91 else: 

92 warning(f"Ignoring capacitance matrix cell [{i},{j}], " 

93 f"{'%.12g' % cap_value} is below threshold {'%.12g' % cap_threshold}") 

94 

95 # ------------------------------------------------------------- 

96 # Example capacitance matrix: 

97 # [C11+C12+C13 -C12 -C13] 

98 # [-C21 C21+C22+C23 -C23] 

99 # [-C31 -C32 C31+C32+C33] 

100 # ------------------------------------------------------------- 

101 # 

102 # - Diagonal elements m[i][i] contain the capacitance over GND (Cii), 

103 # but in a sum including all the other values of the row 

104 # 

105 # https://www.fastfieldsolvers.com/Papers/The_Maxwell_Capacitance_Matrix_WP110301_R03.pdf 

106 # 

107 for i in range(0, cap_matrix.dimension): 

108 row = cap_matrix[i] 

109 cap_ii = row[i] 

110 for j in range(0, cap_matrix.dimension): 

111 if i == j: 

112 continue 

113 cap_value = -row[j] # off-diagonals are always stored as negative values 

114 cap_ii -= cap_value # subtract summands to filter out Cii 

115 if j > i: 

116 add_parasitic_cap(i=i, j=j, 

117 net1=nets[i], net2=nets[j], 

118 cap_value=cap_value) 

119 if i > 0: 

120 add_parasitic_cap(i=i, j=i, 

121 net1=nets[i], net2=nets[0], 

122 cap_value=cap_ii) 

123 

124 # Short VSUBS and FC_GND together 

125 # VSUBS ... substrate block 

126 # FC_GND ... FasterCap's GND, i.e. the diagonal Cii elements 

127 # create capacitor class 

128 

129 res = kdb.DeviceClassResistor() 

130 res.name = 'PEX_RES' 

131 res.description = "Extracted by kpex/FasterCap PEX" 

132 expanded_netlist.add(res) 

133 

134 gnd_net = name2net.get('GND', None) 

135 if not gnd_net: 

136 gnd_net = top_circuit.create_net('GND') # create GROUND net 

137 

138 c: kdb.Device = top_circuit.create_device(res, f"Rext_FC_GND_GND") 

139 c.connect_terminal('A', fc_gnd_net) 

140 c.connect_terminal('B', gnd_net) 

141 c.set_parameter('R', 0) 

142 

143 c: kdb.Device = top_circuit.create_device(res, f"Rext_VSUBS_GND") 

144 c.connect_terminal('A', vsubs_net) 

145 c.connect_terminal('B', gnd_net) 

146 c.set_parameter('R', 0) 

147 

148 return expanded_netlist