Coverage for klayout_pex/kpex_cli.py: 72%

508 statements  

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

1#! /usr/bin/env python3 

2# 

3# -------------------------------------------------------------------------------- 

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

5# Johannes Kepler University, Institute for Integrated Circuits. 

6# 

7# This file is part of KPEX  

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

9# 

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

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

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

13# (at your option) any later version. 

14# 

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

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

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

18# GNU General Public License for more details. 

19# 

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

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

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

23# -------------------------------------------------------------------------------- 

24# 

25 

26import argparse 

27from datetime import datetime 

28from enum import StrEnum 

29from functools import cached_property 

30import logging 

31import os 

32import os.path 

33from pathlib import Path 

34import rich.console 

35import rich.markdown 

36import rich.text 

37from rich_argparse import RichHelpFormatter 

38import shlex 

39import shutil 

40import sys 

41from typing import * 

42 

43import klayout.db as kdb 

44import klayout.rdb as rdb 

45 

46from .fastercap.fastercap_input_builder import FasterCapInputBuilder 

47from .fastercap.fastercap_model_generator import FasterCapModelGenerator 

48from .fastercap.fastercap_runner import run_fastercap, fastercap_parse_capacitance_matrix 

49from .fastcap.fastcap_runner import run_fastcap, fastcap_parse_capacitance_matrix 

50from .klayout.lvs_runner import LVSRunner 

51from .klayout.lvsdb_extractor import KLayoutExtractionContext, KLayoutExtractedLayerInfo 

52from .klayout.netlist_expander import NetlistExpander 

53from .klayout.netlist_csv import NetlistCSVWriter 

54from .klayout.netlist_reducer import NetlistReducer 

55from .klayout.repair_rdb import repair_rdb 

56from .log import ( 

57 LogLevel, 

58 set_log_level, 

59 register_additional_handler, 

60 deregister_additional_handler, 

61 # console, 

62 # debug, 

63 info, 

64 warning, 

65 subproc, 

66 error, 

67 rule 

68) 

69from .magic.magic_ext_file_parser import parse_magic_pex_run 

70from .magic.magic_runner import ( 

71 MagicPEXMode, 

72 MagicShortMode, 

73 MagicMergeMode, 

74 run_magic, 

75 prepare_magic_script, 

76) 

77from .magic.magic_log_analyzer import MagicLogAnalyzer 

78from .pdk_config import PDKConfig 

79from .rcx25.extractor import RCExtractor, ExtractionResults 

80from .rcx25.pex_mode import PEXMode 

81from .tech_info import TechInfo 

82from .util.multiple_choice import MultipleChoicePattern 

83from .util.argparse_helpers import render_enum_help, true_or_false 

84from .version import __version__ 

85 

86 

87# ------------------------------------------------------------------------------------ 

88 

89PROGRAM_NAME = "kpex" 

90 

91 

92class ArgumentValidationError(Exception): 

93 pass 

94 

95 

96class InputMode(StrEnum): 

97 LVSDB = "lvsdb" 

98 GDS = "gds" 

99 

100 

101# TODO: this should be externally configurable 

102class PDK(StrEnum): 

103 IHP_SG13G2 = 'ihp_sg13g2' 

104 SKY130A = 'sky130A' 

105 

106 @cached_property 

107 def config(self) -> PDKConfig: 

108 # NOTE: installation paths of resources in the distribution wheel differs from source repo 

109 base_dir = os.path.dirname(os.path.realpath(__file__)) 

110 

111 # NOTE: .git can be dir (standalone clone), or file (in case of submodule) 

112 if os.path.exists(os.path.join(base_dir, '..', '.git')): # in source repo 

113 base_dir = os.path.dirname(base_dir) 

114 tech_pb_json_dir = os.path.join(base_dir, 'klayout_pex_protobuf') 

115 else: # site-packages/klayout_pex -> site-packages/klayout_pex_protobuf 

116 tech_pb_json_dir = os.path.join(os.path.dirname(base_dir), 'klayout_pex_protobuf') 

117 

118 match self: 

119 case PDK.IHP_SG13G2: 

120 return PDKConfig( 

121 name=self, 

122 pex_lvs_script_path=os.path.join(base_dir, 'pdk', self, 'libs.tech', 'kpex', 'sg130g2.lvs'), 

123 tech_pb_json_path=os.path.join(tech_pb_json_dir, f"{self}_tech.pb.json") 

124 ) 

125 case PDK.SKY130A: 

126 return PDKConfig( 

127 name=self, 

128 pex_lvs_script_path=os.path.join(base_dir, 'pdk', self, 'libs.tech', 'kpex', 'sky130.lvs'), 

129 tech_pb_json_path=os.path.join(tech_pb_json_dir, f"{self}_tech.pb.json") 

130 ) 

131 

132 

133class KpexCLI: 

134 @staticmethod 

135 def parse_args(arg_list: List[str] = None) -> argparse.Namespace: 

136 # epilog = f"See '{PROGRAM_NAME} <subcommand> -h' for help on subcommand" 

137 epilog = """ 

138| Variable | Example | Description | 

139| -------- | -------------------- | --------------------------------------- | 

140| PDKPATH | (e.g. $HOME/.volare) | Optional (required for default magicrc) | 

141| PDK | (e.g. sky130A) | Optional (required for default magicrc) | 

142""" 

143 epilog_md = rich.console.Group( 

144 rich.text.Text('Environmental variables:', style='argparse.groups'), 

145 rich.markdown.Markdown(epilog, style='argparse.text') 

146 ) 

147 main_parser = argparse.ArgumentParser(description=f"{PROGRAM_NAME}: " 

148 f"KLayout-integrated Parasitic Extraction Tool", 

149 epilog=epilog_md, 

150 add_help=False, 

151 formatter_class=RichHelpFormatter) 

152 

153 group_special = main_parser.add_argument_group("Special options") 

154 group_special.add_argument("--help", "-h", action='help', help="show this help message and exit") 

155 group_special.add_argument("--version", "-v", action='version', version=f'{PROGRAM_NAME} {__version__}') 

156 group_special.add_argument("--log_level", dest='log_level', default='subprocess', 

157 help=render_enum_help(topic='log_level', enum_cls=LogLevel)) 

158 group_special.add_argument("--threads", dest='num_threads', type=int, 

159 default=os.cpu_count() * 4, 

160 help="number of threads (e.g. for FasterCap) (default is %(default)s)") 

161 

162 klayout_exe_default = 'klayout' 

163 if os.name == 'nt': 

164 klayout_exe_default = 'klayout_app' 

165 

166 group_special.add_argument('--klayout', dest='klayout_exe_path', default=klayout_exe_default, 

167 help="Path to klayout executable (default is '%(default)s')") 

168 

169 group_pex = main_parser.add_argument_group("Parasitic Extraction Setup") 

170 group_pex.add_argument("--pdk", dest="pdk", required=True, 

171 type=PDK, choices=list(PDK), 

172 help=render_enum_help(topic='pdk', enum_cls=PDK)) 

173 

174 group_pex.add_argument("--out_dir", "-o", dest="output_dir_base_path", default="output", 

175 help="Output directory path (default is '%(default)s')") 

176 

177 group_pex_input = main_parser.add_argument_group("Parasitic Extraction Input", 

178 description="Either LVS is run, or an existing LVSDB is used") 

179 group_pex_input.add_argument("--gds", "-g", dest="gds_path", help="GDS path (for LVS)") 

180 group_pex_input.add_argument("--schematic", "-s", dest="schematic_path", 

181 help="Schematic SPICE netlist path (for LVS). " 

182 "If none given, a dummy schematic will be created") 

183 group_pex_input.add_argument("--lvsdb", "-l", dest="lvsdb_path", help="KLayout LVSDB path (bypass LVS)") 

184 group_pex_input.add_argument("--cell", "-c", dest="cell_name", default=None, 

185 help="Cell (default is the top cell)") 

186 

187 group_pex_input.add_argument("--cache-lvs", dest="cache_lvs", 

188 type=true_or_false, default=True, 

189 help="Used cached LVSDB (for given input GDS) (default is %(default)s)") 

190 group_pex_input.add_argument("--cache-dir", dest="cache_dir_path", default=None, 

191 help="Path for cached LVSDB (default is .kpex_cache within --out_dir)") 

192 group_pex_input.add_argument("--lvs-verbose", dest="klayout_lvs_verbose", 

193 type=true_or_false, default=False, 

194 help="Verbose KLayout LVS output (default is %(default)s)") 

195 

196 group_pex_options = main_parser.add_argument_group("Parasitic Extraction Options") 

197 group_pex_options.add_argument("--blackbox", dest="blackbox_devices", 

198 type=true_or_false, default=False, # TODO: in the future this should be True by default 

199 help="Blackbox devices like MIM/MOM caps, as they are handled by SPICE models " 

200 "(default is %(default)s for testing now)") 

201 group_pex_options.add_argument("--fastercap", dest="run_fastercap", 

202 action='store_true', default=False, 

203 help="Run FasterCap engine (default is %(default)s)") 

204 group_pex_options.add_argument("--fastcap", dest="run_fastcap", 

205 action='store_true', default=False, 

206 help="Run FastCap2 engine (default is %(default)s)") 

207 group_pex_options.add_argument("--magic", dest="run_magic", 

208 action='store_true', default=False, 

209 help="Run MAGIC engine (default is %(default)s)") 

210 group_pex_options.add_argument("--2.5D", dest="run_2_5D", 

211 action='store_true', default=False, 

212 help="Run 2.5D analytical engine (default is %(default)s)") 

213 

214 group_fastercap = main_parser.add_argument_group("FasterCap options") 

215 group_fastercap.add_argument("--k_void", "-k", dest="k_void", 

216 type=float, default=3.9, 

217 help="Dielectric constant of void (default is %(default)s)") 

218 

219 # TODO: reflect that these are also now used by KPEX/2.5D engine! 

220 group_fastercap.add_argument("--delaunay_amax", "-a", dest="delaunay_amax", 

221 type=float, default=50, 

222 help="Delaunay triangulation maximum area (default is %(default)s)") 

223 group_fastercap.add_argument("--delaunay_b", "-b", dest="delaunay_b", 

224 type=float, default=0.5, 

225 help="Delaunay triangulation b (default is %(default)s)") 

226 group_fastercap.add_argument("--geo_check", dest="geometry_check", 

227 type=true_or_false, default=False, 

228 help=f"Validate geometries before passing to FasterCap " 

229 f"(default is False)") 

230 group_fastercap.add_argument("--diel", dest="dielectric_filter", 

231 type=str, default="all", 

232 help=f"Comma separated list of dielectric filter patterns. " 

233 f"Allowed patterns are: (none, all, -dielname1, +dielname2) " 

234 f"(default is %(default)s)") 

235 

236 group_fastercap.add_argument("--tolerance", dest="fastercap_tolerance", 

237 type=float, default=0.05, 

238 help="FasterCap -aX error tolerance (default is %(default)s)") 

239 group_fastercap.add_argument("--d_coeff", dest="fastercap_d_coeff", 

240 type=float, default=0.5, 

241 help=f"FasterCap -d direct potential interaction coefficient to mesh refinement " 

242 f"(default is %(default)s)") 

243 group_fastercap.add_argument("--mesh", dest="fastercap_mesh_refinement_value", 

244 type=float, default=0.5, 

245 help="FasterCap -m Mesh relative refinement value (default is %(default)s)") 

246 group_fastercap.add_argument("--ooc", dest="fastercap_ooc_condition", 

247 type=float, default=2, 

248 help="FasterCap -f out-of-core free memory to link memory condition " 

249 "(0 = don't go OOC, default is %(default)s)") 

250 group_fastercap.add_argument("--auto_precond", dest="fastercap_auto_preconditioner", 

251 type=true_or_false, default=True, 

252 help=f"FasterCap -ap Automatic preconditioner usage (default is %(default)s)") 

253 group_fastercap.add_argument("--galerkin", dest="fastercap_galerkin_scheme", 

254 action='store_true', default=False, 

255 help=f"FasterCap -g Use Galerkin scheme (default is %(default)s)") 

256 group_fastercap.add_argument("--jacobi", dest="fastercap_jacobi_preconditioner", 

257 action='store_true', default=False, 

258 help="FasterCap -pj Use Jacobi preconditioner (default is %(default)s)") 

259 

260 PDKPATH = os.environ.get('PDKPATH', None) 

261 default_magicrc_path = \ 

262 None if PDKPATH is None \ 

263 else os.path.abspath(f"{PDKPATH}/libs.tech/magic/{os.environ['PDK']}.magicrc") 

264 group_magic = main_parser.add_argument_group("MAGIC options") 

265 group_magic.add_argument('--magicrc', dest='magicrc_path', default=default_magicrc_path, 

266 help=f"Path to magicrc configuration file (default is '%(default)s')") 

267 group_magic.add_argument("--magic_mode", dest='magic_pex_mode', 

268 default=MagicPEXMode.DEFAULT, type=MagicPEXMode, choices=list(MagicPEXMode), 

269 help=render_enum_help(topic='magic_mode', enum_cls=MagicPEXMode)) 

270 group_magic.add_argument("--magic_cthresh", dest="magic_cthresh", 

271 type=float, default=0.01, 

272 help="Threshold (in fF) for ignored parasitic capacitances (default is %(default)s). " 

273 "(MAGIC command: ext2spice cthresh <value>)") 

274 group_magic.add_argument("--magic_rthresh", dest="magic_rthresh", 

275 type=int, default=100, 

276 help="Threshold (in Ω) for ignored parasitic resistances (default is %(default)s). " 

277 "(MAGIC command: ext2spice rthresh <value>)") 

278 group_magic.add_argument("--magic_tolerance", dest="magic_tolerance", 

279 type=float, default=1, 

280 help="Set ratio between resistor and device tolerance (default is %(default)s). " 

281 "(MAGIC command: extresist tolerance <value>)") 

282 group_magic.add_argument("--magic_halo", dest="magic_halo", 

283 type=float, default=None, 

284 help="Custom sidewall halo distance (in µm) " 

285 "(MAGIC command: extract halo <value>) (default is no custom halo)") 

286 group_magic.add_argument("--magic_short", dest='magic_short_mode', 

287 default=MagicShortMode.DEFAULT, type=MagicShortMode, choices=list(MagicShortMode), 

288 help=render_enum_help(topic='magic_short', enum_cls=MagicShortMode)) 

289 group_magic.add_argument("--magic_merge", dest='magic_merge_mode', 

290 default=MagicMergeMode.DEFAULT, type=MagicMergeMode, choices=list(MagicMergeMode), 

291 help=render_enum_help(topic='magic_merge', enum_cls=MagicMergeMode)) 

292 group_magic.add_argument('--magic_exe', dest='magic_exe_path', default='magic', 

293 help="Path to magic executable (default is '%(default)s')") 

294 

295 group_25d = main_parser.add_argument_group("2.5D options") 

296 group_25d.add_argument("--mode", dest='pex_mode', 

297 default=PEXMode.DEFAULT, type=PEXMode, choices=list(PEXMode), 

298 help=render_enum_help(topic='mode', enum_cls=PEXMode)) 

299 group_25d.add_argument("--halo", dest="halo", 

300 type=float, default=None, 

301 help="Custom sidewall halo distance (in µm) to override tech info " 

302 "(default is no custom halo)") 

303 group_25d.add_argument("--scale", dest="scale_ratio_to_fit_halo", 

304 type=true_or_false, default=True, 

305 help=f"Scale fringe ratios, so that halo distance is 100%% (default is %(default)s)") 

306 

307 if arg_list is None: 

308 arg_list = sys.argv[1:] 

309 args = main_parser.parse_args(arg_list) 

310 return args 

311 

312 @staticmethod 

313 def validate_args(args: argparse.Namespace): 

314 found_errors = False 

315 

316 pdk_config: PDKConfig = args.pdk.config 

317 args.tech_pbjson_path = pdk_config.tech_pb_json_path 

318 args.lvs_script_path = pdk_config.pex_lvs_script_path 

319 

320 def input_file_stem(path: str): 

321 # could be *.gds, or *.gds.gz, so remove all extensions 

322 return os.path.basename(path).split(sep='.')[0] 

323 

324 if not os.path.isfile(args.klayout_exe_path): 

325 path = shutil.which(args.klayout_exe_path) 

326 if not path: 

327 error(f"Can't locate KLayout executable at {args.klayout_exe_path}") 

328 found_errors = True 

329 

330 if not os.path.isfile(args.tech_pbjson_path): 

331 error(f"Can't read technology file at path {args.tech_pbjson_path}") 

332 found_errors = True 

333 

334 if not os.path.isfile(args.lvs_script_path): 

335 error(f"Can't locate LVS script path at {args.lvs_script_path}") 

336 found_errors = True 

337 

338 rule('Input Layout') 

339 

340 # input mode: LVS or existing LVSDB? 

341 if args.gds_path: 

342 info(f"GDS input file passed, running in LVS mode") 

343 args.input_mode = InputMode.GDS 

344 if not os.path.isfile(args.gds_path): 

345 error(f"Can't read GDS file (LVS input) at path {args.gds_path}") 

346 found_errors = True 

347 else: 

348 args.layout = kdb.Layout() 

349 args.layout.read(args.gds_path) 

350 

351 top_cells = args.layout.top_cells() 

352 

353 if args.cell_name: # explicit user-specified cell name 

354 args.effective_cell_name = args.cell_name 

355 

356 found_cell: Optional[kdb.Cell] = None 

357 for cell in args.layout.cells('*'): 

358 if cell.name == args.effective_cell_name: 

359 found_cell = cell 

360 break 

361 if not found_cell: 

362 error(f"Could not find cell {args.cell_name} in GDS {args.gds_path}") 

363 found_errors = True 

364 

365 is_only_top_cell = len(top_cells) == 1 and top_cells[0].name == args.cell_name 

366 if is_only_top_cell: 

367 info(f"Found cell {args.cell_name} in GDS {args.gds_path} (only top cell)") 

368 else: # there are other cells => extract the top cell to a tmp layout 

369 run_dir_id = f"{input_file_stem(args.gds_path)}__{args.effective_cell_name}" 

370 args.output_dir_path = os.path.join(args.output_dir_base_path, run_dir_id) 

371 os.makedirs(args.output_dir_path, exist_ok=True) 

372 args.effective_gds_path = os.path.join(args.output_dir_path, 

373 f"{args.cell_name}_exported.gds.gz") 

374 info(f"Found cell {args.cell_name} in GDS {args.gds_path}, " 

375 f"but it is not the only top cell, " 

376 f"so layout is exported to: {args.effective_gds_path}") 

377 

378 found_cell.write(args.effective_gds_path) 

379 else: # find top cell 

380 if len(top_cells) == 1: 

381 args.effective_cell_name = top_cells[0].name 

382 info(f"No explicit top cell specified, using top cell '{args.effective_cell_name}'") 

383 else: 

384 args.effective_cell_name = 'TOP' 

385 error(f"Could not determine the default top cell in GDS {args.gds_path}, " 

386 f"there are multiple: {', '.join([c.name for c in top_cells])}. " 

387 f"Use --cell to specify the cell") 

388 found_errors = True 

389 

390 if not hasattr(args, 'effective_gds_path'): 

391 args.effective_gds_path = args.gds_path 

392 else: 

393 info(f"LVSDB input file passed, bypassing LVS") 

394 args.input_mode = InputMode.LVSDB 

395 if not hasattr(args, 'lvsdb_path'): 

396 error(f"LVSDB input path not specified (argument --lvsdb)") 

397 found_errors = True 

398 elif not os.path.isfile(args.lvsdb_path): 

399 error(f"Can't read KLayout LVSDB file at path {args.lvsdb_path}") 

400 found_errors = True 

401 else: 

402 lvsdb = kdb.LayoutVsSchematic() 

403 lvsdb.read(args.lvsdb_path) 

404 top_cell: kdb.Cell = lvsdb.internal_top_cell() 

405 args.effective_cell_name = top_cell.name 

406 

407 if hasattr(args, 'effective_cell_name'): 

408 run_dir_id: str 

409 match args.input_mode: 

410 case InputMode.GDS: 

411 run_dir_id = f"{input_file_stem(args.gds_path)}__{args.effective_cell_name}" 

412 case InputMode.LVSDB: 

413 run_dir_id = f"{input_file_stem(args.lvsdb_path)}__{args.effective_cell_name}" 

414 case _: 

415 raise NotImplementedError(f"Unknown input mode {args.input_mode}") 

416 

417 args.output_dir_path = os.path.join(args.output_dir_base_path, run_dir_id) 

418 os.makedirs(args.output_dir_path, exist_ok=True) 

419 if args.input_mode == InputMode.GDS: 

420 if args.schematic_path: 

421 args.effective_schematic_path = args.schematic_path 

422 if not os.path.isfile(args.schematic_path): 

423 error(f"Can't read schematic (LVS input) at path {args.schematic_path}") 

424 found_errors = True 

425 else: 

426 info(f"LVS input schematic not specified (argument --schematic), using dummy schematic") 

427 args.effective_schematic_path = os.path.join(args.output_dir_path, 

428 f"{args.effective_cell_name}_dummy_schematic.spice") 

429 with open(args.effective_schematic_path, 'w') as f: 

430 f.writelines([ 

431 f".subckt {args.effective_cell_name} VDD VSS\n", 

432 '.ends\n', 

433 '.end\n' 

434 ]) 

435 

436 try: 

437 args.log_level = LogLevel[args.log_level.upper()] 

438 except KeyError: 

439 error(f"Requested log level {args.log_level.lower()} does not exist, " 

440 f"{render_enum_help(topic='log_level', enum_cls=LogLevel, print_default=False)}") 

441 found_errors = True 

442 

443 try: 

444 pattern_string: str = args.dielectric_filter 

445 args.dielectric_filter = MultipleChoicePattern(pattern=pattern_string) 

446 except ValueError as e: 

447 error("Failed to parse --diel arg", e) 

448 found_errors = True 

449 

450 # at least one engine must be activated 

451 

452 if not (args.run_magic or args.run_fastcap or args.run_fastercap or args.run_2_5D): 

453 error("No PEX engines activated") 

454 engine_help = """ 

455| Argument | Description | 

456| ------------ | ------------------------- | 

457| --fastercap | Run kpex/FasterCap engine | 

458| --2.5D | Run kpex/2.5D engine | 

459| --magic | Run MAGIC engine | 

460""" 

461 subproc(f"\n\nPlease activate one or more engines using the arguments:") 

462 rich.print(rich.markdown.Markdown(engine_help, style='argparse.text')) 

463 found_errors = True 

464 

465 if args.cache_dir_path is None: 

466 args.cache_dir_path = os.path.join(args.output_dir_base_path, '.kpex_cache') 

467 

468 if found_errors: 

469 raise ArgumentValidationError("Argument validation failed") 

470 

471 def build_fastercap_input(self, 

472 args: argparse.Namespace, 

473 pex_context: KLayoutExtractionContext, 

474 tech_info: TechInfo) -> str: 

475 rule('Process stackup') 

476 fastercap_input_builder = FasterCapInputBuilder(pex_context=pex_context, 

477 tech_info=tech_info, 

478 k_void=args.k_void, 

479 delaunay_amax=args.delaunay_amax, 

480 delaunay_b=args.delaunay_b) 

481 gen: FasterCapModelGenerator = fastercap_input_builder.build() 

482 

483 rule('FasterCap Input File Generation') 

484 faster_cap_input_dir_path = os.path.join(args.output_dir_path, 'FasterCap_Input_Files') 

485 os.makedirs(faster_cap_input_dir_path, exist_ok=True) 

486 

487 lst_file = gen.write_fastcap(output_dir_path=faster_cap_input_dir_path, prefix='FasterCap_Input_') 

488 

489 rule('STL File Generation') 

490 geometry_dir_path = os.path.join(args.output_dir_path, 'Geometries') 

491 os.makedirs(geometry_dir_path, exist_ok=True) 

492 gen.dump_stl(output_dir_path=geometry_dir_path, prefix='') 

493 

494 if args.geometry_check: 

495 rule('Geometry Validation') 

496 gen.check() 

497 

498 return lst_file 

499 

500 

501 def run_fastercap_extraction(self, 

502 args: argparse.Namespace, 

503 pex_context: KLayoutExtractionContext, 

504 lst_file: str): 

505 rule('FasterCap Execution') 

506 info(f"Configure number of OpenMP threads (environmental variable OMP_NUM_THREADS) as {args.num_threads}") 

507 os.environ['OMP_NUM_THREADS'] = f"{args.num_threads}" 

508 

509 exe_path = "FasterCap" 

510 log_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_FasterCap_Output.txt") 

511 raw_csv_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_FasterCap_Result_Matrix_Raw.csv") 

512 avg_csv_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_FasterCap_Result_Matrix_Avg.csv") 

513 expanded_netlist_path = os.path.join(args.output_dir_path, 

514 f"{args.effective_cell_name}_FasterCap_Expanded_Netlist.cir") 

515 expanded_netlist_csv_path = os.path.join(args.output_dir_path, 

516 f"{args.effective_cell_name}_FasterCap_Expanded_Netlist.csv") 

517 reduced_netlist_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_FasterCap_Reduced_Netlist.cir") 

518 

519 run_fastercap(exe_path=exe_path, 

520 lst_file_path=lst_file, 

521 log_path=log_path, 

522 tolerance=args.fastercap_tolerance, 

523 d_coeff=args.fastercap_d_coeff, 

524 mesh_refinement_value=args.fastercap_mesh_refinement_value, 

525 ooc_condition=args.fastercap_ooc_condition, 

526 auto_preconditioner=args.fastercap_auto_preconditioner, 

527 galerkin_scheme=args.fastercap_galerkin_scheme, 

528 jacobi_preconditioner=args.fastercap_jacobi_preconditioner) 

529 

530 cap_matrix = fastercap_parse_capacitance_matrix(log_path) 

531 cap_matrix.write_csv(raw_csv_path) 

532 

533 cap_matrix = cap_matrix.averaged_off_diagonals() 

534 cap_matrix.write_csv(avg_csv_path) 

535 

536 netlist_expander = NetlistExpander() 

537 expanded_netlist = netlist_expander.expand( 

538 extracted_netlist=pex_context.lvsdb.netlist(), 

539 top_cell_name=pex_context.annotated_top_cell.name, 

540 cap_matrix=cap_matrix, 

541 blackbox_devices=args.blackbox_devices 

542 ) 

543 

544 # create a nice CSV for reports, useful for spreadsheets 

545 netlist_csv_writer = NetlistCSVWriter() 

546 netlist_csv_writer.write_csv(netlist=expanded_netlist, 

547 top_cell_name=pex_context.annotated_top_cell.name, 

548 output_path=expanded_netlist_csv_path) 

549 

550 rule("Extended netlist (CSV format):") 

551 with open(expanded_netlist_csv_path, 'r') as f: 

552 for line in f.readlines(): 

553 subproc(line[:-1]) # abusing subproc, simply want verbatim 

554 rule() 

555 

556 info(f"Wrote expanded netlist CSV to: {expanded_netlist_csv_path}") 

557 

558 spice_writer = kdb.NetlistSpiceWriter() 

559 spice_writer.use_net_names = True 

560 spice_writer.with_comments = False 

561 expanded_netlist.write(expanded_netlist_path, spice_writer) 

562 info(f"Wrote expanded netlist to: {expanded_netlist_path}") 

563 

564 netlist_reducer = NetlistReducer() 

565 reduced_netlist = netlist_reducer.reduce(netlist=expanded_netlist, 

566 top_cell_name=pex_context.annotated_top_cell.name) 

567 reduced_netlist.write(reduced_netlist_path, spice_writer) 

568 info(f"Wrote reduced netlist to: {reduced_netlist_path}") 

569 

570 self._fastercap_extracted_csv_path = expanded_netlist_csv_path 

571 

572 def run_magic_extraction(self, 

573 args: argparse.Namespace): 

574 if args.input_mode != InputMode.GDS: 

575 error(f"MAGIC engine only works with GDS input mode" 

576 f" (currently {args.input_mode})") 

577 return 

578 

579 magic_run_dir = os.path.join(args.output_dir_path, f"magic_{args.magic_pex_mode}") 

580 magic_log_path = os.path.join(magic_run_dir, 

581 f"{args.effective_cell_name}_MAGIC_{args.magic_pex_mode}_Output.txt") 

582 magic_script_path = os.path.join(magic_run_dir, 

583 f"{args.effective_cell_name}_MAGIC_{args.magic_pex_mode}_Script.tcl") 

584 

585 output_netlist_path = os.path.join(magic_run_dir, f"{args.effective_cell_name}.pex.spice") 

586 report_db_path = os.path.join(magic_run_dir, f"{args.effective_cell_name}_MAGIC_report.rdb.gz") 

587 

588 os.makedirs(magic_run_dir, exist_ok=True) 

589 

590 prepare_magic_script(gds_path=args.effective_gds_path, 

591 cell_name=args.effective_cell_name, 

592 run_dir_path=magic_run_dir, 

593 script_path=magic_script_path, 

594 output_netlist_path=output_netlist_path, 

595 pex_mode=args.magic_pex_mode, 

596 c_threshold=args.magic_cthresh, 

597 r_threshold=args.magic_rthresh, 

598 tolerance=args.magic_tolerance, 

599 halo=args.magic_halo, 

600 short_mode=args.magic_short_mode, 

601 merge_mode=args.magic_merge_mode) 

602 

603 run_magic(exe_path=args.magic_exe_path, 

604 magicrc_path=args.magicrc_path, 

605 script_path=magic_script_path, 

606 log_path=magic_log_path) 

607 

608 magic_pex_run = parse_magic_pex_run(Path(magic_run_dir)) 

609 

610 layout = kdb.Layout() 

611 layout.read(args.effective_gds_path) 

612 

613 report = rdb.ReportDatabase('') 

614 magic_log_analyzer = MagicLogAnalyzer(magic_pex_run=magic_pex_run, 

615 report=report, 

616 dbu=layout.dbu) 

617 magic_log_analyzer.analyze() 

618 report.save(report_db_path) 

619 

620 rule("Paths") 

621 subproc(f"Report DB saved at: {report_db_path}") 

622 subproc(f"SPICE netlist saved at: {output_netlist_path}") 

623 

624 rule("MAGIC PEX SPICE netlist") 

625 with open(output_netlist_path, 'r') as f: 

626 subproc(f.read()) 

627 rule() 

628 

629 def run_fastcap_extraction(self, 

630 args: argparse.Namespace, 

631 pex_context: KLayoutExtractionContext, 

632 lst_file: str): 

633 rule('FastCap2 Execution') 

634 exe_path = "fastcap" 

635 log_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_FastCap2_Output.txt") 

636 raw_csv_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_FastCap2_Result_Matrix_Raw.csv") 

637 avg_csv_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_FastCap2_Result_Matrix_Avg.csv") 

638 expanded_netlist_path = os.path.join(args.output_dir_path, 

639 f"{args.effective_cell_name}_FastCap2_Expanded_Netlist.cir") 

640 reduced_netlist_path = os.path.join(args.output_dir_path, 

641 f"{args.effective_cell_name}_FastCap2_Reduced_Netlist.cir") 

642 

643 run_fastcap(exe_path=exe_path, 

644 lst_file_path=lst_file, 

645 log_path=log_path) 

646 

647 cap_matrix = fastcap_parse_capacitance_matrix(log_path) 

648 cap_matrix.write_csv(raw_csv_path) 

649 

650 cap_matrix = cap_matrix.averaged_off_diagonals() 

651 cap_matrix.write_csv(avg_csv_path) 

652 

653 netlist_expander = NetlistExpander() 

654 expanded_netlist = netlist_expander.expand( 

655 extracted_netlist=pex_context.lvsdb.netlist(), 

656 top_cell_name=pex_context.annotated_top_cell.name, 

657 cap_matrix=cap_matrix, 

658 blackbox_devices=args.blackbox_devices 

659 ) 

660 

661 spice_writer = kdb.NetlistSpiceWriter() 

662 spice_writer.use_net_names = True 

663 spice_writer.with_comments = False 

664 expanded_netlist.write(expanded_netlist_path, spice_writer) 

665 info(f"Wrote expanded netlist to: {expanded_netlist_path}") 

666 

667 netlist_reducer = NetlistReducer() 

668 reduced_netlist = netlist_reducer.reduce(netlist=expanded_netlist, 

669 top_cell_name=pex_context.annotated_top_cell.name) 

670 reduced_netlist.write(reduced_netlist_path, spice_writer) 

671 info(f"Wrote reduced netlist to: {reduced_netlist_path}") 

672 

673 def run_kpex_2_5d_engine(self, 

674 args: argparse.Namespace, 

675 pex_context: KLayoutExtractionContext, 

676 tech_info: TechInfo, 

677 report_path: str, 

678 netlist_csv_path: str): 

679 # TODO: make this separatly configurable 

680 # for now we use 0 

681 args.rcx25d_delaunay_amax = 0 

682 args.rcx25d_delaunay_b = 0.5 

683 

684 extractor = RCExtractor(pex_context=pex_context, 

685 pex_mode=args.pex_mode, 

686 delaunay_amax=args.rcx25d_delaunay_amax, 

687 delaunay_b=args.rcx25d_delaunay_b, 

688 scale_ratio_to_fit_halo=args.scale_ratio_to_fit_halo, 

689 tech_info=tech_info, 

690 report_path=report_path) 

691 extraction_results = extractor.extract() 

692 

693 with open(netlist_csv_path, 'w') as f: 

694 summary = extraction_results.summarize() 

695 

696 f.write('Device;Net1;Net2;Capacitance [fF];Resistance [Ω]\n') 

697 for idx, (key, cap_value) in enumerate(summary.capacitances.items()): 

698 # f.write(f"C{idx + 1};{key.net1};{key.net2};{cap_value / 1e15};{round(cap_value, 3)}\n") 

699 f.write(f"C{idx + 1};{key.net1};{key.net2};{round(cap_value, 3)};\n") 

700 for idx, (key, res_value) in enumerate(summary.resistances.items()): 

701 f.write(f"R{idx + 1};{key.net1};{key.net2};;{round(res_value, 3)}\n") 

702 

703 rule("kpex/2.5D extracted netlist (CSV format):") 

704 with open(netlist_csv_path, 'r') as f: 

705 for line in f.readlines(): 

706 subproc(line[:-1]) # abusing subproc, simply want verbatim 

707 

708 rule("Extracted netlist CSV") 

709 subproc(f"{netlist_csv_path}") 

710 

711 

712 # NOTE: there was a KLayout bug that some of the categories were lost, 

713 # so that the marker browser could not load the report file 

714 try: 

715 report = rdb.ReportDatabase('') 

716 report.load(report_path) # try loading rdb 

717 except Exception as e: 

718 rule("Repair broken marker DB") 

719 warning(f"Detected KLayout bug: RDB can't be loaded due to exception {e}") 

720 repair_rdb(report_path) 

721 

722 return extraction_results 

723 

724 def setup_logging(self, args: argparse.Namespace): 

725 def register_log_file_handler(log_path: str, 

726 formatter: Optional[logging.Formatter]) -> logging.Handler: 

727 handler = logging.FileHandler(log_path) 

728 handler.setLevel(LogLevel.SUBPROCESS) 

729 if formatter: 

730 handler.setFormatter(formatter) 

731 register_additional_handler(handler) 

732 return handler 

733 

734 def reregister_log_file_handler(handler: logging.Handler, 

735 log_path: str, 

736 formatter: Optional[logging.Formatter]): 

737 deregister_additional_handler(handler) 

738 handler.flush() 

739 handler.close() 

740 os.makedirs(args.output_dir_path, exist_ok=True) 

741 new_path = os.path.join(args.output_dir_path, os.path.basename(log_path)) 

742 if os.path.exists(new_path): 

743 ctime = os.path.getctime(new_path) 

744 dt = datetime.fromtimestamp(ctime) 

745 timestamp = dt.strftime('%Y-%m-%d_%H-%M-%S') 

746 backup_path = f"{new_path[:-4]}_{timestamp}.bak.log" 

747 shutil.move(new_path, backup_path) 

748 log_path = shutil.move(log_path, new_path) 

749 register_log_file_handler(log_path, formatter) 

750 

751 # setup preliminary logger 

752 cli_log_path_plain = os.path.join(args.output_dir_base_path, f"kpex_plain.log") 

753 cli_log_path_formatted = os.path.join(args.output_dir_base_path, f"kpex.log") 

754 formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s') 

755 file_handler_plain = register_log_file_handler(cli_log_path_plain, None) 

756 file_handler_formatted = register_log_file_handler(cli_log_path_formatted, formatter) 

757 try: 

758 self.validate_args(args) 

759 except ArgumentValidationError: 

760 if hasattr(args, 'output_dir_path'): 

761 reregister_log_file_handler(file_handler_plain, cli_log_path_plain, None) 

762 reregister_log_file_handler(file_handler_formatted, cli_log_path_formatted, formatter) 

763 sys.exit(1) 

764 reregister_log_file_handler(file_handler_plain, cli_log_path_plain, None) 

765 reregister_log_file_handler(file_handler_formatted, cli_log_path_formatted, formatter) 

766 

767 set_log_level(args.log_level) 

768 

769 @staticmethod 

770 def modification_date(filename: str) -> datetime: 

771 t = os.path.getmtime(filename) 

772 return datetime.fromtimestamp(t) 

773 

774 def create_lvsdb(self, args: argparse.Namespace) -> kdb.LayoutVsSchematic: 

775 lvsdb = kdb.LayoutVsSchematic() 

776 

777 match args.input_mode: 

778 case InputMode.LVSDB: 

779 lvsdb.read(args.lvsdb_path) 

780 case InputMode.GDS: 

781 lvs_log_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_lvs.log") 

782 lvsdb_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}.lvsdb.gz") 

783 lvsdb_cache_path = os.path.join(args.cache_dir_path, args.pdk, 

784 os.path.splitroot(os.path.abspath(args.gds_path))[-1], 

785 f"{args.effective_cell_name}.lvsdb.gz") 

786 

787 lvs_needed = True 

788 

789 if args.cache_lvs: 

790 if not os.path.exists(lvsdb_cache_path): 

791 info(f"Cache miss: extracted LVSDB does not exist") 

792 subproc(lvsdb_cache_path) 

793 elif self.modification_date(lvsdb_cache_path) <= self.modification_date(args.gds_path): 

794 info(f"Cache miss: extracted LVSDB is older than the input GDS") 

795 subproc(lvsdb_cache_path) 

796 else: 

797 warning(f"Cache hit: Reusing cached LVSDB") 

798 subproc(lvsdb_cache_path) 

799 lvs_needed = False 

800 

801 if lvs_needed: 

802 lvs_runner = LVSRunner() 

803 lvs_runner.run_klayout_lvs(exe_path=args.klayout_exe_path, 

804 lvs_script=args.lvs_script_path, 

805 gds_path=args.effective_gds_path, 

806 schematic_path=args.effective_schematic_path, 

807 log_path=lvs_log_path, 

808 lvsdb_path=lvsdb_path, 

809 verbose=args.klayout_lvs_verbose) 

810 if args.cache_lvs: 

811 cache_dir_path = os.path.dirname(lvsdb_cache_path) 

812 if not os.path.exists(cache_dir_path): 

813 os.makedirs(cache_dir_path, exist_ok=True) 

814 shutil.copy(lvsdb_path, lvsdb_cache_path) 

815 

816 lvsdb.read(lvsdb_path) 

817 return lvsdb 

818 

819 def main(self, argv: List[str]): 

820 if '-v' not in argv and \ 

821 '--version' not in argv and \ 

822 '-h' not in argv and \ 

823 '--help' not in argv: 

824 rule('Command line arguments') 

825 subproc(' '.join(map(shlex.quote, sys.argv))) 

826 

827 args = self.parse_args(argv[1:]) 

828 

829 os.makedirs(args.output_dir_base_path, exist_ok=True) 

830 self.setup_logging(args) 

831 

832 tech_info = TechInfo.from_json(args.tech_pbjson_path, 

833 dielectric_filter=args.dielectric_filter) 

834 

835 if args.halo is not None: 

836 tech_info.tech.process_parasitics.side_halo = args.halo 

837 

838 if args.run_magic: 

839 rule('MAGIC') 

840 self.run_magic_extraction(args) 

841 

842 # no need to run LVS etc if only running magic engine 

843 if not (args.run_fastcap or args.run_fastercap or args.run_2_5D): 

844 return 

845 

846 rule('Prepare LVSDB') 

847 lvsdb = self.create_lvsdb(args) 

848 

849 pex_context = KLayoutExtractionContext.prepare_extraction(top_cell=args.effective_cell_name, 

850 lvsdb=lvsdb, 

851 tech=tech_info, 

852 blackbox_devices=args.blackbox_devices) 

853 rule('Non-empty layers in LVS database') 

854 for gds_pair, layer_info in pex_context.extracted_layers.items(): 

855 names = [l.lvs_layer_name for l in layer_info.source_layers] 

856 info(f"{gds_pair} -> ({' '.join(names)})") 

857 

858 gds_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_l2n_extracted.oas") 

859 pex_context.annotated_layout.write(gds_path) 

860 

861 gds_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_l2n_internal.oas") 

862 pex_context.lvsdb.internal_layout().write(gds_path) 

863 

864 def dump_layers(cell: str, 

865 layers: List[KLayoutExtractedLayerInfo], 

866 layout_dump_path: str): 

867 layout = kdb.Layout() 

868 layout.dbu = lvsdb.internal_layout().dbu 

869 

870 top_cell = layout.create_cell(cell) 

871 for ulyr in layers: 

872 li = kdb.LayerInfo(*ulyr.gds_pair) 

873 li.name = ulyr.lvs_layer_name 

874 layer = layout.insert_layer(li) 

875 layout.insert(top_cell.cell_index(), layer, ulyr.region.dup()) 

876 

877 layout.write(layout_dump_path) 

878 

879 if len(pex_context.unnamed_layers) >= 1: 

880 layout_dump_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_unnamed_LVS_layers.gds.gz") 

881 dump_layers(cell=args.effective_cell_name, 

882 layers=pex_context.unnamed_layers, 

883 layout_dump_path=layout_dump_path) 

884 

885 if len(pex_context.extracted_layers) >= 1: 

886 layout_dump_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_nonempty_LVS_layers.gds.gz") 

887 nonempty_layers = [l \ 

888 for layers in pex_context.extracted_layers.values() \ 

889 for l in layers.source_layers] 

890 dump_layers(cell=args.effective_cell_name, 

891 layers=nonempty_layers, 

892 layout_dump_path=layout_dump_path) 

893 else: 

894 error("No extracted layers found") 

895 sys.exit(1) 

896 

897 if args.run_fastcap or args.run_fastercap: 

898 lst_file = self.build_fastercap_input(args=args, 

899 pex_context=pex_context, 

900 tech_info=tech_info) 

901 if args.run_fastercap: 

902 self.run_fastercap_extraction(args=args, 

903 pex_context=pex_context, 

904 lst_file=lst_file) 

905 if args.run_fastcap: 

906 self.run_fastcap_extraction(args=args, 

907 pex_context=pex_context, 

908 lst_file=lst_file) 

909 

910 if args.run_2_5D: 

911 rule("kpex/2.5D PEX Engine") 

912 report_path = os.path.join(args.output_dir_path, f"{args.effective_cell_name}_k25d_pex_report.rdb.gz") 

913 netlist_csv_path = os.path.abspath(os.path.join(args.output_dir_path, f"{args.effective_cell_name}_k25d_pex_netlist.csv")) 

914 

915 self._rcx25_extraction_results = self.run_kpex_2_5d_engine( # NOTE: store for test case 

916 args=args, 

917 pex_context=pex_context, 

918 tech_info=tech_info, 

919 report_path=report_path, 

920 netlist_csv_path=netlist_csv_path 

921 ) 

922 

923 self._rcx25_extracted_csv_path = netlist_csv_path 

924 

925 @property 

926 def rcx25_extraction_results(self) -> ExtractionResults: 

927 if not hasattr(self, '_rcx25_extraction_results'): 

928 raise Exception('rcx25_extraction_results is not initialized, was run_kpex_2_5d_engine called?') 

929 return self._rcx25_extraction_results 

930 

931 @property 

932 def rcx25_extracted_csv_path(self) -> str: 

933 if not hasattr(self, '_rcx25_extracted_csv_path'): 

934 raise Exception('rcx25_extracted_csv_path is not initialized, was run_kpex_2_5d_engine called?') 

935 return self._rcx25_extracted_csv_path 

936 

937 @property 

938 def fastercap_extracted_csv_path(self) -> str: 

939 if not hasattr(self, '_fastercap_extracted_csv_path'): 

940 raise Exception('fastercap_extracted_csv_path is not initialized, was run_fastercap_extraction called?') 

941 return self._fastercap_extracted_csv_path 

942 

943 

944if __name__ == "__main__": 

945 cli = KpexCLI() 

946 cli.main(sys.argv)