Skip to content

Commit

Permalink
Merge pull request #1851 from KLayout/bugfix/issue-1832
Browse files Browse the repository at this point in the history
Bugfix/issue 1832
  • Loading branch information
klayoutmatthias authored Sep 21, 2024
2 parents ff0a2b8 + 074238b commit 2d57fee
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/db/db/dbCircuit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,41 @@ void Circuit::do_purge_nets (bool keep_pins)
}
}

static bool can_purge_device (const db::Device &device)
{
if (! device.device_class ()) {
return false;
}

const std::vector<db::DeviceTerminalDefinition> &tdefs = device.device_class ()->terminal_definitions ();
if (tdefs.size () <= 1) {
return false;
}

const db::Net *net = device.net_for_terminal (tdefs.front ().id ());
for (auto t = tdefs.begin () + 1; t != tdefs.end (); ++t) {
if (net != device.net_for_terminal (t->id ())) {
return false;
}
}

return true;
}

void Circuit::purge_devices ()
{
std::vector<db::Device *> devices_to_be_purged;
for (device_iterator d = begin_devices (); d != end_devices (); ++d) {
if (can_purge_device (*d)) {
devices_to_be_purged.push_back (d.operator-> ());
}
}

for (auto d = devices_to_be_purged.begin (); d != devices_to_be_purged.end (); ++d) {
remove_device (*d);
}
}

/**
* @brief Sanity check for device to be removed
*/
Expand Down
8 changes: 8 additions & 0 deletions src/db/db/dbCircuit.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,14 @@ class DB_PUBLIC Circuit
*/
void purge_nets_keep_pins ();

/**
* @brief Purges invalid devices
*
* This method will purge all invalid devices, i.e. those
* whose terminals are all connected to the same net.
*/
void purge_devices ();

/**
* @brief Combine devices
*
Expand Down
9 changes: 9 additions & 0 deletions src/db/db/dbNetlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,13 @@ void Netlist::purge_nets ()
}
}

void Netlist::purge_devices ()
{
for (bottom_up_circuit_iterator c = begin_bottom_up (); c != end_bottom_up (); ++c) {
c->purge_devices ();
}
}

void Netlist::make_top_level_pins ()
{
size_t ntop = top_circuit_count ();
Expand Down Expand Up @@ -684,6 +691,8 @@ void Netlist::simplify ()
{
make_top_level_pins ();
purge ();

// combine devices are purge nets that are created in that step
combine_devices ();
purge_nets ();
}
Expand Down
8 changes: 8 additions & 0 deletions src/db/db/dbNetlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,14 @@ class DB_PUBLIC Netlist
*/
void purge_nets ();

/**
* @brief Purges invalid devices
*
* This method will purge all invalid devices, i.e. those
* whose terminals are all connected to the same net.
*/
void purge_devices ();

/**
* @brief Creates pins for top-level circuits
*
Expand Down
6 changes: 3 additions & 3 deletions src/db/db/dbNetlistDeviceClasses.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ResistorDeviceCombiner
{
double va = a->parameter_value (0);
double vb = b->parameter_value (0);
a->set_parameter_value (0, va + vb < 1e-10 ? 0.0 : va * vb / (va + vb));
a->set_parameter_value (0, va + vb < 1e-30 ? 0.0 : va * vb / (va + vb));

// parallel width is sum of both, length is the one that gives the same value of resistance
// R = 1/(1/R1 + 1/R2)
Expand Down Expand Up @@ -204,7 +204,7 @@ class CapacitorDeviceCombiner
{
double va = a->parameter_value (0);
double vb = b->parameter_value (0);
a->set_parameter_value (0, va + vb < 1e-10 ? 0.0 : va * vb / (va + vb));
a->set_parameter_value (0, va + vb < 1e-30 ? 0.0 : va * vb / (va + vb));

// TODO: does this implementation make sense?
double aa = a->parameter_value (1);
Expand Down Expand Up @@ -259,7 +259,7 @@ class InductorDeviceCombiner
{
double va = a->parameter_value (0);
double vb = b->parameter_value (0);
a->set_parameter_value (0, va + vb < 1e-10 ? 0.0 : va * vb / (va + vb));
a->set_parameter_value (0, va + vb < 1e-30 ? 0.0 : va * vb / (va + vb));
}

void serial (Device *a, Device *b) const
Expand Down
12 changes: 12 additions & 0 deletions src/db/db/gsiDeclDbNetlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,12 @@ Class<db::Circuit> decl_dbCircuit (decl_dbNetlistObject, "db", "Circuit",
"For example, serial or parallel resistors can be combined into "
"a single resistor.\n"
) +
gsi::method ("purge_devices", &db::Circuit::purge_devices,
"@brief Purges invalid devices.\n"
"Purges devices which are considered invalid. Such devices are for example those whose terminals are all connected to a single net.\n"
"\n"
"This method has been added in version 0.29.7."
) +
gsi::method ("purge_nets", &db::Circuit::purge_nets,
"@brief Purges floating nets.\n"
"Floating nets are nets with no device or subcircuit attached to. Such floating "
Expand Down Expand Up @@ -2176,6 +2182,12 @@ Class<db::Netlist> decl_dbNetlist ("db", "Netlist",
"Floating nets can be created as effect of reconnections of devices or pins. "
"This method will eliminate all nets that make less than two connections."
) +
gsi::method ("purge_devices", &db::Netlist::purge_devices,
"@brief Purges invalid devices.\n"
"Purges devices which are considered invalid. Such devices are for example those whose terminals are all connected to a single net.\n"
"\n"
"This method has been added in version 0.29.7."
) +
gsi::method ("simplify", &db::Netlist::simplify,
"@brief Convenience method that combines the simplification.\n"
"This method is a convenience method that runs \\make_top_level_pins, \\purge, \\combine_devices and \\purge_nets."
Expand Down
Loading

0 comments on commit 2d57fee

Please sign in to comment.