From 709c9b767f472d77f8544b8aa3480b81e05cd71e Mon Sep 17 00:00:00 2001 From: Eduardo Almeida Date: Thu, 20 Jun 2024 18:32:01 +0100 Subject: [PATCH] spectrum: Keep results file open and do not append to previous content --- .../examples/three-gpp-ntn-channel-example.cc | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/spectrum/examples/three-gpp-ntn-channel-example.cc b/src/spectrum/examples/three-gpp-ntn-channel-example.cc index 960c435f0..525713882 100644 --- a/src/spectrum/examples/three-gpp-ntn-channel-example.cc +++ b/src/spectrum/examples/three-gpp-ntn-channel-example.cc @@ -58,7 +58,8 @@ using namespace ns3; static Ptr m_propagationLossModel; //!< the PropagationLossModel object static Ptr - m_spectrumLossModel; //!< the SpectrumPropagationLossModel object + m_spectrumLossModel; //!< the SpectrumPropagationLossModel object +static std::ofstream resultsFile; //!< The results file /** * @brief Create the PSD for the TX @@ -298,12 +299,10 @@ ComputeSnr(ComputeSnrParams& params) // compute the SNR NS_LOG_DEBUG("Average SNR " << 10 * log10(Sum(*rxSsp->psd) / Sum(*noisePsd)) << " dB"); - // print the SNR and pathloss values in the ntn-snr-trace.txt file - std::ofstream f; - f.open("ntn-snr-trace.txt", std::ios::out | std::ios::app); - f << Simulator::Now().GetSeconds() << " " << 10 * log10(Sum(*rxSsp->psd) / Sum(*noisePsd)) - << " " << propagationGainDb << std::endl; - f.close(); + // print the SNR and pathloss values in the output file + resultsFile << Simulator::Now().GetSeconds() << " " + << 10 * log10(Sum(*rxSsp->psd) / Sum(*noisePsd)) << " " << propagationGainDb + << std::endl; } int @@ -482,6 +481,10 @@ main(int argc, char* argv[]) DoBeamforming(rxDev, rxAntenna, txDev); DoBeamforming(txDev, txAntenna, rxDev); + // Open the output results file + resultsFile.open("ntn-snr-trace.txt", std::ios::out); + NS_ASSERT_MSG(resultsFile.is_open(), "Results file could not be created"); + for (int i = 0; i < floor(simTimeMs / timeResMs); i++) { Simulator::Schedule(MilliSeconds(timeResMs * i), @@ -499,5 +502,8 @@ main(int argc, char* argv[]) Simulator::Run(); Simulator::Destroy(); + + resultsFile.close(); + return 0; }