Exemplo n.º 1
0
 public override void Evaluate <T>(
     ICircuitEvaluator <T> evaluator,
     EvaluationState <T> evaluationState,
     CircuitContext circuitContext)
 {
     evaluationState.SetGateEvaluationValue(this, evaluationState.GetInput(Context.TypeUniqueId));
 }
        public override SpiceSharp.Components.Component Generate(string componentIdentifier, string originalName, string type, ParameterCollection parameters, ICircuitContext context)
        {
            SubCircuit     subCircuitDefinition = FindSubcircuitDefinition(parameters, context);
            CircuitContext subCircuitContext    = CreateSubcircuitContext(componentIdentifier, originalName, subCircuitDefinition, parameters, context);

            var ifPreprocessor = new IfPreprocessor();

            ifPreprocessor.CaseSettings = subCircuitContext.CaseSensitivity;
            ifPreprocessor.Validation   = new SpiceParserValidationResult()
            {
                Reading = context.Result.Validation
            };
            ifPreprocessor.EvaluationContext = subCircuitContext.Evaluator.GetEvaluationContext();
            subCircuitDefinition.Statements  = ifPreprocessor.Process(subCircuitDefinition.Statements);

            ReadParamControl(subCircuitDefinition, subCircuitContext);
            ReadFuncControl(subCircuitDefinition, subCircuitContext);
            ReadSubcircuits(subCircuitDefinition, subCircuitContext);
            CreateSubcircuitModels(subCircuitDefinition, subCircuitContext); // TODO: Share models someday between instances of subcircuits
            CreateSubcircuitComponents(subCircuitDefinition, subCircuitContext);

            context.Children.Add(subCircuitContext);

            // null is intentional
            return(null);
        }
 private void UpdateContext(CircuitContext context)
 {
     if (_context == null)
     {
         _context = context;
     }
 }
        /// <inheritdoc />
        public override void Register(CircuitContext context, IDiagnosticHandler diagnostics)
        {
            if (Fix)
            {
                RegisterFixed(context, diagnostics);
                return;
            }

            var    map       = context.Nodes.Shorts;
            var    ckt       = context.Circuit;
            string x         = map[X];
            string ox        = map[_origin.X];
            string y         = map[Y];
            string oy        = map[_origin.Y];
            var    direction = _origin is ITransformingDrawable tfd?tfd.TransformNormal(Direction) : Direction;

            direction = direction.Order(ref ox, ref x, ref oy, ref y);

            // If we only work along one axis, we can simplify the schematic
            if (x == ox)
            {
                MinimumConstraint.AddMinimum(ckt, Y, oy, y, MinimumOffset);
                return;
            }
            if (y == oy)
            {
                MinimumConstraint.AddMinimum(ckt, X, ox, x, MinimumOffset);
                return;
            }

            // General case, both X and Y are different
            AddControlledMinimum(ckt, $"{Owner.Name}[{Name}]", ox, x, oy, y, direction);
            MinimumConstraint.AddMinimum(ckt, $"{Owner.Name}[{Name}].min.x", ox, x, direction.X * MinimumOffset);
            MinimumConstraint.AddMinimum(ckt, $"{Owner.Name}[{Name}].min.y", oy, y, direction.Y * MinimumOffset);
        }
        public void testStorage()
        {
            using (var db = new CircuitContext())
            {
                Circuit circuit = new Circuit();
                Connection con0 = new Connection(); // GND
                Connection con1 = new Connection();
                Connection con2 = new Connection();

                Component battery = new Component(con0, con1, 0, 12);
                Component resistor1 = new Component(con0, con2, 2, 0);
                Component resistor2 = new Component(con1, con2, 2, 0);
                Component resistor3 = new Component(con1, con2, 2, 0);


                circuit.AddComponent(battery);
                circuit.AddComponent(resistor1);
                circuit.AddComponent(resistor2);
                circuit.AddComponent(resistor3);

                db.Circuits.Add(circuit);



            }
        }
        public int EvaluateNotGate(int value, GateContext gateContext, CircuitContext circuitContext)
        {
            int layerIndex = value;

            GetLayerStatisticsBuilder(layerIndex).AddLinearGate();
            UpdateContext(circuitContext);
            return(layerIndex);
        }
Exemplo n.º 7
0
        public override void Evaluate <T>(
            ICircuitEvaluator <T> evaluator,
            EvaluationState <T> evaluationState,
            CircuitContext circuitContext)
        {
            T value = evaluator.EvaluateNotGate(
                evaluationState.GetGateEvaluationValue(_inputGate),
                Context,
                circuitContext
                );

            evaluationState.SetGateEvaluationValue(this, value);
        }
Exemplo n.º 8
0
        /// <inheritdoc />
        public void Update(IBiasingSimulationState state, CircuitContext context, IDiagnosticHandler diagnostics)
        {
            double x = 0, y = 0;

            if (state.TryGetValue(context.Nodes.Shorts[X], out var xValue))
            {
                x = xValue.Value;
            }
            if (state.TryGetValue(context.Nodes.Shorts[Y], out var yValue))
            {
                y = yValue.Value;
            }
            Location = new(x, y);
        }
Exemplo n.º 9
0
        public T[] EvaluateAndGateBatch(GateEvaluationInput <T>[] evaluationInputs, CircuitContext circuitContext)
        {
            T[] outputValues = new T[evaluationInputs.Length];
            for (int i = 0; i < evaluationInputs.Length; ++i)
            {
                GateEvaluationInput <T> evaluationInput = evaluationInputs[i];
                outputValues[i] = _innerEvaluator.EvaluateAndGate(
                    evaluationInput.LeftValue,
                    evaluationInput.RightValue,
                    evaluationInput.Context,
                    circuitContext
                    );
            }

            return(outputValues);
        }
Exemplo n.º 10
0
        private void RegisterFixed(CircuitContext context, IDiagnosticHandler diagnostics)
        {
            // No need to go through all these difficult things, let's just apply directly
            var    map       = context.Nodes.Shorts;
            string x         = map[X];
            string ox        = map[_origin.X];
            string y         = map[Y];
            string oy        = map[_origin.Y];
            var    direction = _origin is ITransformingDrawable tfd?tfd.TransformNormal(Direction) : Direction;

            direction = direction.Order(ref ox, ref x, ref oy, ref y);
            if (x != ox)
            {
                OffsetConstraint.AddOffset(context.Circuit, X, ox, x, direction.X * MinimumOffset);
            }
            if (y != oy)
            {
                OffsetConstraint.AddOffset(context.Circuit, Y, oy, y, direction.Y * MinimumOffset);
            }
        }
Exemplo n.º 11
0
        /// <inheritdoc />
        public override void Register(CircuitContext context, IDiagnosticHandler diagnostics)
        {
            Vector2 offset = _origin is ITransformingDrawable tfd?tfd.TransformOffset(Offset) : Offset;

            var    map = context.Nodes.Shorts;
            string x   = map[X];
            string ox  = map[_origin.X];
            string y   = map[Y];
            string oy  = map[_origin.Y];

            if (x != ox)
            {
                string i = $"{X}.i";
                context.Circuit.Add(new Resistor($"R{X}", i, x, 1e-3));
                context.Circuit.Add(new VoltageSource($"V{X}", i, ox, offset.X));
            }
            if (y != oy)
            {
                string i = $"{Y}.i";
                context.Circuit.Add(new Resistor($"R{Y}", i, y, 1e-3));
                context.Circuit.Add(new VoltageSource($"V{Y}", y, oy, offset.Y));
            }
        }
Exemplo n.º 12
0
 /// <inheritdoc />
 public override void Register(CircuitContext context, IDiagnosticHandler diagnostics)
 {
     // Left to whoever owns this pin...
 }
Exemplo n.º 13
0
        public async Task <Bit> EvaluateXorGate(Task <Bit> leftValue, Task <Bit> rightValue, GateContext gateContext, CircuitContext circuitContext)
        {
            Bit leftShare = await leftValue.ConfigureAwait(false);

            Bit rightShare = await rightValue.ConfigureAwait(false);

            return(leftShare ^ rightShare);
        }
Exemplo n.º 14
0
        public async Task <Bit> EvaluateNotGate(Task <Bit> value, GateContext gateContext, CircuitContext circuitContext)
        {
            Bit share = await value.ConfigureAwait(false);

            if (_session.LocalParty.IsFirstParty())
            {
                return(~share);
            }

            return(share);
        }
Exemplo n.º 15
0
 public Task <Bit>[] EvaluateAndGateBatch(GateEvaluationInput <Task <Bit> >[] evaluationInputs, CircuitContext circuitContext)
 {
     return(EvaluateAndGateBatchAsync(evaluationInputs, circuitContext).ToSubTasks(bits => bits.ToArray(), evaluationInputs.Length));
 }
Exemplo n.º 16
0
        private async Task <BitArray> EvaluateAndGateBatchAsync(GateEvaluationInput <Task <Bit> >[] evaluationInputs, CircuitContext circuitContext)
        {
            int numberOfInvocations = evaluationInputs.Length;

            BitArray leftShares  = new BitArray(numberOfInvocations);
            BitArray rightShares = new BitArray(numberOfInvocations);

            for (int i = 0; i < numberOfInvocations; ++i)
            {
                leftShares[i]  = await evaluationInputs[i].LeftValue.ConfigureAwait(false);
                rightShares[i] = await evaluationInputs[i].RightValue.ConfigureAwait(false);
            }

            BitArray multiplicativeShares = await _multiplicativeSharing.ComputeMultiplicativeSharesAsync(_session, leftShares, rightShares, numberOfInvocations);

#if DEBUG
            Console.WriteLine(
                "Evaluated AND gates {0} of {1} total.",
                String.Join(", ", evaluationInputs.Select(input => input.Context.TypeUniqueId + 1)),
                circuitContext.NumberOfAndGates
                );
#endif
            return(multiplicativeShares);
        }
Exemplo n.º 17
0
        public ForwardGate EvaluateNotGate(ForwardGate inputGate, GateContext gateContext, CircuitContext circuitContext)
        {
            ForwardGate gate = new ForwardNotGate(gateContext);

            inputGate.AddSuccessor(gate);
            return(gate);
        }
Exemplo n.º 18
0
 public void SendOutputValue <T>(T value, IBatchCircuitEvaluator <T> evaluator, ForwardEvaluationState <T> evaluationState, CircuitContext circuitContext)
 {
     foreach (ForwardGate successor in _successors)
     {
         successor.ReceiveInputValue(value, evaluator, evaluationState, circuitContext);
     }
 }
Exemplo n.º 19
0
 public T EvaluateXorGate(T leftValue, T rightValue, GateContext gateContext, CircuitContext circuitContext)
 {
     return(_innerEvaluator.EvaluateXorGate(leftValue, rightValue, gateContext, circuitContext));
 }
Exemplo n.º 20
0
        protected override void ReceiveInputValues <T>(T leftValue, T rightValue, IBatchCircuitEvaluator <T> evaluator, ForwardEvaluationState <T> evaluationState, CircuitContext circuitContext)
        {
            T outputValue = evaluator.EvaluateXorGate(leftValue, rightValue, _context, circuitContext);

            SendOutputValue(outputValue, evaluator, evaluationState, circuitContext);
        }
Exemplo n.º 21
0
        public int EvaluateXorGate(int leftValue, int rightValue, GateContext gateContext, CircuitContext circuitContext)
        {
            int layerIndex = Math.Max(leftValue, rightValue);

            GetLayerStatisticsBuilder(layerIndex).AddLinearGate();
            UpdateContext(circuitContext);
            return(layerIndex);
        }
Exemplo n.º 22
0
        protected override void ReceiveInputValues <T>(T leftValue, T rightValue, IBatchCircuitEvaluator <T> evaluator, ForwardEvaluationState <T> evaluationState, CircuitContext circuitContext)
        {
            GateEvaluationInput <T> evaluationInput = new GateEvaluationInput <T>(_context, leftValue, rightValue);

            evaluationState.DelayAndGateEvaluation(new GateEvaluation <T>(this, evaluationInput));
        }
Exemplo n.º 23
0
 /// <inheritdoc />
 public abstract void Register(CircuitContext context, IDiagnosticHandler diagnostics);
Exemplo n.º 24
0
        /// <summary>
        /// Translates Netlist object mode to SpiceSharp netlist.
        /// </summary>
        /// <param name="netlist">A object model of the netlist.</param>
        /// <returns>
        /// A new SpiceSharp netlist.
        /// </returns>
        public SpiceModel <SpiceSharp.Circuit, Simulation> Read(SpiceNetlist netlist)
        {
            if (netlist == null)
            {
                throw new System.ArgumentNullException(nameof(netlist));
            }

            // Get result netlist
            var result = new SpiceModel <Circuit, Simulation>(
                new Circuit(StringComparerProvider.Get(Settings.CaseSensitivity.IsEntityNameCaseSensitive)),
                netlist.Title);

            // Get reading context
            var resultService     = new ResultService(result);
            var nodeNameGenerator = new MainCircuitNodeNameGenerator(
                new [] { "0" },
                Settings.CaseSensitivity.IsNodeNameCaseSensitive);
            var            objectNameGenerator = new ObjectNameGenerator(string.Empty);
            INameGenerator nameGenerator       = new NameGenerator(nodeNameGenerator, objectNameGenerator);
            IRandomizer    randomizer          = new Randomizer(
                Settings.CaseSensitivity.IsDistributionNameCaseSensitive,
                seed: Settings.Seed);

            IExpressionParserFactory  expressionParserFactory  = new ExpressionParserFactory(Settings.CaseSensitivity);
            IExpressionFeaturesReader expressionFeaturesReader = new ExpressionFeaturesReader(expressionParserFactory);
            IExpressionValueProvider  expressionValueProvider  = new ExpressionValueProvider(expressionParserFactory);

            EvaluationContext expressionContext = new SpiceEvaluationContext(
                string.Empty,
                Settings.EvaluatorMode,
                Settings.CaseSensitivity,
                randomizer,
                expressionParserFactory,
                expressionFeaturesReader,
                expressionValueProvider,
                nameGenerator,
                resultService);

            var simulationEvaluationContexts = new SimulationEvaluationContexts(expressionContext);
            ISimulationPreparations simulationPreparations = new SimulationPreparations(
                new EntityUpdates(Settings.CaseSensitivity.IsParameterNameCaseSensitive, simulationEvaluationContexts),
                new SimulationsUpdates(simulationEvaluationContexts));

            ICircuitEvaluator      circuitEvaluator = new CircuitEvaluator(simulationEvaluationContexts, expressionContext);
            ISpiceStatementsReader statementsReader = new SpiceStatementsReader(Settings.Mappings.Controls,
                                                                                Settings.Mappings.Models, Settings.Mappings.Components);
            IWaveformReader waveformReader = new WaveformReader(Settings.Mappings.Waveforms);

            ICircuitContext circuitContext = new CircuitContext(
                "Root circuit context",
                null,
                circuitEvaluator,
                simulationPreparations,
                resultService,
                nameGenerator,
                statementsReader,
                waveformReader,
                Settings.CaseSensitivity,
                Settings.Mappings.Exporters,
                Settings.WorkingDirectory,
                null);

            // Set initial seed
            circuitContext.Evaluator.Seed = Settings.Seed;

            // Read statements form input netlist using created context
            circuitContext.Read(netlist.Statements, Settings.Orderer);

            // Set final seed
            result.Seed = circuitContext.Evaluator.Seed;

            return(result);
        }
Exemplo n.º 25
0
        public override void ReceiveInputValue <T>(T value, IBatchCircuitEvaluator <T> evaluator, ForwardEvaluationState <T> evaluationState, CircuitContext circuitContext)
        {
            T outputValue = evaluator.EvaluateNotGate(value, _context, circuitContext);

            SendOutputValue(outputValue, evaluator, evaluationState, circuitContext);
        }
Exemplo n.º 26
0
 public T EvaluateNotGate(T value, GateContext gateContext, CircuitContext circuitContext)
 {
     return(_innerEvaluator.EvaluateNotGate(value, gateContext, circuitContext));
 }
Exemplo n.º 27
0
 public abstract void ReceiveInputValue <T>(T value, IBatchCircuitEvaluator <T> evaluator, ForwardEvaluationState <T> evaluationState, CircuitContext circuitContext);
Exemplo n.º 28
0
 public override void ReceiveInputValue <T>(T value, IBatchCircuitEvaluator <T> evaluator, ForwardEvaluationState <T> evaluationState, CircuitContext circuitContext)
 {
     evaluationState.SetOutput(_outputIndex, value);
 }
Exemplo n.º 29
0
        public ForwardGate EvaluateXorGate(ForwardGate leftInputGate, ForwardGate rightInputGate, GateContext gateContext, CircuitContext circuitContext)
        {
            ForwardGate gate = new ForwardXorGate(gateContext);

            leftInputGate.AddSuccessor(gate);
            rightInputGate.AddSuccessor(gate);
            return(gate);
        }
Exemplo n.º 30
0
 public ForwardCircuit(IEvaluableCircuit circuit)
 {
     InputGates = ForwardCircuitBuilder.Build(circuit);
     Context    = circuit.Context;
 }
Exemplo n.º 31
0
 private StatisticsCircuitEvaluator()
 {
     _layerStatistics = new List <CircuitLayerStatisticsBuilder>();
     _context         = null;
 }