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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024-2025, The OpenROAD Authors

import os
import re
import argparse
import math
import xlsxwriter
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np


def OpenFile(file_path, rw="r"):
    if rw == "r":
        if os.path.exists(file_path):
            # print(file_path)
            return open(file_path, rw)
        else:
            print(f"The file {file_path} does not exist.")
            exit()
    if rw == "w":
        return open(file_path, rw)


def getWSformat(sp):
    dist = "{:.4f}".format(sp)
    return dist


def getFF(cap):
    cc = "{:9.6f}".format(cap * 1e15)
    return cc


def getWS(word, SW, ii):
    # parsing W0.14_W0.14
    s1 = float(word.split("_")[ii].split(SW)[1])
    return s1


# g2_wire_M5_w0  -7.3632e-17 2.82679e-16 -1.01845e-16 -5.93445e-17 -5.83917e-17 -5.96175e-17 -1.01798e-16
# g3_wire_M4_w1  -1.41823e-16 -2.30241e-17 5.5345e-16 -2.76143e-16 -3.19856e-18 1.15872e-18 -2.70547e-20


def parseMatrixRow(row, met, matrixRowIndex, wireCnt, diagWireCnt, dbg=0):
    # process one row of cap matrix at a time
    # returns wire index last number of the first column
    # return diagonal cap value for the wire index
    # return total coupling left and right of diagonal
    # return 2 caps left and right after immediate left and right
    # return diag cap as the before last column

    capValues = row.split()
    ii = matrixRowIndex

    # g3_wire_M4_w1
    targetMetWord = "_M" + str(met) + "_"

    if dbg > 0:
        print("-------------------------------------------------")
        print(row)
    if dbg > 1:
        print("targetMetWord= ", targetMetWord)

    if targetMetWord not in capValues[0]:
        return []

    wireIndex = int(capValues[0].split("w")[2])
    if wireIndex == 0:
        return []

    preWireCnt = matrixRowIndex - wireIndex
    diagWireIndex = preWireCnt + wireCnt
    if dbg > 1:
        print("wire= ", wireIndex)
        print("preWireCnt= ", preWireCnt)
        print("diagWireIndex= ", diagWireIndex)

    # diagonal
    tot = float(capValues[ii])
    cc = 0.0
    if wireIndex > 1:
        cc += float(capValues[ii - 1])
    if wireIndex + 1 <= wireCnt:
        cc += float(capValues[ii + 1])

    cc2 = 0.0
    if wireIndex > 2:
        cc2 += float(capValues[ii - 2])
    if wireIndex + 2 <= wireCnt:
        cc2 += float(capValues[ii + 2])

    diagCC = []
    for i in range(1, diagWireCnt + 1):
        jj = i + diagWireIndex
        diagCC.append(-float(capValues[jj]))

    return [wireIndex, tot, -cc, -cc2, diagCC]


def getWireCnt(capMatrix, mets, dbg=0):
    # returns wireCnt of target met=met and cnt of diagonal wires
    # skipping the context conductors
    # traverses all the rows of the cap matrix looking for patterns like _M2_
    diag = mets[3]
    met = mets[0]
    metUnder = mets[1]
    metOver = mets[2]

    # _M3_
    targetMetWord = "_M" + str(met) + "_"
    targetDiagMetWord = ""
    if diag:
        targetDiagMetWord = "_M" + str(metOver) + "_"

    wireCnt = 0
    diagWireCnt = 0
    for i in range(len(capMatrix)):
        name = capMatrix[i].split()[0]
        if dbg > 0:
            print(name)

        if targetMetWord in name:
            wireCnt = wireCnt + 1
        if diag and targetDiagMetWord in name:
            diagWireCnt = diagWireCnt + 1
            continue
    if dbg > 0:
        print(targetMetWord, targetDiagMetWord, [wireCnt, diagWireCnt])

    return [wireCnt, diagWireCnt]


def getPatternName(word):
    # returns last 4 subwords of the Input Pattern Name
    n = len(word)
    name = ""
    for i in range(5, 1, -1):
        name += word[n - i]
        name += "/"
    return name


def getMets(word):
    # returns metal indices from the pattern
    diag = 0
    met = 0
    metOver = 0
    metUnder = 0

    over = word.split("o")
    # print(over)
    if len(over) > 1:
        met = over[0].split("M")[1]
        mu = over[1].split("u")
        metUnder = mu[0].split("M")[1]
        if len(mu) > 1:
            metOver = mu[1].split("M")[1]
    else:
        under = word.split("u")
        # Diag M1duM3
        if "d" in under[0]:
            diag = 1
            met = under[0].split("d")[0].split("M")[1]
        else:
            met = under[0].split("M")[1]

        metOver = under[1].split("M")[1]

    return [met, metUnder, metOver, diag]


def readFasterCapOutPutLog(in_file, out_file, warnFP, emptyFP, statsFP, dbg):
    # reads the log file and parses the cap values for all conductors from the last valid iteration

    if dbg > 1:
        print(in_file)

    iterCnt = 0
    dimension = 0
    capMatrixCompleted = 0
    lastIterationIndex = 0
    iteration = []
    rows = []
    patternName = ""
    spacings = []
    widths = []
    mets = []
    patternType = ""

    mbytes = 0
    secs = 0

    line_cnt = 0
    f = OpenFile(in_file)
    for line in f:
        line_cnt = line_cnt + 1
        # parsing OverUnder5/M4oM3uM5/W0.14_W0.14/S0.14_S0.14/wires.lst
        if "Input file:" in line:
            file_line = line.split()
            full_pattern_file = file_line[2]
            word = file_line[2].split("/")
            n = len(word)
            if dbg > 0:
                print("Parsing Pattern: ", file_line[2])

            # print(word)
            patternName = getPatternName(word)
            spacings = [getWS(word[n - 2], "S", 0), getWS(word[n - 2], "S", 1)]
            widths = [getWS(word[n - 3], "W", 0), getWS(word[n - 3], "W", 1)]
            len_in_widths = int(word[n - 2].split("L")[1])
            mets = getMets(word[n - 4])
            patternType = word[n - 5]
            if dbg > 1:
                print(patternName, spacings, widths, mets, len_in_widths)
            continue

        if "Iteration number" in line:
            iterCnt = iterCnt + 1
            rows = []
            continue

        if "Dimension" in line:
            capMatrixCompleted = 0
            dimension = int(line.split()[1])
            continue

        # Total allocated memory: 1439880 kilobytes
        # Total time: 375.292633s (0 days, 0 hours, 6 mins, 15 s)
        if "Weighted Frobenius" in line:
            capMatrixCompleted = 1

        if "Total allocated memory" in line:
            mbytes = int(line.split()[3]) / 1000

        if "Total time" in line:
            secs = int(line.split()[2].split("s")[0].split(".")[0])

        if dimension > 0:
            capMatrixCompleted = 0
            rows.append(line)
            dimension = dimension - 1
            if dimension == 0:
                iteration.append(rows)
            continue

    warning = ""
    if line_cnt == 0:
        warning = "Warning: Empty File " + in_file
        emptyFP.write(in_file + "\n")
        return [0, line_cnt, warning]

    if len(iteration) == 0:
        # warning= 'Warning: No Cap Matrix in File ' + in_file
        warning = "Warning: No Cap Matrix in File " + full_pattern_file
        warnFP.write(warning + "\n")
        return [0, line_cnt, warning]

    if dbg > 2:
        print("last iteration:")
        print(iteration)

    warning1 = ""
    warning = ""
    if capMatrixCompleted > 0:
        lastIterationIndex = len(iteration) - 1
    else:
        if dbg > 0:
            print("Warning -- iterations= ", iterCnt, len(iteration))
            print(iteration[lastIterationIndex])

        # warning1='Incomplete Last Iteration ' + in_file
        warning1 = "Incomplete Last Iteration " + full_pattern_file
        warnFP.write(warning1 + "\n")

        warning = "Incomplete"
        lastIterationIndex = lastIterationIndex - 2
        if lastIterationIndex < 0 or len(iteration[lastIterationIndex]) < dimension:
            if dbg > 0:
                print(
                    "Warning: Before last iteration: ",
                    lastIterationIndex,
                    " Incomplete",
                )
            # warning1='Incomplete Before Last Iteration ' + in_file
            warning1 = "Incomplete Before Last Iteration " + full_pattern_file
            warnFP.write(warning1 + "\n")
            return [0, line_cnt, warning1]

    wireCnt = getWireCnt(iteration[0], mets, dbg)
    met = mets[0]

    # parse last complete capacitance matrix
    initCapRowIndex = 0
    ii = 0
    for row in iteration[lastIterationIndex]:
        ii = ii + 1
        # print(row)
        caps = parseMatrixRow(row, met, ii, wireCnt[0], wireCnt[1])
        if len(caps) == 0:
            continue
        if dbg > 0:
            print("Caps = ", caps)

        diagUnder = " Under "
        diagCaps = ""
        if mets[3] > 0:
            diagUnder = " DiagUnder "
            dcc = getFF(caps[4][0])
            diagCaps = (
                " DiagDist "
                + str(spacings[1])
                + " DiagWidth "
                + str(widths[1])
                + " DiagCC "
                + dcc
            )

        dist = getWSformat(spacings[0])
        width1 = getWSformat(widths[0])
        out_line = (
            "Metal "
            + str(mets[0])
            + " Over "
            + str(mets[1])
            + diagUnder
            + str(mets[2])
            + "  Dist "
            + dist
            + " Width "
            + width1
        )
        cc = getFF(caps[2])
        fr = getFF(caps[1] - caps[2])
        tc = getFF(caps[1])
        cc2 = getFF(caps[3])
        full_net_name = patternName + "wire_" + str(caps[0])

        # print(out_line, "CC", cc, "FR", fr , 'TC', tc, 'CC2', cc2, ' ', diagCaps, full_net_name, run_stats, warning)
        out_file.write(
            out_line
            + "  LEN "
            + str(len_in_widths)
            + "  CC "
            + cc
            + "  FR "
            + fr
            + "  TC "
            + tc
            + "  CC2 "
            + cc2
            + "  "
            + diagCaps
            + " "
            + full_net_name
            + " "
            + warning
        )
        out_file.write("\n")
    run_stats = (
        str(mbytes)
        + " MB "
        + str(secs)
        + " secs "
        + str(len(iteration))
        + " iterations"
    )
    statsFP.write(run_stats + " " + full_pattern_file + "\n")
    return [1, line_cnt, warning1]


def main():
    arg_parser = argparse.ArgumentParser(description="Parser of a FasterCap file")
    arg_parser.add_argument(
        "-in_list_file",
        type=str,
        default="",
        help="Input Filename with list of input file paths, default= ",
    )
    arg_parser.add_argument(
        "-in_file",
        type=str,
        default="wires.log",
        help="Input Filename, default=wires.log",
    )
    arg_parser.add_argument(
        "-out_file",
        type=str,
        default="pattern.caps",
        help="Output Filename, default=pattern.caps",
    )
    arg_parser.add_argument(
        "-wire", type=int, default=3, help="target wire number, default=3"
    )
    arg_parser.add_argument("-dbg", type=int, default=0, help="debug level, default=0")

    args = arg_parser.parse_args()

    outFP = OpenFile(args.out_file, "w")
    warnFP = OpenFile("warnings", "w")
    emptyFP = OpenFile("empty_files", "w")
    statsFP = OpenFile("run_stats", "w")
    file_cnt = 0
    incompleteCnt = 0
    empty_file_cnt = 0
    successCnt = 0

    dbg = args.dbg
    # Single file
    if len(args.in_list_file) == 0:
        readFasterCapOutPutLog(args.in_file, outFP, warnFP, emptyFP, statsFP, dbg)
        file_cnt += 1

        if retCode[0] == 1:
            successCnt += 1
            if "Incomplete" in retCode[2]:
                incompleteCnt += 1

        if retCode[0] == 0 and "Empty" in retCode[2]:
            empty_file_cnt += 1
        if retCode[0] == 0 and "Incomplete" in retCode[2]:
            incompleteCnt += 1
        exit()

    # list of files
    f = OpenFile(args.in_list_file)
    for file_line in f:
        retCode = readFasterCapOutPutLog(
            file_line.split()[0], outFP, warnFP, emptyFP, statsFP, dbg
        )
        file_cnt += 1

        if retCode[0] == 1:
            successCnt += 1
            if "Incomplete" in retCode[2]:
                incompleteCnt += 1

        if retCode[0] == 0 and "Empty" in retCode[2]:
            empty_file_cnt += 1
        if retCode[0] == 0 and "Incomplete" in retCode[2]:
            incompleteCnt += 1

    outFP.close()
    warnFP.close()
    emptyFP.close()
    statsFP.close()

    print(file_cnt, " Files Parsed")
    print(empty_file_cnt, " Files are Empty -- look at file:empty_files")
    print(incompleteCnt, " Files were incomplete -- look at file: warnings")


if __name__ == "__main__":
    main()

# Dimension 7 x 7
# g1_wire_M3_w0  6.9136e-16 -5.40629e-17 -1.68603e-16 -1.02291e-16 -9.64771e-17 -1.05795e-16 -1.6943e-16
# g2_wire_M5_w0  -7.3632e-17 2.82679e-16 -1.01845e-16 -5.93445e-17 -5.83917e-17 -5.96175e-17 -1.01798e-16
# g3_wire_M4_w1  -1.41823e-16 -2.30241e-17 5.5345e-16 -2.76143e-16 -3.19856e-18 1.15872e-18 -2.70547e-20
# g4_wire_M4_w2  -9.1805e-17 -1.36862e-17 -2.73845e-16 7.20986e-16 -2.7526e-16 -4.54052e-18 1.38176e-18
# g5_wire_M4_w3  -9.05258e-17 -1.32927e-17 -3.24811e-18 -2.75761e-16 7.2318e-16 -2.80613e-16 -3.72233e-18
# g6_wire_M4_w4  -9.13211e-17 -1.37111e-17 1.56117e-18 -4.38767e-18 -2.81118e-16 7.2663e-16 -2.7262e-16
# g7_wire_M4_w5  -1.4171e-16 -2.30185e-17 -3.0492e-20 8.26274e-19 -5.05703e-18 -2.73595e-16 5.535e-16

# Solve statistics:
# Total allocated memory: 4950569 kilobytes
# Total time: 2578.236084s (0 days, 0 hours, 42 mins, 58 s)