bindings: fix segmentation violation in python scripts
And move sample-rng-plot.py code to a main function
This commit is contained in:
@@ -235,7 +235,7 @@ def main(argv):
|
||||
# Reset the address base-- all of the 802.11 networks will be in
|
||||
# the "10.0" address space
|
||||
ipAddrs.SetBase(ns.network.Ipv4Address("10.0.0.0"), ns.network.Ipv4Mask("255.255.255.0"))
|
||||
|
||||
tempRef = [] # list of references to be held to prevent garbage collection
|
||||
for i in range(backboneNodes):
|
||||
print ("Configuring wireless network for backbone node ", i)
|
||||
#
|
||||
@@ -279,11 +279,18 @@ def main(argv):
|
||||
# the network mask initialized above
|
||||
#
|
||||
ipAddrs.NewNetwork()
|
||||
|
||||
# This call returns an instance that needs to be stored in the outer scope
|
||||
# not to be garbage collected when overwritten in the next iteration
|
||||
subnetAlloc = ns.mobility.ListPositionAllocator()
|
||||
|
||||
# Appending the object to a list is enough to prevent the garbage collection
|
||||
tempRef.append(subnetAlloc)
|
||||
|
||||
#
|
||||
# The new wireless nodes need a mobility model so we aggregate one
|
||||
# to each of the nodes we just finished building.
|
||||
#
|
||||
subnetAlloc = ns.mobility.ListPositionAllocator()
|
||||
for j in range(infra.GetN()):
|
||||
subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0))
|
||||
|
||||
|
||||
@@ -29,37 +29,43 @@ import sys
|
||||
import argparse
|
||||
from ns import ns
|
||||
|
||||
parser = argparse.ArgumentParser("sample-rng-plot")
|
||||
parser.add_argument("--not-blocking",
|
||||
action="store_true",
|
||||
default=False)
|
||||
args = parser.parse_args(sys.argv[1:])
|
||||
|
||||
# mu, var = 100, 225
|
||||
def main():
|
||||
parser = argparse.ArgumentParser("sample-rng-plot")
|
||||
parser.add_argument("--not-blocking",
|
||||
action="store_true",
|
||||
default=False)
|
||||
args = parser.parse_args(sys.argv[1:])
|
||||
|
||||
## Random number generator.
|
||||
rng = ns.CreateObject("NormalRandomVariable")
|
||||
rng.SetAttribute("Mean", ns.DoubleValue(100.0))
|
||||
rng.SetAttribute("Variance", ns.DoubleValue(225.0))
|
||||
# mu, var = 100, 225
|
||||
|
||||
## Random number samples.
|
||||
x = [rng.GetValue() for t in range(10000)]
|
||||
## Random number generator.
|
||||
rng = ns.CreateObject("NormalRandomVariable")
|
||||
rng.SetAttribute("Mean", ns.DoubleValue(100.0))
|
||||
rng.SetAttribute("Variance", ns.DoubleValue(225.0))
|
||||
|
||||
# the histogram of the data
|
||||
## Random number samples.
|
||||
x = [rng.GetValue() for t in range(10000)]
|
||||
|
||||
## Make a probability density histogram
|
||||
density = 1
|
||||
## Plot color
|
||||
facecolor='g'
|
||||
## Plot alpha value (transparency)
|
||||
alpha=0.75
|
||||
# the histogram of the data
|
||||
|
||||
# We don't really need the plot results, we're just going to show it later.
|
||||
# n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75)
|
||||
n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)
|
||||
## Make a probability density histogram
|
||||
density = 1
|
||||
## Plot color
|
||||
facecolor = 'g'
|
||||
## Plot alpha value (transparency)
|
||||
alpha = 0.75
|
||||
|
||||
plt.title('ns-3 histogram')
|
||||
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
|
||||
plt.axis([40, 160, 0, 0.03])
|
||||
plt.grid(True)
|
||||
plt.show(block=not args.not_blocking)
|
||||
# We don't really need the plot results, we're just going to show it later.
|
||||
# n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75)
|
||||
n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)
|
||||
|
||||
plt.title('ns-3 histogram')
|
||||
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
|
||||
plt.axis([40, 160, 0, 0.03])
|
||||
plt.grid(True)
|
||||
plt.show(block=not args.not_blocking)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -214,6 +214,8 @@ class TestSimulator(unittest.TestCase):
|
||||
self.assertTrue(self._received_packet is not None)
|
||||
self.assertEqual(self._received_packet.GetSize(), 19)
|
||||
|
||||
# Delete Ptr<>'s on the python side to let C++ clean them
|
||||
del internet
|
||||
|
||||
def testAttributes(self):
|
||||
"""! Test attributes function
|
||||
@@ -243,6 +245,9 @@ class TestSimulator(unittest.TestCase):
|
||||
mobility.GetAttribute("PositionAllocator", ptr2)
|
||||
self.assertNotEqual(ptr.GetObject(), ns.core.Ptr["Object"](ns.cppyy.nullptr))
|
||||
|
||||
# Delete Ptr<>'s on the python side to let C++ clean them
|
||||
del queue, mobility, ptr, ptr2
|
||||
|
||||
def testIdentity(self):
|
||||
"""! Test identify
|
||||
@param self this object
|
||||
@@ -257,6 +262,9 @@ class TestSimulator(unittest.TestCase):
|
||||
|
||||
self.assertEqual(c1, c2)
|
||||
|
||||
# Delete Ptr<>'s on the python side to let C++ clean them
|
||||
del csma, channel
|
||||
|
||||
def testTypeId(self):
|
||||
"""! Test type ID
|
||||
@param self this object
|
||||
|
||||
Reference in New Issue
Block a user