Skip to content

Commit fc7d656

Browse files
ShiboSoftwareDevYour Name
andauthored
feat: Add support for simulation_current_source (#31)
Co-authored-by: Your Name <you@example.com>
1 parent 03a4d68 commit fc7d656

File tree

4 files changed

+802
-1
lines changed

4 files changed

+802
-1
lines changed

lib/circuitJsonToSpice.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SpiceComponent } from "./spice-classes/SpiceComponent"
33
import { ResistorCommand } from "./spice-commands/ResistorCommand"
44
import { CapacitorCommand } from "./spice-commands/CapacitorCommand"
55
import { VoltageSourceCommand } from "./spice-commands/VoltageSourceCommand"
6+
import { CurrentSourceCommand } from "./spice-commands/CurrentSourceCommand"
67
import { BJTCommand } from "./spice-commands/BJTCommand"
78
import { DiodeCommand } from "./spice-commands/DiodeCommand"
89
import { InductorCommand } from "./spice-commands/InductorCommand"
@@ -565,6 +566,94 @@ export function circuitJsonToSpice(
565566
}
566567
}
567568

569+
// Process simulation current sources
570+
for (const simSource of su(circuitJson).simulation_current_source.list()) {
571+
if (simSource.type !== "simulation_current_source") continue
572+
573+
if ((simSource as any).is_dc_source === false) {
574+
// AC/PULSE Source
575+
const positivePortId = (simSource as any).terminal1_source_port_id
576+
const negativePortId = (simSource as any).terminal2_source_port_id
577+
578+
if (positivePortId && negativePortId) {
579+
const positiveNode = nodeMap.get(positivePortId) || "0"
580+
const negativeNode = nodeMap.get(negativePortId) || "0"
581+
582+
let value = ""
583+
const wave_shape = (simSource as any).wave_shape
584+
if (wave_shape === "sinewave") {
585+
const i_offset = 0 // not provided
586+
const i_peak = ((simSource as any).peak_to_peak_current ?? 0) / 2
587+
const freq = (simSource as any).frequency ?? 0
588+
const delay = 0
589+
const damping_factor = 0
590+
const phase = (simSource as any).phase ?? 0
591+
if (freq > 0) {
592+
value = `SIN(${i_offset} ${i_peak} ${freq} ${delay} ${damping_factor} ${phase})`
593+
} else {
594+
value = `DC ${i_peak}`
595+
}
596+
} else if (wave_shape === "square") {
597+
const i_initial = 0
598+
const i_pulsed = (simSource as any).peak_to_peak_current ?? 0
599+
const freq = (simSource as any).frequency ?? 0
600+
const period_from_freq = freq === 0 ? Infinity : 1 / freq
601+
const period = (simSource as any).period ?? period_from_freq
602+
const duty_cycle = (simSource as any).duty_cycle ?? 0.5
603+
const pulse_width = period * duty_cycle
604+
const delay = 0
605+
const rise_time = "1n"
606+
const fall_time = "1n"
607+
value = `PULSE(${i_initial} ${i_pulsed} ${delay} ${rise_time} ${fall_time} ${pulse_width} ${period})`
608+
}
609+
610+
if (value) {
611+
const currentSourceCmd = new CurrentSourceCommand({
612+
name: simSource.simulation_current_source_id,
613+
positiveNode,
614+
negativeNode,
615+
value,
616+
})
617+
618+
const spiceComponent = new SpiceComponent(
619+
simSource.simulation_current_source_id,
620+
currentSourceCmd,
621+
[positiveNode, negativeNode],
622+
)
623+
netlist.addComponent(spiceComponent)
624+
}
625+
}
626+
} else {
627+
// DC Source
628+
const positivePortId = (simSource as any).positive_source_port_id
629+
const negativePortId = (simSource as any).negative_source_port_id
630+
631+
if (
632+
positivePortId &&
633+
negativePortId &&
634+
"current" in simSource &&
635+
(simSource as any).current !== undefined
636+
) {
637+
const positiveNode = nodeMap.get(positivePortId) || "0"
638+
const negativeNode = nodeMap.get(negativePortId) || "0"
639+
640+
const currentSourceCmd = new CurrentSourceCommand({
641+
name: simSource.simulation_current_source_id,
642+
positiveNode,
643+
negativeNode,
644+
value: `DC ${(simSource as any).current}`,
645+
})
646+
647+
const spiceComponent = new SpiceComponent(
648+
simSource.simulation_current_source_id,
649+
currentSourceCmd,
650+
[positiveNode, negativeNode],
651+
)
652+
netlist.addComponent(spiceComponent)
653+
}
654+
}
655+
}
656+
568657
const simExperiment = circuitJson.find(
569658
(elm) => elm.type === "simulation_experiment",
570659
)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@tscircuit/circuit-json-util": "^0.0.72",
1717
"@types/bun": "^1.2.15",
1818
"bun-match-svg": "^0.0.13",
19-
"circuit-json": "^0.0.322",
19+
"circuit-json": "^0.0.350",
2020
"eecircuit-engine": "^1.5.2",
2121
"tscircuit": "^0.0.936",
2222
"tsup": "^8.4.0"

0 commit comments

Comments
 (0)