grid.c 6.15 KB
Newer Older
1
/*******************************************************************************
nikaeinn's avatar
nikaeinn committed
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

nikaeinn's avatar
nikaeinn committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


nikaeinn's avatar
nikaeinn committed
11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

nikaeinn's avatar
nikaeinn committed
16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
nikaeinn's avatar
nikaeinn committed
22 23 24
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
25

nikaeinn's avatar
nikaeinn committed
26
  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
27

nikaeinn's avatar
nikaeinn committed
28
*******************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
/*! \file grid.c
* \brief 
* \author S. Gashaw, N. Nikaein, J. Harri
* \date 2014
* \version 0.1
* \company Eurecom
* \email: 
* \note
* \warning 
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "grid.h"
#define   LEFT 0
#define   RIGHT 1
#define   UPPER 2
#define   LOWER 3

int
max_vertices_ongrid (omg_global_param omg_param)
{
  return ((int) (omg_param.max_x / xloc_div + 1.0) *
	  (int) (omg_param.max_y / yloc_div + 1.0)) - 1;
}

int
max_connecteddomains_ongrid (omg_global_param omg_param)
{
  return ((int) (omg_param.max_x / xloc_div) *
	  (int) (omg_param.max_y / yloc_div)) - 1;
}

double
vertice_xpos (int loc_num, omg_global_param omg_param)
{
  int div, mod;
  double x_pos;

  div = (int) (omg_param.max_x / xloc_div + 1.0);
  mod = (loc_num % div) * xloc_div;
  x_pos = omg_param.min_x + (double) mod;
  //LOG_D(OMG,"mod %d div %d x pos %.2f \n\n",mod,div,x_pos);
  return x_pos;
}

double
vertice_ypos (int loc_num, omg_global_param omg_param)
{
  //LOG_D(OMG,"y pos %.2f \n\n",omg_param.min_y + (double)yloc_div * (int)( loc_num / (int)(omg_param.max_x/xloc_div + 1.0) ));
  return omg_param.min_y +
    (double) yloc_div *(int) (loc_num /
			      (int) (omg_param.max_x / xloc_div + 1.0));
}

double
area_minx (int block_num, omg_global_param omg_param)
{
  return omg_param.min_x +
    xloc_div * (block_num % (int) (omg_param.max_x / xloc_div));

}

double
area_miny (int block_num, omg_global_param omg_param)
{
  return omg_param.min_y +
    yloc_div * (int) (block_num / (int) (omg_param.max_x / xloc_div));
}

/*for connected domain that only move to the neighbor domains */

unsigned int
next_block (int current_bn, omg_global_param omg_param)
{

  double rnd = randomgen (0, 1);
  unsigned int blk;
  int div = (int) (omg_param.max_x / xloc_div);

/*left border blocks*/
  if ((current_bn % div) == 0)
    {
      /*case 1 for left upper and lower corners(only 2 neighbors) */
      if ((int) (current_bn / div) == 0)
	{
	  if (rnd <= 0.5)
	    blk = selected_blockn (current_bn, RIGHT, div);
	  else
	    blk = selected_blockn (current_bn, UPPER, div);
	}
      else if ((int) (current_bn / div) ==
	       (int) (omg_param.max_y / yloc_div) - 1)
	{
	  if (rnd <= 0.5)
	    blk = selected_blockn (current_bn, RIGHT, div);
	  else
	    blk = selected_blockn (current_bn, LOWER, div);
	}
      /*for 3 neighbor blocks */
      else
	{
	  if (rnd <= 0.33)
	    blk = selected_blockn (current_bn, RIGHT, div);
	  else if (rnd > 0.33 && rnd <= 0.66)
	    blk = selected_blockn (current_bn, UPPER, div);
	  else
	    blk = selected_blockn (current_bn, LOWER, div);
	}

    }
/*right boredr blocks*/
  else if ((current_bn % (int) (omg_param.max_x / xloc_div)) == div - 1)
    {
      /*case 1 for right upper and lower corners(only 2 neighbors) */
      if ((int) (current_bn / div) == 0)
	{
	  if (rnd <= 0.5)
	    blk = selected_blockn (current_bn, LEFT, div);
	  else
	    blk = selected_blockn (current_bn, UPPER, div);
	}
      else if ((int) (current_bn / div) ==
	       (int) (omg_param.max_y / yloc_div) - 1)
	{
	  if (rnd <= 0.5)
	    blk = selected_blockn (current_bn, LEFT, div);
	  else
	    blk = selected_blockn (current_bn, LOWER, div);
	}
      /*for 3 neighbor blocks */
      else
	{
	  if (rnd <= 0.33)
	    blk = selected_blockn (current_bn, LEFT, div);
	  else if (rnd > 0.33 && rnd <= 0.66)
	    blk = selected_blockn (current_bn, UPPER, div);
	  else
	    blk = selected_blockn (current_bn, LOWER, div);
	}


    }
/*for 3 neighbor uper and lower borders*/
  else if ((int) (current_bn / div) == 0
	   || (int) (current_bn / div) ==
	   (int) (omg_param.max_y / yloc_div) - 1)
    {

      if ((int) (current_bn / div) == 0)
	{
	  if (rnd <= 0.33)
	    blk = selected_blockn (current_bn, LEFT, div);
	  else if (rnd > 0.33 && rnd <= 0.66)
	    blk = selected_blockn (current_bn, RIGHT, div);
	  else
	    blk = selected_blockn (current_bn, UPPER, div);
	}

      else
	{
	  if (rnd <= 0.33)
	    blk = selected_blockn (current_bn, LEFT, div);
	  else if (rnd > 0.33 && rnd <= 0.66)
	    blk = selected_blockn (current_bn, RIGHT, div);
	  else
	    blk = selected_blockn (current_bn, LOWER, div);
	}

    }
  else
    {
      if (rnd <= 0.25)
	blk = selected_blockn (current_bn, LEFT, div);
      else if (rnd > 0.25 && rnd <= 0.50)
	blk = selected_blockn (current_bn, RIGHT, div);
      else if (rnd > 0.50 && rnd <= 0.75)
	blk = selected_blockn (current_bn, UPPER, div);
      else
	blk = selected_blockn (current_bn, LOWER, div);
    }


  return blk;

}

/*retun the block number of neighbor selected for next move */
unsigned int
selected_blockn (int block_n, int type, int div)
{
  unsigned int next_blk = 0;
  switch (type)
    {
    case LEFT:
      next_blk = block_n - 1;
      break;
    case RIGHT:
      next_blk = block_n + 1;
      break;
    case UPPER:
      next_blk = block_n + div;
      break;
    case LOWER:
      next_blk = block_n - div;
      break;
    default:
      LOG_E (OMG, "wrong type input\n");
    }
  return next_blk;

}