spectre/analysis/
dc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
//! Spectre DC sweeps and operating point analyses.

use crate::{InstanceTail, SimSignal, Spectre};
use arcstr::ArcStr;
use scir::{NamedSliceOne, SliceOnePath};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use substrate::{
    schematic::conv::ConvertedNodePath,
    simulation::{
        data::{Save, SaveOutput},
        Analysis, SimulationContext, Simulator, SupportedBy,
    },
    types::schematic::{NestedNode, NestedTerminal, RawNestedNode},
};

/// A DC operating point analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DcOp;

/// The result of a [`DcOp`] analysis.
#[derive(Debug, Clone)]
pub struct OpOutput {
    /// A map from signal name to value.
    pub raw_values: HashMap<ArcStr, f64>,
    /// A map from a save ID to a raw value identifier.
    pub(crate) saved_values: HashMap<u64, ArcStr>,
}

/// An identifier for a saved DC voltage.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct VoltageSaveKey(pub(crate) u64);

/// An identifier for a saved DC current.
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CurrentSaveKey(pub(crate) Vec<u64>);

impl Analysis for DcOp {
    type Output = OpOutput;
}

impl SupportedBy<Spectre> for DcOp {
    fn into_input(self, inputs: &mut Vec<<Spectre as Simulator>::Input>) {
        inputs.push(self.into());
    }
    fn from_output(
        outputs: &mut impl Iterator<Item = <Spectre as Simulator>::Output>,
    ) -> <Self as Analysis>::Output {
        let item = outputs.next().unwrap();
        item.try_into().unwrap()
    }
}

impl Save<Spectre, DcOp> for SaveOutput {
    type SaveKey = ();
    type Saved = OpOutput;

    fn save(
        &self,
        _ctx: &SimulationContext<Spectre>,
        _opts: &mut <Spectre as Simulator>::Options,
    ) -> <Self as Save<Spectre, DcOp>>::SaveKey {
    }

    fn from_saved(
        output: &<DcOp as Analysis>::Output,
        _key: &<Self as Save<Spectre, DcOp>>::SaveKey,
    ) -> <Self as Save<Spectre, DcOp>>::Saved {
        output.clone()
    }
}

impl Save<Spectre, DcOp> for NestedNode {
    type SaveKey = VoltageSaveKey;
    type Saved = f64;

    fn save(
        &self,
        ctx: &SimulationContext<Spectre>,
        opts: &mut <Spectre as Simulator>::Options,
    ) -> <Self as Save<Spectre, DcOp>>::SaveKey {
        opts.save_dc_voltage(SimSignal::ScirVoltage(
            match ctx.lib.convert_node_path(&self.path()).unwrap() {
                ConvertedNodePath::Cell(path) => path.clone(),
                ConvertedNodePath::Primitive {
                    instances, port, ..
                } => SliceOnePath::new(instances.clone(), NamedSliceOne::new(port.clone())),
            },
        ))
    }

    fn from_saved(
        output: &<DcOp as Analysis>::Output,
        key: &<Self as Save<Spectre, DcOp>>::SaveKey,
    ) -> <Self as Save<Spectre, DcOp>>::Saved {
        *output
            .raw_values
            .get(output.saved_values.get(&key.0).unwrap())
            .unwrap()
    }
}

impl Save<Spectre, DcOp> for RawNestedNode {
    type SaveKey = VoltageSaveKey;
    type Saved = f64;

    fn save(
        &self,
        ctx: &SimulationContext<Spectre>,
        opts: &mut <Spectre as Simulator>::Options,
    ) -> <Self as Save<Spectre, DcOp>>::SaveKey {
        let itail = InstanceTail {
            instance: ctx.lib.convert_instance_path(self.instances()).unwrap(),
            tail: self.tail().clone(),
        };
        opts.save_dc_voltage(itail)
    }

    fn from_saved(
        output: &<DcOp as Analysis>::Output,
        key: &<Self as Save<Spectre, DcOp>>::SaveKey,
    ) -> <Self as Save<Spectre, DcOp>>::Saved {
        *output
            .raw_values
            .get(output.saved_values.get(&key.0).unwrap())
            .unwrap()
    }
}

/// Data saved from a nested terminal in an [`DcOp`] simulation.
pub struct NestedTerminalOutput {
    /// The voltage at the terminal.
    pub v: f64,
    /// The current flowing through the terminal.
    pub i: f64,
}

impl Save<Spectre, DcOp> for NestedTerminal {
    type SaveKey = (VoltageSaveKey, CurrentSaveKey);
    type Saved = NestedTerminalOutput;

    fn save(
        &self,
        ctx: &SimulationContext<Spectre>,
        opts: &mut <Spectre as Simulator>::Options,
    ) -> <Self as Save<Spectre, DcOp>>::SaveKey {
        (
            <NestedNode as Save<Spectre, DcOp>>::save(self, ctx, opts),
            CurrentSaveKey(
                ctx.lib
                    .convert_terminal_path(&self.path())
                    .unwrap()
                    .into_iter()
                    .flat_map(|path| {
                        opts.save_tran_current(SimSignal::ScirCurrent(match path {
                            ConvertedNodePath::Cell(path) => path.clone(),
                            ConvertedNodePath::Primitive {
                                instances, port, ..
                            } => SliceOnePath::new(
                                instances.clone(),
                                NamedSliceOne::new(port.clone()),
                            ),
                        }))
                        .0
                    })
                    .collect(),
            ),
        )
    }

    fn from_saved(
        output: &<DcOp as Analysis>::Output,
        key: &<Self as Save<Spectre, DcOp>>::SaveKey,
    ) -> <Self as Save<Spectre, DcOp>>::Saved {
        let v = *output
            .raw_values
            .get(output.saved_values.get(&key.0 .0).unwrap())
            .unwrap();
        let i = key
            .1
             .0
            .iter()
            .map(|key| {
                output
                    .raw_values
                    .get(output.saved_values.get(key).unwrap())
                    .unwrap()
            })
            .sum();

        NestedTerminalOutput { v, i }
    }
}