diff --git a/bindings/pyfiction/include/pyfiction/io/read_fgl_layout.hpp b/bindings/pyfiction/include/pyfiction/io/read_fgl_layout.hpp new file mode 100644 index 000000000..07cdbfa32 --- /dev/null +++ b/bindings/pyfiction/include/pyfiction/io/read_fgl_layout.hpp @@ -0,0 +1,42 @@ +// +// Created by simon on 09.10.22. +// + +#ifndef PYFICTION_READ_FGL_LAYOUT_HPP +#define PYFICTION_READ_FGL_LAYOUT_HPP + +#include "pyfiction/documentation.hpp" +#include "pyfiction/types.hpp" + +#include +#include + +#include + +#include + +namespace pyfiction +{ + +inline void read_fgl_layout(pybind11::module& m) +{ + using namespace pybind11::literals; + + py_cartesian_gate_layout (*read_cartesian_fgl_layout_function_pointer)( + const std::string_view&, const std::string_view&) = &fiction::read_fgl_layout; + py_shifted_cartesian_gate_layout (*read_shifted_cartesian_fgl_layout_function_pointer)( + const std::string_view&, const std::string_view&) = &fiction::read_fgl_layout; + py_hexagonal_gate_layout (*read_hexagonal_fgl_layout_function_pointer)( + const std::string_view&, const std::string_view&) = &fiction::read_fgl_layout; + + m.def("read_cartesian_fgl_layout", read_cartesian_fgl_layout_function_pointer, "filename"_a, "layout_name"_a = "", + DOC(fiction_read_fgl_layout_3)); + m.def("read_shifted_cartesian_fgl_layout", read_shifted_cartesian_fgl_layout_function_pointer, "filename"_a, + "layout_name"_a = "", DOC(fiction_read_fgl_layout_3)); + m.def("read_hexagonal_fgl_layout", read_hexagonal_fgl_layout_function_pointer, "filename"_a, "layout_name"_a = "", + DOC(fiction_read_fgl_layout_3)); +} + +} // namespace pyfiction + +#endif // PYFICTION_READ_FGL_LAYOUT_HPP diff --git a/bindings/pyfiction/include/pyfiction/io/write_fgl_layout.hpp b/bindings/pyfiction/include/pyfiction/io/write_fgl_layout.hpp new file mode 100644 index 000000000..556ac5d98 --- /dev/null +++ b/bindings/pyfiction/include/pyfiction/io/write_fgl_layout.hpp @@ -0,0 +1,46 @@ +// +// Created by simon on 09.10.22. +// + +#ifndef PYFICTION_WRITE_FGL_LAYOUT_HPP +#define PYFICTION_WRITE_FGL_LAYOUT_HPP + +#include "pyfiction/documentation.hpp" +#include "pyfiction/types.hpp" + +#include +#include + +#include + +#include + +namespace pyfiction +{ + +namespace detail +{ + +template +void write_fgl_layout(pybind11::module& m) +{ + using namespace pybind11::literals; + + m.def( + "write_fgl_layout", + [](const Lyt& lyt, const std::string_view& filename) { fiction::write_fgl_layout(lyt, filename); }, + "layout"_a, "filename"_a, DOC(fiction_write_fgl_layout_2)); +} + +} // namespace detail + +inline void write_fgl_layout(pybind11::module& m) +{ + detail::write_fgl_layout(m); + detail::write_fgl_layout(m); + detail::write_fgl_layout(m); +} + +} // namespace pyfiction + +#endif // PYFICTION_WRITE_FGL_LAYOUT_HPP diff --git a/bindings/pyfiction/include/pyfiction/pybind11_mkdoc_docstrings.hpp b/bindings/pyfiction/include/pyfiction/pybind11_mkdoc_docstrings.hpp index cf25e5413..ba511911d 100644 --- a/bindings/pyfiction/include/pyfiction/pybind11_mkdoc_docstrings.hpp +++ b/bindings/pyfiction/include/pyfiction/pybind11_mkdoc_docstrings.hpp @@ -5532,6 +5532,48 @@ neutral). Parameter ``charge_layout``: Initialized charge layout.)doc"; +static const char *__doc_fiction_detail_read_fgl_layout_impl = R"doc()doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_gate_storage = +R"doc(@struct gate_storage + +Represents a gate in a fcn layout, storing its unique ID, type, name, +location, and incoming connections.)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_gate_storage_compare_by_id = +R"doc(Static member function to compare gate_storage objects by their IDs. + +Parameter ``gate1``: + First gate to be compared. + +Parameter ``gate2``: + Second gate to be compared. + +Returns: + True if gate1's ID is less than gate2's ID, false otherwise.)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_gate_storage_id = R"doc(Unique identifier for the gate.)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_gate_storage_incoming = R"doc(List of incoming connections to the gate.)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_gate_storage_loc = R"doc(Location of the gate represented its x-, y- and z-coordinate.)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_gate_storage_name = R"doc(Name of the gate (for inputs and outputs).)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_gate_storage_type = +R"doc(Type of the gate, can be an alias (AND, OR, PI, ..) or the implemented +function in a binary or hexadecimal form.)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_is = R"doc(The input stream from which the gate-level layout is read.)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_lyt = R"doc(The layout which will be altered based on the parsed information.)doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_read_fgl_layout_impl = R"doc()doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_read_fgl_layout_impl_2 = R"doc()doc"; + +static const char *__doc_fiction_detail_read_fgl_layout_impl_run = R"doc()doc"; + static const char *__doc_fiction_detail_read_fqca_layout_impl = R"doc()doc"; static const char *__doc_fiction_detail_read_fqca_layout_impl_cell_label_map = R"doc()doc"; @@ -5773,6 +5815,16 @@ static const char *__doc_fiction_detail_wire_east = R"doc()doc"; static const char *__doc_fiction_detail_wire_south = R"doc()doc"; +static const char *__doc_fiction_detail_write_fgl_layout_impl = R"doc()doc"; + +static const char *__doc_fiction_detail_write_fgl_layout_impl_lyt = R"doc(The layout to be written.)doc"; + +static const char *__doc_fiction_detail_write_fgl_layout_impl_os = R"doc(The output stream to which the gate-level layout is written.)doc"; + +static const char *__doc_fiction_detail_write_fgl_layout_impl_run = R"doc()doc"; + +static const char *__doc_fiction_detail_write_fgl_layout_impl_write_fgl_layout_impl = R"doc()doc"; + static const char *__doc_fiction_detail_write_fqca_layout_impl = R"doc()doc"; static const char *__doc_fiction_detail_write_fqca_layout_impl_alphabet_iterator = R"doc()doc"; @@ -7076,6 +7128,16 @@ Parameter ``g``: Returns: Transposed `fcn_gate`.)doc"; +static const char *__doc_fiction_fgl_parsing_error = +R"doc(Exception thrown when an error occurs during parsing of a .fgl file +containing a gate-level layout.)doc"; + +static const char *__doc_fiction_fgl_parsing_error_fgl_parsing_error = +R"doc(Constructs a fgl_parsing_error object with the given error message. + +Parameter ``msg``: + The error message describing the parsing error.)doc"; + static const char *__doc_fiction_find_first_two_of = R"doc(A derivative of `std::find_first_of` that uses the example implementation given at @@ -11776,6 +11838,72 @@ R"doc(Standard constructor with forward reference. Parameter ``range``: Begin and end iterator pair.)doc"; +static const char *__doc_fiction_read_fgl_layout = +R"doc(Reads a gate-level layout from an FGL file provided as an input +stream. + +May throw an `fgl_parsing_exception` if the FGL file is malformed. + +Template parameter ``Lyt``: + The layout type to be created from an input. + +Parameter ``is``: + The input stream to read from. + +Parameter ``name``: + The name to give to the generated layout.)doc"; + +static const char *__doc_fiction_read_fgl_layout_2 = +R"doc(Reads a gate-level layout from an FGL file provided as an input +stream. + +May throw an `fgl_parsing_exception` if the FGL file is malformed. + +This is an in-place version of read_fgl_layout that utilizes the given +layout as a target to write to. + +Template parameter ``Lyt``: + The layout type to be used as input. + +Parameter ``lyt``: + The layout to write to. + +Parameter ``is``: + The input stream to read from.)doc"; + +static const char *__doc_fiction_read_fgl_layout_3 = +R"doc(Reads a gate-level layout from an FGL file provided as an input +stream. + +May throw an `fgl_parsing_exception` if the FGL file is malformed. + +Template parameter ``Lyt``: + The layout type to be created from an input. + +Parameter ``filename``: + The file name to open and read from. + +Parameter ``name``: + The name to give to the generated layout.)doc"; + +static const char *__doc_fiction_read_fgl_layout_4 = +R"doc(Reads a gate-level layout from an FGL file provided as an input +stream. + +May throw an `fgl_parsing_exception` if the FGL file is malformed. + +This is an in-place version of `read_fgl_layout` that utilizes the +given layout as a target to write to. + +Template parameter ``Lyt``: + The layout type to be used as input. + +Parameter ``lyt``: + The layout to write to. + +Parameter ``filename``: + The file name to open and read from.)doc"; + static const char *__doc_fiction_read_fqca_layout = R"doc(Reads a cell-level QCA layout from an fqca file provided as an input stream. The format is used by QCA-STACK by Willem Lambooy @@ -13806,6 +13934,35 @@ Parameter ``lyt``: Parameter ``filename``: Filename)doc"; +static const char *__doc_fiction_write_fgl_layout = +R"doc(Writes an FGL layout to a file. + +This overload uses an output stream to write into. + +Template parameter ``Lyt``: + Layout. + +Parameter ``lyt``: + The layout to be written. + +Parameter ``os``: + The output stream to write into.)doc"; + +static const char *__doc_fiction_write_fgl_layout_2 = +R"doc(Writes an FGL layout to a file. + +This overload uses a file name to create and write into. + +Template parameter ``Lyt``: + Layout. + +Parameter ``lyt``: + The layout to be written. + +Parameter ``filename``: + The file name to create and write into. Should preferably use the + .fgl extension.)doc"; + static const char *__doc_fiction_write_fqca_layout = R"doc(Writes a cell-level QCA layout to an fqca file provided as an output stream. The format is used by QCA-STACK by Willem Lambooy diff --git a/bindings/pyfiction/pyfiction.cpp b/bindings/pyfiction/pyfiction.cpp index bc77af7b5..98ec6184d 100644 --- a/bindings/pyfiction/pyfiction.cpp +++ b/bindings/pyfiction/pyfiction.cpp @@ -31,9 +31,11 @@ // #include "pyfiction/algorithms/simulation/sidb/time_to_solution.hpp" #include "pyfiction/algorithms/verification/design_rule_violations.hpp" #include "pyfiction/algorithms/verification/equivalence_checking.hpp" +#include "pyfiction/io/read_fgl_layout.hpp" #include "pyfiction/io/read_fqca_layout.hpp" #include "pyfiction/io/read_sqd_layout.hpp" #include "pyfiction/io/write_dot_layout.hpp" +#include "pyfiction/io/write_fgl_layout.hpp" #include "pyfiction/io/write_fqca_layout.hpp" #include "pyfiction/io/write_qca_layout.hpp" #include "pyfiction/io/write_qcc_layout.hpp" @@ -139,12 +141,14 @@ PYBIND11_MODULE(pyfiction, m) * Input/Output */ pyfiction::write_dot_layout(m); + pyfiction::write_fgl_layout(m); pyfiction::write_qca_layout(m); pyfiction::write_svg_layout(m); pyfiction::write_sqd_layout(m); pyfiction::write_qcc_layout(m); pyfiction::write_qll_layout(m); pyfiction::write_fqca_layout(m); + pyfiction::read_fgl_layout(m); pyfiction::read_fqca_layout(m); pyfiction::read_sqd_layout(m); /** diff --git a/bindings/pyfiction/test/io/__init__.py b/bindings/pyfiction/test/io/__init__.py new file mode 100644 index 000000000..5fde0b8ad --- /dev/null +++ b/bindings/pyfiction/test/io/__init__.py @@ -0,0 +1,16 @@ +from os.path import dirname, basename, isfile, join +import glob +import os +import sys +from pathlib import Path + +if sys.platform == "win32" and sys.version_info > (3, 8, 0) and "Z3_ROOT" in os.environ: + lib_path = Path(os.environ["Z3_ROOT"]) / "lib" + if lib_path.exists(): + os.add_dll_directory(str(lib_path)) + bin_path = Path(os.environ["Z3_ROOT"]) / "bin" + if bin_path.exists(): + os.add_dll_directory(str(bin_path)) + +modules = glob.glob(join(dirname(__file__), "*.py")) +__all__ = [basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')] diff --git a/bindings/pyfiction/test/io/test_read_write_fgl_layout.py b/bindings/pyfiction/test/io/test_read_write_fgl_layout.py new file mode 100644 index 000000000..c2d5d89d0 --- /dev/null +++ b/bindings/pyfiction/test/io/test_read_write_fgl_layout.py @@ -0,0 +1,39 @@ +from fiction.pyfiction import * +from dotenv import load_dotenv +import unittest +import os + +dir_path = os.path.dirname(os.path.realpath(__file__)) +load_dotenv() +z3 = os.environ.get("z3", "OFF") + + +class TestReadWriteFglLayout(unittest.TestCase): + + def test_read_write(self): + network = read_logic_network(dir_path + "/../resources/mux21.v") + cart_layout = orthogonal(network) + write_fgl_layout(cart_layout, "mux21_cartesian.fgl") + read_cart_layout = read_cartesian_fgl_layout("mux21_cartesian.fgl") + self.assertEqual(equivalence_checking(read_cart_layout, cart_layout), eq_type.STRONG) + + hex_layout = hexagonalization(cart_layout) + write_fgl_layout(hex_layout, "mux21_hexagonal.fgl") + read_hexagonal_layout = read_hexagonal_fgl_layout("mux21_hexagonal.fgl") + self.assertEqual(equivalence_checking(read_hexagonal_layout, hex_layout), eq_type.STRONG) + + shifted_cart_layout = shifted_cartesian_gate_layout((3, 3, 1), "2DDWave", "Layout") + write_fgl_layout(shifted_cart_layout, "empty_shifted_cartesian.fgl") + read_shifted_cart_layout = read_shifted_cartesian_fgl_layout("empty_shifted_cartesian.fgl") + self.assertEqual(equivalence_checking(read_shifted_cart_layout, shifted_cart_layout), eq_type.STRONG) + + try: + os.remove("mux21_cartesian.fgl") + os.remove("mux21_hexagonal.fgl") + os.remove("empty_shifted_cartesian.fgl") + except OSError as e: + print("Error: %s - %s." % (e.filename, e.strerror)) + + +if __name__ == '__main__': + unittest.main() diff --git a/docs/io/input.rst b/docs/io/input.rst index 0c579dd99..484564fc1 100644 --- a/docs/io/input.rst +++ b/docs/io/input.rst @@ -22,14 +22,22 @@ Truth Tables Gate-level Layouts ################## -**Header:** ``fiction/io/read_fgl_layout.hpp`` +.. tabs:: + .. tab:: C++ + + **Header:** ``fiction/io/read_fgl_layout.hpp`` -.. doxygenfunction:: fiction::read_fgl_layout(std::istream& is, const std::string_view& name = "") -.. doxygenfunction:: fiction::read_fgl_layout(Lyt& lyt, std::istream& is) -.. doxygenfunction:: fiction::read_fgl_layout(const std::string_view& filename, const std::string_view& name = "") -.. doxygenfunction:: fiction::read_fgl_layout(Lyt& lyt, const std::string_view& filename) + .. doxygenfunction:: fiction::read_fgl_layout(std::istream& is, const std::string_view& name = "") + .. doxygenfunction:: fiction::read_fgl_layout(Lyt& lyt, std::istream& is) + .. doxygenfunction:: fiction::read_fgl_layout(const std::string_view& filename, const std::string_view& name = "") + .. doxygenfunction:: fiction::read_fgl_layout(Lyt& lyt, const std::string_view& filename) -.. doxygenclass:: fiction::fgl_parsing_error + .. doxygenclass:: fiction::fgl_parsing_error + + .. tab:: Python + .. autofunction:: fiction.pyfiction.read_cartesian_fgl_layout + .. autofunction:: fiction.pyfiction.read_shifted_cartesian_fgl_layout + .. autofunction:: fiction.pyfiction.read_hexagonal_fgl_layout Cell-level Layouts ################## @@ -47,8 +55,7 @@ Cell-level Layouts .. doxygenclass:: fiction::sqd_parsing_error .. tab:: Python - .. autoclass:: fiction.pyfiction.read_sqd_layout - :members: + .. autofunction:: fiction.pyfiction.read_sqd_layout .. tabs:: .. tab:: C++ @@ -62,8 +69,7 @@ Cell-level Layouts .. doxygenclass:: fiction::unrecognized_cell_definition_exception .. tab:: Python - .. autoclass:: fiction.pyfiction.read_fqca_layout - :members: + .. autofunction:: fiction.pyfiction.read_fqca_layout SiDB Surface Defects #################### diff --git a/docs/io/physical_simulation.rst b/docs/io/physical_simulation.rst index a98b617da..f3499f683 100644 --- a/docs/io/physical_simulation.rst +++ b/docs/io/physical_simulation.rst @@ -125,7 +125,12 @@ SiDB Operational Domain Technology-independent Gate-level Layouts ######################################### -**Header:** ``fiction/io/write_fgl_layout.hpp`` +.. tabs:: + .. tab:: C++ + **Header:** ``fiction/io/write_fgl_layout.hpp`` -.. doxygenfunction:: fiction::write_fgl_layout(const Lyt& lyt, std::ostream& os) -.. doxygenfunction:: fiction::write_fgl_layout(const Lyt& lyt, const std::string_view& filename) + .. doxygenfunction:: fiction::write_fgl_layout(const Lyt& lyt, std::ostream& os) + .. doxygenfunction:: fiction::write_fgl_layout(const Lyt& lyt, const std::string_view& filename) + + .. tab:: Python + .. autofunction:: fiction.pyfiction.write_fgl_layout