Loading...
Searching...
No Matches
satellite-antenna-gain-pattern-container.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013 Magister Solutions Ltd.
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 * Author: Jani Puttonen <jani.puttonen@magister.fi>
19 */
20
22
24
25#include "ns3/log.h"
26#include "ns3/satellite-env-variables.h"
27#include "ns3/singleton.h"
28#include "ns3/string.h"
29
30#include <cmath>
31#include <cstddef>
32#include <dirent.h>
33#include <errno.h>
34#include <fstream>
35#include <limits>
36#include <map>
37#include <sstream>
38#include <string.h>
39#include <string>
40#include <utility>
41
42NS_LOG_COMPONENT_DEFINE("SatAntennaGainPatternContainer");
43
44const std::string numbers{"0123456789"};
45
46namespace ns3
47{
48
49NS_OBJECT_ENSURE_REGISTERED(SatAntennaGainPatternContainer);
50
51TypeId
53{
54 static TypeId tid = TypeId("ns3::SatAntennaGainPatternContainer")
55 .SetParent<Object>()
56 .AddConstructor<SatAntennaGainPatternContainer>();
57 return tid;
58}
59
61{
62 NS_LOG_FUNCTION(this);
63
64 NS_FATAL_ERROR("Constructor not in use");
65}
66
68 std::string patternsFolder)
69{
70 NS_LOG_FUNCTION(this << nbSats << patternsFolder);
71
72 m_patternsFolder = patternsFolder;
73
75
76 NS_LOG_INFO(this << " directory for antenna patterns set to " << m_patternsFolder);
77
78 if (!SatEnvVariables::GetInstance()->IsValidDirectory(m_patternsFolder))
79 {
80 NS_FATAL_ERROR("SatAntennaGainPatternContainer::SatAntennaGainPatternContainer directory "
81 << m_patternsFolder << " not found in antennapatterns folder");
82 }
83
84 DIR* dir;
85 struct dirent* ent;
86 std::string prefix;
87 if ((dir = opendir(m_patternsFolder.c_str())) != nullptr)
88 {
89 /* process all the files and directories within m_patternsFolder */
90 while ((ent = readdir(dir)) != nullptr)
91 {
92 std::string filename{ent->d_name};
93 std::size_t pathLength = filename.length();
94 if (pathLength > 4)
95 {
96 pathLength -= 4; // Size of .txt extention
97 if (filename.substr(pathLength) == ".txt")
98 {
99 std::string num, stem = filename.substr(0, pathLength);
100 std::size_t found = stem.find_last_not_of(numbers);
101 if (found == std::string::npos)
102 {
103 num = stem;
104 stem.erase(0);
105 }
106 else
107 {
108 num = stem.substr(found + 1);
109 stem.erase(found + 1);
110 }
111
112 if (prefix.empty())
113 {
114 prefix = stem;
115 }
116
117 if (prefix != stem)
118 {
119 NS_FATAL_ERROR(
120 "SatAntennaGainPatternContainer::SatAntennaGainPatternContainer mixing "
121 "different prefix for antenna pattern names: "
122 << prefix << " and " << stem);
123 }
124
125 std::string filePath = m_patternsFolder + "/" + filename;
126 std::istringstream ss{num};
127 uint32_t beamId;
128 ss >> beamId;
129 if (ss.bad())
130 {
131 NS_FATAL_ERROR(
132 "SatAntennaGainPatternContainer::SatAntennaGainPatternContainer unable "
133 "to find beam number in "
134 << filePath << " file name");
135 }
136
137 Ptr<SatAntennaGainPattern> gainPattern =
138 CreateObject<SatAntennaGainPattern>(filePath, geoPos);
139 std::pair<std::map<uint32_t, Ptr<SatAntennaGainPattern>>::iterator, bool> ret;
140 ret = m_antennaPatternMap.insert(std::make_pair(beamId, gainPattern));
141
142 if (ret.second == false)
143 {
144 NS_FATAL_ERROR("SatAntennaGainPatternContainer::"
145 "SatAntennaGainPatternContainer an antenna pattern for beam "
146 << beamId << " already exists!");
147 }
148 }
149 }
150 }
151 closedir(dir);
152 }
153 else
154 {
155 /* could not open directory */
156 const char* error = strerror(errno);
157 NS_FATAL_ERROR("SatAntennaGainPatternContainer::SatAntennaGainPatternContainer unable to "
158 "open directory "
159 << m_patternsFolder << ": " << error);
160 }
161}
162
167
170{
171 NS_LOG_FUNCTION(this);
172
173 std::string geoPosFilename = m_patternsFolder + "/GeoPos.in";
174
175 // READ FROM THE SPECIFIED INPUT FILE
176 std::ifstream* ifs = new std::ifstream(geoPosFilename, std::ifstream::in);
177
178 if (!ifs->is_open())
179 {
180 NS_FATAL_ERROR("The file " << geoPosFilename << " is not found.");
181 }
182
183 double lat, lon, alt;
184 *ifs >> lat >> lon >> alt;
185
186 // Store the values
187 GeoCoordinate coord(lat, lon, alt);
188
189 ifs->close();
190 delete ifs;
191
192 return coord;
193}
194
195Ptr<SatAntennaGainPattern>
197{
198 NS_LOG_FUNCTION(this << beamId);
199
200 std::map<uint32_t, Ptr<SatAntennaGainPattern>>::const_iterator agp =
201 m_antennaPatternMap.find(beamId);
202 if (agp == m_antennaPatternMap.end())
203 {
204 NS_FATAL_ERROR(
205 "SatAntennaGainPatternContainer::GetAntennaGainPattern - unvalid beam id: " << beamId);
206 }
207
208 return agp->second;
209}
210
211Ptr<SatMobilityModel>
213{
214 NS_LOG_FUNCTION(this << satelliteId);
215
216 std::map<uint32_t, Ptr<SatMobilityModel>>::const_iterator mm =
217 m_mobilityModelMap.find(satelliteId);
218 if (mm == m_mobilityModelMap.end())
219 {
220 NS_FATAL_ERROR(
221 "SatAntennaGainPatternContainer::GetAntennaGainPattern - unvalid satellite id: "
222 << satelliteId);
223 }
224
225 return mm->second;
226}
227
228uint32_t
230 GeoCoordinate coord,
231 bool ignoreNan)
232{
233 NS_LOG_FUNCTION(this << satelliteId << coord.GetLatitude() << coord.GetLongitude()
234 << ignoreNan);
235
236 double bestGain(-100.0);
237 uint32_t bestId(0);
238
239 Ptr<SatMobilityModel> mobility = m_mobilityModelMap[satelliteId];
240
241 for (const auto& entry : m_antennaPatternMap)
242 {
243 uint32_t i = entry.first;
244 double gain = entry.second->GetAntennaGain_lin(coord, mobility);
245
246 // The antenna pattern has returned a NAN gain. This means
247 // that this position is not valid. Return 0, which is not a valid beam id.
248 if (std::isnan(gain))
249 {
250 if (ignoreNan)
251 {
252 NS_LOG_WARN("SatAntennaGainPatternContainer::GetBestBeamId - Beam "
253 << i << " returned a NAN antenna gain value!");
254 }
255 else
256 {
257 NS_FATAL_ERROR("SatAntennaGainPatternContainer::GetBestBeamId - Beam "
258 << i << " returned a NAN antenna gain value!");
259 }
260 }
261 else if (gain > bestGain)
262 {
263 bestGain = gain;
264 bestId = i;
265 }
266 }
267
268 if (bestId == 0 && ignoreNan)
269 {
270 NS_LOG_WARN(
271 "SatAntennaGainPatternContainer::GetBestBeamId - did not find any good beam! The "
272 "ground station is probably too far from the satellite. Return 0 by default.");
273 }
274
275 return bestId;
276}
277
278double
280 uint32_t beamId,
281 GeoCoordinate coord)
282{
283 NS_LOG_FUNCTION(this << satelliteId << beamId << coord.GetLatitude() << coord.GetLongitude());
284
285 Ptr<SatMobilityModel> mobility = m_mobilityModelMap[satelliteId];
286
287 if (m_antennaPatternMap.find(beamId) == m_antennaPatternMap.end())
288 {
289 return std::numeric_limits<double>::quiet_NaN();
290 }
291 Ptr<SatAntennaGainPattern> pattern = m_antennaPatternMap.at(beamId);
292
293 return pattern->GetAntennaGain_lin(coord, mobility);
294}
295
296uint32_t
298{
299 NS_LOG_FUNCTION(this);
300
301 // Note, that now we assume that all the antenna patterns are created
302 // regardless of how many beams are actually simulated.
303 return m_antennaPatternMap.size();
304}
305
306void
308 Ptr<SatMobilityModel> mobility)
309{
310 NS_LOG_FUNCTION(this << satelliteId << mobility);
311
312 m_mobilityModelMap[satelliteId] = mobility;
313}
314
315void
317{
318 std::map<uint32_t, Ptr<SatAntennaGainPattern>>::iterator it = m_antennaPatternMap.begin();
319 while (it != m_antennaPatternMap.end())
320 {
321 uint32_t beamIdIterator = it->first;
322 BeamUserInfoMap_t::iterator infoIterator;
323 bool found = false;
324 for (infoIterator = info.begin(); infoIterator != info.end(); infoIterator++)
325 {
326 uint32_t beamId = infoIterator->first.second;
327 if (beamId == beamIdIterator)
328 {
329 found = true;
330 }
331 }
332 if (!found)
333 {
334 it = m_antennaPatternMap.erase(it);
335 }
336 else
337 {
338 it++;
339 }
340 }
341}
342
343} // namespace ns3
GeoCoordinate class is used to store and operate with geodetic coordinates.
double GetLatitude() const
Gets latitude value of coordinate.
double GetLongitude() const
Gets longitude value of coordinate.
Antenna gain pattern container holds all antenna patterns related to a satellite system.
double GetBeamGain(uint32_t satelliteId, uint32_t beamId, GeoCoordinate coord)
Get beam gain for given coordinates.
Ptr< SatAntennaGainPattern > GetAntennaGainPattern(uint32_t beamId) const
Get the antenna pattern of a specified beam id.
GeoCoordinate GetDefaultGeoPosition()
Load the default satellite position associated to these traces.
uint32_t GetBestBeamId(uint32_t satelliteId, GeoCoordinate coord, bool ignoreNan)
Get the best beam id based on the antenna patterns in a specified geo coordinate.
std::map< uint32_t, Ptr< SatAntennaGainPattern > > m_antennaPatternMap
Container of antenna patterns.
uint32_t GetNAntennaGainPatterns() const
Get the number of stored antenna pattern.
std::map< std::pair< uint32_t, uint32_t >, SatBeamUserInfo > BeamUserInfoMap_t
definition for beam map key is pair sat ID / beam ID and value is UT/user info.
void ConfigureBeamsMobility(uint32_t satelliteId, Ptr< SatMobilityModel > mobility)
Ptr< SatMobilityModel > GetAntennaMobility(uint32_t satelliteId) const
Get the mobility model of a specified satellite id.
std::map< uint32_t, Ptr< SatMobilityModel > > m_mobilityModelMap
Container of mobility models.
static Ptr< SatEnvVariables > GetInstance()
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.
const std::string numbers