piel.integration.gdsfactory_hdl21
=================================

.. py:module:: piel.integration.gdsfactory_hdl21


Submodules
----------

.. toctree::
   :maxdepth: 1

   /autoapi/piel/integration/gdsfactory_hdl21/conversion/index
   /autoapi/piel/integration/gdsfactory_hdl21/core/index
   /autoapi/piel/integration/gdsfactory_hdl21/utils/index


Functions
---------

.. autoapisummary::

   piel.integration.gdsfactory_hdl21.gdsfactory_netlist_to_spice_netlist
   piel.integration.gdsfactory_hdl21.construct_hdl21_module
   piel.integration.gdsfactory_hdl21.convert_connections_to_tuples
   piel.integration.gdsfactory_hdl21.gdsfactory_netlist_to_spice_string_connectivity_netlist
   piel.integration.gdsfactory_hdl21.gdsfactory_netlist_with_hdl21_generators
   piel.integration.gdsfactory_hdl21.get_matching_connections
   piel.integration.gdsfactory_hdl21.get_matching_port_nets


Package Contents
----------------

.. py:function:: gdsfactory_netlist_to_spice_netlist(gdsfactory_netlist: dict, generators: dict, **kwargs) -> piel.types.AnalogueModule

   This function converts a GDSFactory electrical netlist into a standard SPICE netlist. It follows the same
   principle as the `sax` circuit composition.

   Each GDSFactory netlist has a set of instances, each with a corresponding model, and each instance with a given
   set of geometrical settings that can be applied to each particular model. We know the type of SPICE model from
   the instance model we provides.

   We know that the gdsfactory has a set of instances, and we can map unique measurement via sax through our own
   composition circuit. Write the SPICE component based on the model into a total circuit representation in string
   from the reshaped gdsfactory dictionary into our own structure.

   :param gdsfactory_netlist: GDSFactory netlist
   :param generators: Dictionary of Generators

   :returns: hdl21 module or raw SPICE string


.. py:function:: construct_hdl21_module(spice_netlist: dict, **kwargs) -> piel.types.AnalogueModule

   This function converts a gdsfactory-spice converted netlist using the component measurement into a SPICE circuit.

   Part of the complexity of this function is the multiport nature of some components and measurement, and assigning the
   parameters accordingly into the SPICE function. This is because not every SPICE component will be bi-port,
   and many will have multi-connection and parameters accordingly. Each model can implement the composition into a
   SPICE circuit, but they depend on a set of parameters that must be set from the instance. Another aspect is
   that we may want to assign the component ID according to the type of component. However, we can also assign the
   ID based on the individual instance in the circuit, which is also a reasonable approximation. However,
   it could be said, that the ideal implementation would be for each component model provided to return the SPICE
   instance including connectivity except for the ID.

   # TODO implement validators


.. py:function:: convert_connections_to_tuples(connections: dict)

   Convert from:

   .. code-block::

       {
       'straight_1,e1': 'taper_1,e2',
       'straight_1,e2': 'taper_2,e2',
       'taper_1,e1': 'via_stack_1,e3',
       'taper_2,e1': 'via_stack_2,e1'
       }

   to:

   .. code-block::

       [(('straight_1', 'e1'), ('taper_1', 'e2')), (('straight_1', 'e2'), ('taper_2', 'e2')), (('taper_1', 'e1'),
       ('via_stack_1', 'e3')), (('taper_2', 'e1'), ('via_stack_2', 'e1'))]


.. py:function:: gdsfactory_netlist_to_spice_string_connectivity_netlist(gdsfactory_netlist: dict, models=None)

   Not for the `hdl21` design flow, but useful for other SPICE level conversions.

   This function maps the connections of a netlist to a node that can be used in a SPICE netlist. SPICE netlists are
   in the form of:

   .. code-block:: spice

       RXXXXXXX N1 N2 <VALUE> <MNAME> <L=LENGTH> <W=WIDTH> <TEMP=T>

   This means that every instance, is an electrical type, and we define the two particular nodes in which it is
   connected. This means we need to convert the gdsfactory dictionary netlist into a form that allows us to map the
   connectivity for every instance. Then we can define that as a line of the SPICE netlist with a particular
   electrical model. For passives this works fine when it's a two port network such as sources, or electrical
   elements. However, non-passive elements like transistors have three connection or more which are provided in an ordered form.

   This means that the order of translations is as follows:

   .. code-block::

       1. Extract all instances and required measurement from the netlist, and assign the corresponding parameters on instantiation.
       2. Verify that the measurement have been provided. Each model describes the type of component this is, how many connection it requires and so on.
       3. Map the connections to each instance port as part of the instance dictionary.

   We should get as an output a dictionary in the structure:

   .. code-block::

       {
           instance_1: {
               ...
               "connections": [('straight_1,e1', 'taper_1,e2'),
                               ('straight_1,e2', 'taper_2,e2')],
               'spice_nets': {'e1': 'straight_1__e1___taper_1__e2',
                       'e2': 'straight_1__e2___taper_2__e2'},
               'spice_model': <function piel.measurement.physical.electronic.spice.resistor.basic_resistor()>},
           }
           ...
       }


.. py:function:: gdsfactory_netlist_with_hdl21_generators(gdsfactory_netlist: dict, generators=None)

   This function allows us to map the ``hdl21`` measurement dictionary in a `sax`-like implementation to the ``GDSFactory`` netlist. This allows us to iterate over each instance in the netlist and construct a circuit after this function.]

   Example usage:

   .. code-block::

       >>> import gdsfactory as gf
       >>> from piel.integration.gdsfactory_hdl21.conversion import gdsfactory_netlist_with_hdl21_generators
       >>> from piel.measurement.physical.electronic import get_default_models
       >>> gdsfactory_netlist_with_hdl21_generators(gdsfactory_netlist=gf.components.mzi2x2_2x2_phase_shifter().get_netlist(exclude_port_types="optical"),generators=get_default_models())

   :param gdsfactory_netlist: The netlist from ``GDSFactory`` to map to the ``hdl21`` measurement dictionary.
   :param generators: The ``hdl21`` measurement dictionary to map to the ``GDSFactory`` netlist.

   :returns: The ``GDSFactory`` netlist with the ``hdl21`` measurement dictionary.


.. py:function:: get_matching_connections(names: list, connections: dict)

   This function returns a list of tuples that match the names of the connections.

   :param names: List of names to match
   :param connections: Dictionary of connections to match

   :returns: List of tuples that match the names of the connections


.. py:function:: get_matching_port_nets(names, connections)

