Esempio n. 1
0
/**
 * Attaches the relevant prevalence chart to each herd structure.
 *
 * @param self the model.
 * @param herds the herd list.
 */
void
attach_prevalence_charts (struct naadsm_model_t_ *self,
			  HRD_herd_list_t *herds)
{
  local_data_t *local_data;
  HRD_herd_t *herd;
  unsigned int nherds;
  unsigned int i;

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
	 "----- ENTER attach_prevalence_charts (%s)", MODEL_NAME);
#endif

  local_data = (local_data_t *) (self->model_data);
  
  nherds = HRD_herd_list_length (herds);
  for (i = 0; i < nherds; i++)
    {
      herd = HRD_herd_list_get (herds, i);
      if (local_data->production_type[herd->production_type] == TRUE)
	herd->prevalence_curve = local_data->prevalence;
    }

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
	 "----- EXIT attach_prevalence_charts (%s)", MODEL_NAME);
#endif
  return;
}
Esempio n. 2
0
/**
 * Responds to a new day event by releasing any pending infections and
 * stochastically generating infections.
 *
 * @param self the model.
 * @param herds a list of herds.
 * @param event a new day event.
 * @param rng a random number generator.
 * @param queue for any new events the model creates.
 */
void
handle_new_day_event (struct ergadm_model_t_ *self, HRD_herd_list_t * herds,
                      EVT_new_day_event_t * event, RAN_gen_t * rng, EVT_event_queue_t * queue)
{
  local_data_t *local_data;
  HRD_herd_t *herd1;
  unsigned int nherds;          /* number of herds */
  unsigned int herd1_index, herd2_index;
  gboolean herd1_can_be_source;
  double distance;
  GQueue *q;
  EVT_event_t *pending_event;
  struct Rect search_rect;      /* for narrowing down radius searches using the
                                   R-tree (spatial index) */
  double mult;                  /* to account for latitude */
  callback_t callback_data;
#if DEBUG
  GString *s;
#endif

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- ENTER handle_new_day_event (%s)", MODEL_NAME);
#endif

  local_data = (local_data_t *) (self->model_data);

  /* Release any pending (due to airborne transport delays) events. */
  local_data->rotating_index =
    (local_data->rotating_index + 1) % local_data->pending_infections->len;
  q = (GQueue *) g_ptr_array_index (local_data->pending_infections, local_data->rotating_index);
  while (!g_queue_is_empty (q))
    {
      /* Remove the event from this model's internal queue and place it in the
       * simulation's event queue. */
      pending_event = (EVT_event_t *) g_queue_pop_head (q);
      EVT_event_enqueue (queue, pending_event);
      local_data->npending_infections--;
    }

  if (
#if defined(USE_RTREE) && USE_RTREE == 1
       /* For debugging purposes, you can #define USE_RTREE to 0 to never use
        * the spatial index, or 1 to always use it. */
       TRUE ||
#endif
       local_data->use_rtree_index)
    {
      /* Initialize a data structure used by the callback function. */
      callback_data.self = self;
      callback_data.herds = herds;
      callback_data.event = event;
      callback_data.rng = rng;
      callback_data.queue = queue;
    }

  nherds = HRD_herd_list_length (herds);
  for (herd1_index = 0; herd1_index < nherds; herd1_index++)
    {
      herd1 = HRD_herd_list_get (herds, herd1_index);

      /* Can this herd be the source of an exposure? */
#if DEBUG
      s = g_string_new (NULL);
      g_string_sprintf (s, "unit \"%s\" is %s, state is %s: ",
                        herd1->official_id, herd1->production_type_name,
                        HRD_status_name[herd1->status]);
#endif
      herd1_can_be_source =
        local_data->param_block[herd1->production_type] != NULL
        && (herd1->status == InfectiousSubclinical || herd1->status == InfectiousClinical);
#if DEBUG
      g_string_sprintfa (s, "%s be source", herd1_can_be_source ? "can" : "cannot");
      g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", s->str);
      g_string_free (s, TRUE);
#endif
      if (!herd1_can_be_source)
        continue;

      if (local_data->use_rtree_index[herd1->production_type])
        {
          distance = local_data->max_spread[herd1->production_type] / GIS_DEGREE_DISTANCE;
          mult = 1.0 / MIN (cos (DEG2RAD * (herd1->lat + distance)), cos(DEG2RAD * (herd1->lat - distance))); 
          search_rect.boundary[0] = herd1->lon - (distance * mult) - EPSILON;
          search_rect.boundary[1] = herd1->lat - distance - EPSILON;
          search_rect.boundary[2] = herd1->lon + (distance * mult) + EPSILON;
          search_rect.boundary[3] = herd1->lat + distance + EPSILON;
          callback_data.herd1 = herd1;
          RTreeSearch (herds->spatial_index, &search_rect, callback, &callback_data);
        }
      else
        for (herd2_index = 0; herd2_index < nherds; herd2_index++)
          check_and_infect (self, herds, herd1,
                            HRD_herd_list_get (herds, herd2_index), event, rng, queue);
    }

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- EXIT handle_new_day_event (%s)", MODEL_NAME);
#endif

  return;
}
Esempio n. 3
0
int
main (int argc, char *argv[])
{
  int verbosity = 0;
  const char *herd_file = NULL;
  unsigned int nherds;
  HRD_herd_t *herd;
  int i;                        /* loop counter */
  char *summary;
  GError *option_error = NULL;
  GOptionContext *context;
  GOptionEntry options[] = {
    { "herd-file", 'h', 0, G_OPTION_ARG_FILENAME, &herd_file, "SpreadModel 3.0 herd file", NULL },
    { "verbosity", 'V', 0, G_OPTION_ARG_INT, &verbosity, "Message verbosity level (0 = simulation output only, 1 = all debugging output)", NULL },
    { NULL }
  };

  context = g_option_context_new ("");
  g_option_context_add_main_entries (context, options, /* translation = */ NULL);
  if (!g_option_context_parse (context, &argc, &argv, &option_error))
    {
      g_error ("option parsing failed: %s\n", option_error->message);
    }
  g_option_context_free (context);

  /* Set the verbosity level. */
  if (verbosity < 1)
    {
      g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, silent_log_handler, NULL);
      g_log_set_handler ("herd", G_LOG_LEVEL_DEBUG, silent_log_handler, NULL);
    }

  /* Get the list of herds. */
  herds = HRD_new_herd_list ();
  if (herd_file)
    yyin = fopen (herd_file, "r");
  else if (yyin == NULL)
    yyin = stdin;
  while (!feof (yyin))
    yyparse ();
  if (yyin != stdin)
    fclose (yyin);
  nherds = HRD_herd_list_length (herds);

#if DEBUG
  g_debug ("%i herds read", nherds);
  summary = HRD_herd_list_to_string (herds);
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "\n%s", summary);
  free (summary);
#endif

  printf ("<herds>\n");
  for (i = 0; i < nherds; i++)
    {
      herd = HRD_herd_list_get (herds, i);
      printf ("  <herd>\n");
      printf ("    <id>%s</id>\n", herd->official_id);
      printf ("    <production-type>%u</production-type>\n", herd->production_type);
      printf ("    <size>%u</size>\n", herd->size);
      printf ("    <location>\n");
      printf ("      <latitude>%g</latitude>\n", herd->y);
      printf ("      <longitude>%g</longitude>\n", herd->x);
      printf ("    </location>\n");
      printf ("    <status>%s</status>\n", HRD_status_name[herd->status]);
      printf ("  </herd>\n");
    }
  printf ("</herds>\n");

  /* Clean up. */
  HRD_free_herd_list (herds);

  return EXIT_SUCCESS;
}
Esempio n. 4
0
/**
 * Computes the size factor for each herd.
 *
 * @param herds a list of herds.
 * @return an array containing the size factor for each herd.
 */
double *
build_size_factor_list (HRD_herd_list_t * herds)
{
  HRD_herd_t *herd;
  unsigned int nherds;          /* number of herds */
  unsigned int max_size = 0;    /* size of largest herd */
  gsl_histogram *histogram;
  PDF_dist_t *herd_size_dist;
  double *size_factor;
  unsigned int i;               /* loop counter */
#if DEBUG
  char *s;
#endif

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- ENTER build_size_factor_list");
#endif

  nherds = HRD_herd_list_length (herds);
  size_factor = g_new (double, nherds);

  /* Find the largest herd. */
  for (i = 0; i < nherds; i++)
    {
      herd = HRD_herd_list_get (herds, i);
      if (herd->size > max_size)
        max_size = herd->size;
    }
#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "largest herd = %u", max_size);
#endif

  /* Build a histogram with one bin for each herd size. */
  histogram = gsl_histogram_alloc (max_size);
  g_assert (histogram != NULL);
  gsl_histogram_set_ranges_uniform (histogram, 0.5, (double) max_size + 0.5);
  for (i = 0; i < nherds; i++)
    {
      herd = HRD_herd_list_get (herds, i);
      gsl_histogram_increment (histogram, (double) (herd->size));
    }
  herd_size_dist = PDF_new_histogram_dist (histogram);
  g_assert (herd_size_dist != NULL);

#if DEBUG
  s = PDF_dist_to_string (herd_size_dist);
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "herd size distribution =\n%s", s);
  free (s);
#endif

  /* Compute the herd size factors. */
  for (i = 0; i < nherds; i++)
    {
      herd = HRD_herd_list_get (herds, i);
      size_factor[i] = PDF_cdf (herd->size, herd_size_dist) * 2;
    }

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "herd size factors");
  for (i = 0; i < nherds; i++)
    {
      herd = HRD_herd_list_get (herds, i);
      g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "herd #%u (size %u) = %g",
             i, herd->size, size_factor[i]);
    }
#endif

  /* The following line also frees the histogram. */
  PDF_free_dist (herd_size_dist);

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- EXIT build_size_factor_list");
#endif

  return size_factor;
}