SimplePathlossModel.cc

00001 #include "SimplePathlossModel.h"
00002 
00003 #define splmEV (ev.isDisabled()||!debug) ? ev : ev << "PhyLayer(SimplePathlossModel): "
00004 
00005 SimplePathlossConstMapping::SimplePathlossConstMapping(const DimensionSet& dimensions,
00006                              SimplePathlossModel* model,
00007                              const double distFactor) :
00008   SimpleConstMapping(dimensions),
00009   distFactor(distFactor),
00010   model(model),
00011   hasFrequency(dimensions.hasDimension(Dimension::frequency_static()))
00012 {
00013 }
00014 
00015 double SimplePathlossConstMapping::getValue(const Argument& pos) const
00016 {
00017   double freq = model->carrierFrequency;
00018   if(hasFrequency) {
00019     assert(pos.hasArgVal(Dimension::frequency));
00020     freq = pos.getArgValue(Dimension::frequency);
00021   }
00022   double wavelength = BaseWorldUtility::speedOfLight / freq;
00023   return (wavelength * wavelength) * distFactor;
00024 }
00025 
00026 
00027 
00028 void SimplePathlossModel::filterSignal(Signal& s){
00029 
00031   simtime_t sStart = s.getSignalStart();
00032   simtime_t sEnd = s.getSignalLength() + sStart;
00033 
00035   Coord sendersPos = s.getMove().getPositionAt(sStart);
00036   Coord myPos = myMove.getPositionAt(sStart);
00037 
00039   double sqrDistance = useTorus ? myPos.sqrTorusDist(sendersPos, playgroundSize)
00040                   : myPos.sqrdist(sendersPos);
00041 
00042   splmEV << "sqrdistance is: " << sqrDistance << endl;
00043 
00044   if(sqrDistance <= 1.0) {
00045     //attenuation is negligible
00046     return;
00047   }
00048 
00049   // wavelength in meters (this is only used for debug purposes here
00050   // the actual effect of the wavelength on the attenuation is
00051   // calculated in SimplePathlossConstMappings "getValue()" method).
00052   double wavelength = (BaseWorldUtility::speedOfLight/carrierFrequency);
00053   splmEV << "wavelength is: " << wavelength << endl;
00054 
00055   // the part of the attenuation only depending on the distance
00056   double distFactor = pow(sqrDistance, -pathLossAlphaHalf) / (16.0 * M_PI * M_PI);
00057   splmEV << "distance factor is: " << distFactor << endl;
00058 
00059   //is our signal to attenuate defined over frequency?
00060   bool hasFrequency = s.getTransmissionPower()->getDimensionSet().hasDimension(Dimension::frequency);
00061   splmEV << "Signal contains frequency dimension: " << (hasFrequency ? "yes" : "no") << endl;
00062 
00063   const DimensionSet& domain = hasFrequency ? DimensionSet::timeFreqDomain : DimensionSet::timeDomain;
00064 
00065   //create the Attenuation mapping which takes the distance factor as parameter
00066   //to calculate the attenuation from this and the frequency used for the transmission
00067   //see the classes "getValue()" for more
00068   SimplePathlossConstMapping* attMapping = new SimplePathlossConstMapping(
00069                           domain,
00070                           this,
00071                           distFactor);
00072 
00073   /* at last add the created attenuation mapping to the signal */
00074   s.addAttenuation(attMapping);
00075 }
00076 
00077 double SimplePathlossModel::calcPathloss(const Coord& myPos, const Coord& sendersPos)
00078 {
00079   /*
00080    * maybe we can reuse an already calculated value for the square-distance
00081    * at this point.
00082    *
00083    */
00084   double sqrdistance = 0.0;
00085 
00086   if (useTorus)
00087   {
00088     sqrdistance = myPos.sqrTorusDist(sendersPos, playgroundSize);
00089   } else
00090   {
00091     sqrdistance = myPos.sqrdist(sendersPos);
00092   }
00093 
00094   splmEV << "sqrdistance is: " << sqrdistance << endl;
00095 
00096   double attenuation = 1.0;
00097   // wavelength in metres
00098   double wavelength = (BaseWorldUtility::speedOfLight/carrierFrequency);
00099 
00100   splmEV << "wavelength is: " << wavelength << endl;
00101 
00102   if (sqrdistance > 1.0)
00103   {
00104     attenuation = (wavelength * wavelength) / (16.0 * M_PI * M_PI)
00105             * (pow(sqrdistance, -1.0*pathLossAlphaHalf));
00106   }
00107 
00108   splmEV << "attenuation is: " << attenuation << endl;
00109 
00110   return attenuation;
00111 }