Matrix propagation loss model added
authorPavel Boyko <boyko@iitp.ru>
Tue, 09 Mar 2010 11:35:09 +0300
changeset 6115 edc1648e5ad4
parent 6114 7410349be0f9
child 6116 0a1b161d72dd
Matrix propagation loss model added
examples/wireless/wscript
src/common/propagation-loss-model-test-suite.cc
src/common/propagation-loss-model.cc
src/common/propagation-loss-model.h
--- a/examples/wireless/wscript	Mon Mar 08 22:36:54 2010 -0500
+++ b/examples/wireless/wscript	Tue Mar 09 11:35:09 2010 +0300
@@ -37,3 +37,7 @@
 
     obj = bld.create_ns3_program('wifi-blockack', ['core', 'simulator', 'mobility', 'wifi'])
     obj.source = 'wifi-blockack.cc'
+    
+    obj = bld.create_ns3_program('wifi-hidden-terminal', ['core', 'simulator', 'mobility', 'wifi', 'flow-monitor'])
+    obj.source = 'wifi-hidden-terminal.cc'
+
--- a/src/common/propagation-loss-model-test-suite.cc	Mon Mar 08 22:36:54 2010 -0500
+++ b/src/common/propagation-loss-model-test-suite.cc	Tue Mar 09 11:35:09 2010 +0300
@@ -367,6 +367,42 @@
   return GetErrorStatus ();
 }
 
+struct MatrixPropagationLossModelTestCase : public TestCase
+{
+  MatrixPropagationLossModelTestCase () : TestCase ("Test MatrixPropagationLossModel") {}
+  
+  bool DoRun ()
+  {
+    Ptr<Node> n[3];
+    Ptr<MobilityModel> m[3];
+    for (int i = 0; i < 3; ++i)
+      {
+        n[i] = CreateObject<Node> ();
+        m[i] = CreateObject<ConstantPositionMobilityModel> ();
+        n[i]->AggregateObject (m[i]);
+      }
+    
+    MatrixPropagationLossModel loss;
+    // no loss by default
+    loss.SetDefaultLoss (0);       
+    // -10 dB for 0 -> 1 and 1 -> 0
+    loss.SetLoss (n[0], n[1], 10); 
+    // -30 dB from 0 to 2 and -100 dB from 2 to 0
+    loss.SetLoss (n[0], n[2], 30, /*symmetric = */false); 
+    loss.SetLoss (n[2], n[0], 100, /*symmetric = */false); 
+    // default from 1 to 2
+    
+    NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[0], m[1]), -10, "Loss 0 -> 1 incorrect");
+    NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[1], m[0]), -10, "Loss 1 -> 0 incorrect");
+    NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[0], m[2]), -30, "Loss 0 -> 2 incorrect");
+    NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[2], m[0]), -100, "Loss 2 -> 0 incorrect");
+    NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[1], m[2]), 0, "Loss 1 -> 2 incorrect");
+    NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[2], m[1]), 0, "Loss 2 -> 1 incorrect");
+    
+    return GetErrorStatus ();
+  }
+};
+
 class PropagationLossModelsTestSuite : public TestSuite
 {
 public:
@@ -379,6 +415,7 @@
   AddTestCase (new FriisPropagationLossModelTestCase);
   AddTestCase (new TwoRayGroundPropagationLossModelTestCase);
   AddTestCase (new LogDistancePropagationLossModelTestCase);
+  AddTestCase (new MatrixPropagationLossModelTestCase);
 }
 
 PropagationLossModelsTestSuite WifiPropagationLossModelsTestSuite;
--- a/src/common/propagation-loss-model.cc	Mon Mar 08 22:36:54 2010 -0500
+++ b/src/common/propagation-loss-model.cc	Tue Mar 09 11:35:09 2010 +0300
@@ -18,6 +18,7 @@
  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
  * Contributions: Timo Bingmann <timo.bingmann@student.kit.edu>
  * Contributions: Tom Hewer <tomhewer@mac.com> for Two Ray Ground Model
+ *                Pavel Boyko <boyko@iitp.ru> for matrix
  */
 
 #include "propagation-loss-model.h"
@@ -714,4 +715,79 @@
 
 // ------------------------------------------------------------------------- //
 
+NS_OBJECT_ENSURE_REGISTERED (MatrixPropagationLossModel);
+
+TypeId 
+MatrixPropagationLossModel::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("ns3::TopologicalPropagationLossModel")
+    .SetParent<PropagationLossModel> ()
+    .AddConstructor<MatrixPropagationLossModel> ()
+    .AddAttribute ("DefaultLoss", "The default value for propagation loss, dB.",
+                   DoubleValue (std::numeric_limits<double>::max ()),
+                   MakeDoubleAccessor (&MatrixPropagationLossModel::m_default),
+                   MakeDoubleChecker<double> ())
+    ;
+  return tid;
+}
+
+MatrixPropagationLossModel::MatrixPropagationLossModel ()
+  : PropagationLossModel (), m_default (std::numeric_limits<double>::max ())
+{
+}
+
+MatrixPropagationLossModel::~MatrixPropagationLossModel ()
+{
+}
+
+void 
+MatrixPropagationLossModel::SetDefaultLoss (double loss)
+{
+  m_default = loss;
+}
+
+void
+MatrixPropagationLossModel::SetLoss (Ptr<Node> a, Ptr<Node> b, double loss, bool symmetric)
+{
+  Ptr<MobilityModel> ma = a->GetObject<MobilityModel> ();
+  Ptr<MobilityModel> mb = b->GetObject<MobilityModel> ();
+  NS_ASSERT (ma != 0 && mb != 0);
+
+  MobilityPair p = std::make_pair(ma, mb);
+  std::map<MobilityPair, double>::iterator i = m_loss.find (p);
+  
+  if (i == m_loss.end ())
+    {
+      m_loss.insert (std::make_pair (p, loss));
+    }
+  else
+    {
+      i->second = loss;
+    }
+
+  if (symmetric)
+    {
+      SetLoss (b, a, loss, false);
+    }
+}
+
+double 
+MatrixPropagationLossModel::DoCalcRxPower (double txPowerDbm,
+                                           Ptr<MobilityModel> a,
+                                           Ptr<MobilityModel> b) const
+{
+  std::map<MobilityPair, double>::const_iterator i = m_loss.find (std::make_pair (a, b));
+  
+  if (i != m_loss.end ())
+    {
+      return txPowerDbm - i->second;
+    }
+  else
+    {
+      return txPowerDbm - m_default;
+    }
+}
+
+// ------------------------------------------------------------------------- //
+
 } // namespace ns3
--- a/src/common/propagation-loss-model.h	Mon Mar 08 22:36:54 2010 -0500
+++ b/src/common/propagation-loss-model.h	Tue Mar 09 11:35:09 2010 +0300
@@ -19,6 +19,7 @@
  * Contributions: Timo Bingmann <timo.bingmann@student.kit.edu>
  * Contributions: Gary Pei <guangyu.pei@boeing.com> for fixed RSS
  * Contributions: Tom Hewer <tomhewer@mac.com> for two ray ground model
+ *                Pavel Boyko <boyko@iitp.ru> for matrix
  */
 
 #ifndef PROPAGATION_LOSS_MODEL_H
@@ -26,6 +27,8 @@
 
 #include "ns3/object.h"
 #include "ns3/random-variable.h"
+#include "ns3/node.h"
+#include <map>
 
 namespace ns3 {
 
@@ -462,6 +465,44 @@
   double m_rss;
 };
 
+/**
+ * \brief The propagation loss is fixed for each pair of nodes and doesn't depend on their actual positions.
+ * 
+ * This is supposed to be used by synthetic tests. Note that by default propagation loss is assumed to be symmetric.
+ */
+class MatrixPropagationLossModel : public PropagationLossModel
+{
+public:
+  static TypeId GetTypeId (void);
+  
+  MatrixPropagationLossModel ();
+  virtual ~MatrixPropagationLossModel ();
+  
+  /**
+   * \brief Set loss (in dB, positive) between pair of nodes.
+   * 
+   * \param a           Source node
+   * \param b           Destination node
+   * \param loss        a -> b path loss, positive in dB
+   * \param symmetric   If true (default), both a->b and b->a paths will be affected
+   */ 
+  void SetLoss (Ptr<Node> a, Ptr<Node> b, double loss, bool symmetric = true);
+  /// Set default loss (in dB, positive) to be used, \infty if not set
+  void SetDefaultLoss (double);
+  
+private:
+  virtual double DoCalcRxPower (double txPowerDbm,
+                               Ptr<MobilityModel> a,
+                               Ptr<MobilityModel> b) const;
+private:
+  /// default loss
+  double m_default; 
+  
+  typedef std::pair< Ptr<MobilityModel>, Ptr<MobilityModel> > MobilityPair; 
+  /// Fixed loss between pair of nodes
+  std::map<MobilityPair, double> m_loss;
+};
+
 } // namespace ns3
 
 #endif /* PROPAGATION_LOSS_MODEL_H */