1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3 *   Copyright (c) 2020 University of Padova, Dep. of Information Engineering, SIGNET lab.
4 *
5 *   This program is free software; you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License version 2 as
7 *   published by the Free Software Foundation;
8 *
9 *   This program is distributed in the hope that it will be useful,
10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *   GNU General Public License for more details.
13 *
14 *   You should have received a copy of the GNU General Public License
15 *   along with this program; if not, write to the Free Software
16 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 
19 #include "phased-array-model.h"
20 #include <ns3/isotropic-antenna-model.h>
21 #include <ns3/log.h>
22 #include <ns3/double.h>
23 #include <ns3/uinteger.h>
24 #include <ns3/boolean.h>
25 #include <ns3/pointer.h>
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("PhasedArrayModel");
30 
31 NS_OBJECT_ENSURE_REGISTERED (PhasedArrayModel);
32 
33 std::ostream&
operator <<(std::ostream & os,const PhasedArrayModel::ComplexVector & cv)34 operator<< (std::ostream& os, const PhasedArrayModel::ComplexVector& cv)
35 {
36   size_t N = cv.size ();
37 
38   // empty
39   if (N == 0)
40     {
41       os << "[]";
42       return os;
43     }
44 
45   // non-empty
46   os << "[";
47   for (std::size_t i = 0; i < N - 1; ++i)
48     {
49       os << cv[i] << ", ";
50     }
51   os << cv[N - 1] << "]";
52   return os;
53 }
54 
PhasedArrayModel()55 PhasedArrayModel::PhasedArrayModel ()
56   : m_isBfVectorValid {false}
57 {}
58 
59 
~PhasedArrayModel()60 PhasedArrayModel::~PhasedArrayModel ()
61 {
62   m_beamformingVector.clear ();
63 }
64 
65 
66 TypeId
GetTypeId()67 PhasedArrayModel::GetTypeId ()
68 {
69   static TypeId tid = TypeId ("ns3::PhasedArrayModel")
70     .SetParent<Object> ()
71     .SetGroupName ("Antenna")
72     .AddAttribute ("AntennaElement",
73                    "A pointer to the antenna element used by the phased array",
74                    PointerValue (CreateObject<IsotropicAntennaModel> ()),
75                    MakePointerAccessor (&PhasedArrayModel::m_antennaElement),
76                    MakePointerChecker<AntennaModel> ())
77   ;
78   return tid;
79 }
80 
81 
82 void
SetBeamformingVector(const ComplexVector & beamformingVector)83 PhasedArrayModel::SetBeamformingVector (const ComplexVector &beamformingVector)
84 {
85   NS_LOG_FUNCTION (this << beamformingVector);
86   NS_ASSERT_MSG (beamformingVector.size () == GetNumberOfElements (),
87                  beamformingVector.size () << " != " << GetNumberOfElements ());
88   m_beamformingVector = beamformingVector;
89   m_isBfVectorValid = true;
90 }
91 
92 
93 PhasedArrayModel::ComplexVector
GetBeamformingVector() const94 PhasedArrayModel::GetBeamformingVector () const
95 {
96   NS_LOG_FUNCTION (this);
97   NS_ASSERT_MSG (m_isBfVectorValid, "The beamforming vector should be Set before it's Get, and should refer to the current array configuration");
98   return m_beamformingVector;
99 }
100 
101 
102 double
ComputeNorm(const ComplexVector & vector)103 PhasedArrayModel::ComputeNorm (const ComplexVector &vector)
104 {
105   double norm = 0;
106 
107   for (uint64_t i = 0; i < vector.size (); i++)
108     {
109       norm += std::norm (vector[i]);
110     }
111 
112   return std::sqrt (norm);
113 
114 }
115 
116 
117 PhasedArrayModel::ComplexVector
GetBeamformingVector(Angles a) const118 PhasedArrayModel::GetBeamformingVector (Angles a) const
119 {
120   NS_LOG_FUNCTION (this << a);
121 
122   ComplexVector beamformingVector = GetSteeringVector (a);
123   double norm = ComputeNorm (beamformingVector);
124 
125   for (uint64_t i = 0; i < beamformingVector.size (); i++)
126     {
127       beamformingVector[i] = std::conj (beamformingVector[i]) / norm;
128     }
129 
130   return beamformingVector;
131 }
132 
133 
134 
135 PhasedArrayModel::ComplexVector
GetSteeringVector(Angles a) const136 PhasedArrayModel::GetSteeringVector (Angles a) const
137 {
138   ComplexVector steeringVector;
139   steeringVector.resize (GetNumberOfElements ());
140   for (uint64_t i = 0; i < GetNumberOfElements (); i++)
141     {
142       Vector loc = GetElementLocation (i);
143       double phase = -2 * M_PI * (sin (a.GetInclination ()) * cos (a.GetAzimuth ()) * loc.x +
144                                   sin (a.GetInclination ()) * sin (a.GetAzimuth ()) * loc.y +
145                                   cos (a.GetInclination ()) * loc.z);
146       steeringVector[i] = std::polar<double> (1.0, phase);
147     }
148   return steeringVector;
149 }
150 
151 
152 void
SetAntennaElement(Ptr<AntennaModel> antennaElement)153 PhasedArrayModel::SetAntennaElement (Ptr<AntennaModel> antennaElement)
154 {
155   NS_LOG_FUNCTION (this);
156   m_antennaElement = antennaElement;
157 }
158 
159 
160 Ptr<const AntennaModel>
GetAntennaElement() const161 PhasedArrayModel::GetAntennaElement () const
162 {
163   NS_LOG_FUNCTION (this);
164   return m_antennaElement;
165 }
166 
167 
168 } /* namespace ns3 */
169 
170