Repository: PMEAL/OpenPNM
Pull request #1414 (2020-03-03 05:42:26)

Fixing1410 fixing merge networks
Merge network was failing when networks had odd sized data other than Np/Nt by 1.  This PR increases the robustness of the data size checking during merges.


Patch:
diff --git a/openpnm/topotools/topotools.py b/openpnm/topotools/topotools.py
index 7d40528d4..7a508c936 100644
--- a/openpnm/topotools/topotools.py
+++ b/openpnm/topotools/topotools.py
@@ -1436,18 +1436,23 @@ def merge_networks(network, donor=[]):
             if key.split('.')[1] not in ['conns', 'coords', '_id', 'all']:
                 if key in network.keys():
                     pop_flag = False
+                    # If key not on donor add it first with dummy values to
+                    # simplify merging later
                     if key not in donor.keys():
                         logger.debug('Adding ' + key + ' to donor')
-                        # If key not on donor add it first
-                        if network[key].dtype == bool:
+                        if network[key].dtype == bool:  # Deal with labels
                             donor[key] = False
-                        else:
-                            donor[key] = sp.nan
+                        else:  # Deal with numerical data
+                            element = key.split('.')[0]
+                            shape = list(network[key].shape)
+                            N = donor._count(element)
+                            shape[0] = N
+                            donor[key] = sp.empty(shape=shape)*sp.nan
                         pop_flag = True
                     # Then merge it with existing array on network
-                    try:
+                    if len(network[key].shape) == 1:
                         temp = np.hstack((network[key], donor[key]))
-                    except ValueError:
+                    else:
                         temp = np.vstack((network[key], donor[key]))
                     network[key] = temp
                     if pop_flag:


Test patch:
diff --git a/tests/unit/topotools/TopotoolsTest.py b/tests/unit/topotools/TopotoolsTest.py
index ce278fa56..bb4e879c1 100644
--- a/tests/unit/topotools/TopotoolsTest.py
+++ b/tests/unit/topotools/TopotoolsTest.py
@@ -152,13 +152,15 @@ class TopotoolsTest:
         net2 = op.network.Cubic(shape=[3, 3, 3])
         net1['pore.test1'] = True
         net1['pore.test2'] = 10
-        net2['pore.test3'] = True
-        net2['pore.test4'] = 10.0
-        topotools.merge_networks(net1, net2)
+        net1['pore.test3'] = np.ones((net1.Np, 3))
+        net2['pore.test4'] = True
+        net2['pore.test5'] = 10.0
+        net2['pore.test6'] = np.ones((net2.Np, 2))
+        topotools.merge_networks(network=net1, donor=net2)
         assert np.sum(net1['pore.test1']) == 27
-        assert np.sum(net1['pore.test3']) == 27
-        assert np.sum(net1['pore.test2'][:27]) == 270
-        assert np.sum(net1['pore.test4'][27:]) == 270
+        assert np.all(net1['pore.test3'].shape == (54, 3))
+        assert np.sum(net1['pore.test2'][:27]) == 270.0
+        assert np.sum(net1['pore.test4'][27:]) == 27
         assert 'pore.test1' not in net2
         assert 'pore.test2' not in net2