# Python at globalsino:
# https://www.globalsino.com/ICs/page4853.html
# Electrical circuit with switchs at https://www.globalsino.com/ICs/page4693.html
# Switch-like diode

import csv 
import numpy as np
import matplotlib.pyplot as plt
import sys
import PySpice
import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging()
from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *

def Switch_circuit(Input_Voltage):
    circuit = Circuit('Diode circuit')

    # This is not an ideal diode: An ideal diode is a switch
    circuit.model('TheSwitch', 'D')

    # input voltage source, name voltage source Power, connect nodes 'in' and ground
    circuit.V('Power', 'in_put', circuit.gnd, Input_Voltage@u_V)
    # Both Ω or Ohm can be used as units
    circuit.R(1, 'in_put', 'middle', 5@u_kΩ)
    circuit.R(2, 'middle', circuit.gnd, 0.1@u_kΩ)
    circuit.R(3, 'out_put', circuit.gnd, 0.2@u_kΩ)
    circuit.R(4, 'in_put', 'out_put', 2@u_kΩ)       

    # Create simulator
    simulator = circuit.simulator(temperature=25, nominal_temperature=125)
    analysis = simulator.operating_point()

    if float(analysis.nodes['middle']) > float(analysis.nodes['out_put']):
        circuit.R(5, 'out_put', 'middle', 0@u_kΩ)
        simulator = circuit.simulator(temperature=25, nominal_temperature=125)
        analysis = simulator.operating_point()
        
        print('Input and output voltages are ', '\t', float(analysis.nodes['middle']), '\t', float(analysis.nodes['out_put']), '\t', 'V, respectively')
     
        # Create a csv file and append new rows to the csv file if the csv does not exist;
        # otherwise, the directly append the row to the existing file
        with open (r'C:\GlobalSino\images\Electrical_Simulation.csv', 'a', newline ='') as my_csv_file:
            fieldnames = ['Middle','Output_V']
            thewriter = csv.DictWriter(my_csv_file, fieldnames=fieldnames)
            # thewriter.writeheader()
            thewriter.writerow ({'Middle': float(analysis.nodes['middle']),'Output_V': float(analysis.nodes['out_put'])})
            my_csv_file.close()            

    if float(analysis.nodes['middle']) < float(analysis.nodes['out_put']):
        circuit.R(5, 'out_put', 'middle', 20000000000000000@u_kΩ)
        simulator = circuit.simulator(temperature=25, nominal_temperature=125)
        analysis = simulator.operating_point()

        print('Input and output voltages are ', '\t', float(analysis.nodes['middle']), '\t', float(analysis.nodes['out_put']), '\t', 'V, respectively')
     
        # Create a csv file and append new rows to the csv file if the csv does not exist;
        # otherwise, the directly append the row to the existing file
        with open (r'C:\GlobalSino\images\Electrical_Simulation.csv', 'a', newline ='') as my_csv_file:
            fieldnames = ['Middle','Output_V']
            thewriter = csv.DictWriter(my_csv_file, fieldnames=fieldnames)
            # thewriter.writeheader()
            thewriter.writerow ({'Middle': float(analysis.nodes['middle']),'Output_V': float(analysis.nodes['out_put'])})
            my_csv_file.close()                 

GlobalSino = input ('Enter your number: \t')
EM = int(GlobalSino)
Input_Voltage = -20
for i in range(EM):
    Input_Voltage +=i
    Switch_circuit(Input_Voltage)
    
