propagation.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the Computer Systems
00017  *  Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  * 
00034  propagation.cc
00035  $Id: propagation.cc,v 1.7 2005/02/03 20:15:00 haldar Exp $
00036 */
00037 
00038 #include <stdio.h>
00039 
00040 #include <topography.h>
00041 #include <propagation.h>
00042 #include <wireless-phy.h>
00043 
00044 class PacketStamp;
00045 int
00046 Propagation::command(int argc, const char*const* argv)
00047 {
00048   TclObject *obj;  
00049 
00050   if(argc == 3) 
00051     {
00052       if( (obj = TclObject::lookup(argv[2])) == 0) 
00053     {
00054       fprintf(stderr, "Propagation: %s lookup of %s failed\n", argv[1],
00055           argv[2]);
00056       return TCL_ERROR;
00057     }
00058 
00059       if (strcasecmp(argv[1], "topography") == 0) 
00060     {
00061       topo = (Topography*) obj;
00062       return TCL_OK;
00063     }
00064     }
00065   return TclObject::command(argc,argv);
00066 }
00067  
00068 
00069 /* As new network-intefaces are added, add a default method here */
00070 
00071 double
00072 Propagation::Pr(PacketStamp *, PacketStamp *, Phy *)
00073 {
00074     fprintf(stderr,"Propagation model %s not implemented for generic NetIF\n",
00075       name);
00076     abort();
00077     return 0; // Make msvc happy
00078 }
00079 
00080 
00081 double
00082 Propagation::Pr(PacketStamp *, PacketStamp *, WirelessPhy *)
00083 {
00084     fprintf(stderr,
00085         "Propagation model %s not implemented for SharedMedia interface\n",
00086         name);
00087     abort();
00088     return 0; // Make msvc happy
00089 }
00090 
00091 double
00092 Propagation::getDist(double Pr, double Pt, double Gt, double Gr, double hr,
00093              double ht, double L, double lambda)
00094 {
00095     fprintf(stderr,
00096         "Propagtion model %s not implemented for generic use\n", name);
00097     abort();
00098     return 0;
00099 }
00100 
00101 double
00102 Propagation::Friis(double Pt, double Gt, double Gr, double lambda, double L, double d)
00103 {
00104         /*
00105          * Friis free space equation:
00106          *
00107          *       Pt * Gt * Gr * (lambda^2)
00108          *   P = --------------------------
00109          *       (4 * pi * d)^2 * L
00110          */
00111     if (d == 0.0) //XXX probably better check < MIN_DISTANCE or some such
00112         return Pt;
00113   double M = lambda / (4 * PI * d);
00114   return (Pt * Gt * Gr * (M * M)) / L;
00115 }
00116 
00117 
00118 // methods for free space model
00119 static class FreeSpaceClass: public TclClass {
00120 public:
00121     FreeSpaceClass() : TclClass("Propagation/FreeSpace") {}
00122     TclObject* create(int, const char*const*) {
00123         return (new FreeSpace);
00124     }
00125 } class_freespace;
00126 
00127 
00128 double FreeSpace::Pr(PacketStamp *t, PacketStamp *r, WirelessPhy *ifp)
00129 {
00130     double L = ifp->getL();     // system loss
00131     double lambda = ifp->getLambda();   // wavelength
00132 
00133     double Xt, Yt, Zt;      // location of transmitter
00134     double Xr, Yr, Zr;      // location of receiver
00135 
00136     t->getNode()->getLoc(&Xt, &Yt, &Zt);
00137     r->getNode()->getLoc(&Xr, &Yr, &Zr);
00138 
00139     // Is antenna position relative to node position?
00140     Xr += r->getAntenna()->getX();
00141     Yr += r->getAntenna()->getY();
00142     Zr += r->getAntenna()->getZ();
00143     Xt += t->getAntenna()->getX();
00144     Yt += t->getAntenna()->getY();
00145     Zt += t->getAntenna()->getZ();
00146 
00147     double dX = Xr - Xt;
00148     double dY = Yr - Yt;
00149     double dZ = Zr - Zt;
00150     double d = sqrt(dX * dX + dY * dY + dZ * dZ);
00151 
00152     // get antenna gain
00153     double Gt = t->getAntenna()->getTxGain(dX, dY, dZ, lambda);
00154     double Gr = r->getAntenna()->getRxGain(dX, dY, dZ, lambda);
00155 
00156     // calculate receiving power at distance
00157     double Pr = Friis(t->getTxPr(), Gt, Gr, lambda, L, d);
00158     printf("%lf: d: %lf, Pr: %e\n", Scheduler::instance().clock(), d, Pr);
00159 
00160     return Pr;
00161 }
00162 
00163 double
00164 FreeSpace::getDist(double Pr, double Pt, double Gt, double Gr, double hr, double ht, double L, double lambda)
00165 {
00166         return sqrt((Pt * Gt * Gr * lambda * lambda) / (L * Pr)) /
00167                 (4 * PI);
00168 }
00169 

Generated on Tue Mar 6 16:47:49 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6