Beispiel #1
0
void
scew_element_list_del(scew_element* element, XML_Char const* name)
{
    unsigned int i = 0;
    unsigned int count = 0;
    scew_element** list = NULL;

    if ((element == NULL) || (name == NULL))
    {
        return;
    }

    list = scew_element_list(element, name, &count);
    if (list == NULL)
    {
        return;
    }

    for (i = 0; i < count; i++)
    {
        scew_element_free(list[i]);
    }
    scew_element_list_free(list);
}
Beispiel #2
0
    local_data->nherds_detected_by_prodtype =
        RPT_new_reporting ("num-units-detected-by-production-type", NULL, RPT_group, RPT_never, FALSE);
    local_data->cumul_nherds_detected =
        RPT_new_reporting ("cumulative-num-units-detected", NULL, RPT_integer, RPT_never, TRUE);
    local_data->cumul_nherds_detected_by_prodtype =
        RPT_new_reporting ("cumulative-num-units-detected-by-production-type", NULL, RPT_group,
                           RPT_never, TRUE);
    g_ptr_array_add (m->outputs, local_data->detections);
    g_ptr_array_add (m->outputs, local_data->day_1st_detection);
    g_ptr_array_add (m->outputs, local_data->nherds_detected);
    g_ptr_array_add (m->outputs, local_data->nherds_detected_by_prodtype);
    g_ptr_array_add (m->outputs, local_data->cumul_nherds_detected);
    g_ptr_array_add (m->outputs, local_data->cumul_nherds_detected_by_prodtype);

    /* Set the reporting frequency for the output variables. */
    ee = scew_element_list (params, "output", &noutputs);
#if INFO
    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "%i output variables", noutputs);
#endif
    for (i = 0; i < noutputs; i++)
    {
        e = ee[i];
        variable_name = scew_element_contents (scew_element_by_name (e, "variable-name"));
        /* Do the outputs include a variable with this name? */
        for (j = 0; j < m->outputs->len; j++)
        {
            output = (RPT_reporting_t *) g_ptr_array_index (m->outputs, j);
            if (strcmp (output->name, variable_name) == 0)
                break;
        }
        if (j == m->outputs->len)
/**
 * Adds a set of parameters to a contact spread model.
 */
void
set_params (struct naadsm_model_t_ *self, PAR_parameter_t * params)
{
  local_data_t *local_data = (local_data_t *) (self->model_data);
  unsigned int nzones = ZON_zone_list_length (local_data->zones);
  unsigned int nprod_types = local_data->production_types->len;
  scew_element *e, **ee;
  gboolean success;
  gboolean has_destruction_cost_params = FALSE;
  gboolean has_vaccination_cost_params = FALSE;
  gboolean has_surveillance_cost_param = FALSE;
  destruction_cost_data_t destruction_cost_params = {};
  vaccination_cost_data_t vaccination_cost_params = {};
  double surveillance_cost_param = 0;
  gboolean *production_type = NULL;
  gboolean *zone = NULL;
  unsigned int i, j;
  unsigned int noutputs;
  RPT_reporting_t *output;
  const XML_Char *variable_name;

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

  /* Make sure the right XML subtree was sent. */
  g_assert (strcmp (scew_element_name (params), MODEL_NAME) == 0);

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "setting production types");
#endif

  /* Destruction Cost Parameters */
  e = scew_element_by_name (params, "appraisal");
  if (e != NULL)
    {
      destruction_cost_params.appraisal = PAR_get_money (e, &success);
      if (success)
        {
          has_destruction_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting per-unit appraisal cost to 0", MODEL_NAME);
          destruction_cost_params.appraisal = 0;
        }
    }
  else
    {
      g_warning ("%s: per-unit appraisal cost missing, setting to 0", MODEL_NAME);
      destruction_cost_params.appraisal = 0;
    }

  e = scew_element_by_name (params, "euthanasia");
  if (e != NULL)
    {
      destruction_cost_params.euthanasia = PAR_get_money (e, &success);
      if (success)
        {
          has_destruction_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting per-animal euthanasia cost to 0", MODEL_NAME);
          destruction_cost_params.euthanasia = 0;
        }
    }
  else
    {
      g_warning ("%s: per-animal euthanasia cost missing, setting to 0", MODEL_NAME);
      destruction_cost_params.euthanasia = 0;
    }

  e = scew_element_by_name (params, "indemnification");
  if (e != NULL)
    {
      destruction_cost_params.indemnification = PAR_get_money (e, &success);
      if (success)
        {
          has_destruction_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting per-animal indemnification cost to 0", MODEL_NAME);
          destruction_cost_params.indemnification = 0;
        }
    }
  else
    {
      g_warning ("%s: per-animal indemnification cost missing, setting to 0", MODEL_NAME);
      destruction_cost_params.indemnification = 0;
    }

  e = scew_element_by_name (params, "carcass-disposal");
  if (e != NULL)
    {
      destruction_cost_params.carcass_disposal = PAR_get_money (e, &success);
      if (success)
        {
          has_destruction_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting per-animal carcass disposal cost to 0", MODEL_NAME);
          destruction_cost_params.carcass_disposal = 0;
        }
    }
  else
    {
      g_warning ("%s: per-animal carcass disposal cost missing, setting to 0", MODEL_NAME);
      destruction_cost_params.carcass_disposal = 0;
    }

  e = scew_element_by_name (params, "cleaning-disinfecting");
  if (e != NULL)
    {
      destruction_cost_params.cleaning_disinfecting = PAR_get_money (e, &success);
      if (success)
        {
          has_destruction_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting per-unit cleaning and disinfecting cost to 0", MODEL_NAME);
          destruction_cost_params.cleaning_disinfecting = 0;
        }
    }
  else
    {
      g_warning ("%s: per-unit cleaning and disinfecting cost missing, setting to 0", MODEL_NAME);
      destruction_cost_params.cleaning_disinfecting = 0;
    }

  /* Vaccination Cost Parameters */
  e = scew_element_by_name (params, "vaccination-fixed");
  if (e != NULL)
    {
      vaccination_cost_params.vaccination_fixed = PAR_get_money (e, &success);
      if (success)
        {
          has_vaccination_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting per-unit vaccination cost to 0", MODEL_NAME);
          vaccination_cost_params.vaccination_fixed = 0;
        }
    }
  else
    {
      g_warning ("%s: per-unit vaccination cost missing, setting to 0", MODEL_NAME);
      vaccination_cost_params.vaccination_fixed = 0;
    }

  e = scew_element_by_name (params, "vaccination");
  if (e != NULL)
    {
      vaccination_cost_params.vaccination = PAR_get_money (e, &success);
      if (success)
        {
          has_vaccination_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting per-animal vaccination cost to 0", MODEL_NAME);
          vaccination_cost_params.vaccination = 0;
        }
    }
  else
    {
      g_warning ("%s: per-animal vaccination cost missing, setting to 0", MODEL_NAME);
      vaccination_cost_params.vaccination = 0;
    }

  e = scew_element_by_name (params, "baseline-vaccination-capacity");
  if (e != NULL)
    {
      vaccination_cost_params.baseline_capacity = (unsigned int) PAR_get_unitless (e, &success);
      if (success)
        {
          has_vaccination_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting baseline vaccination capacity to 1,000,000", MODEL_NAME);
          vaccination_cost_params.baseline_capacity = 1000000;
        }
    }
  else
    {
      g_warning ("%s: baseline vaccination capacity missing, setting to 1,000,000", MODEL_NAME);
      vaccination_cost_params.baseline_capacity = 1000000;
    }

  e = scew_element_by_name (params, "additional-vaccination");
  if (e != NULL)
    {
      vaccination_cost_params.extra_vaccination = PAR_get_money (e, &success);
      if (success)
        {
          has_vaccination_cost_params = TRUE;
        }
      else
        {
          g_warning ("%s: setting additional per-animal vaccination cost to 0", MODEL_NAME);
          vaccination_cost_params.extra_vaccination = 0;
        }
    }
  else
    {
      g_warning ("%s: additional per-animal vaccination cost missing, setting to 0", MODEL_NAME);
      vaccination_cost_params.extra_vaccination = 0;
    }

  /* No vaccinations have been performed yet. */
  vaccination_cost_params.capacity_used = 0;

  /* Surveillance Cost Parameters */
  e = scew_element_by_name (params, "surveillance");
  if (e != NULL)
    {
      surveillance_cost_param = PAR_get_money (e, &success);
      if (success)
        {
          has_surveillance_cost_param = TRUE;
        }
      else
        {
          g_warning ("%s: setting per-animal zone surveillance cost to 0", MODEL_NAME);
          surveillance_cost_param = 0;
        }
    }
  else
    {
      g_warning ("%s: per-animal zone surveillance cost missing, setting to 0", MODEL_NAME);
      surveillance_cost_param = 0;
    }

  /* Set the reporting frequency for the output variables. */
  ee = scew_element_list (params, "output", &noutputs);
#if DEBUG
  g_debug ("%i output variables", noutputs);
#endif
  for (i = 0; i < noutputs; i++)
    {
      e = ee[i];
      variable_name = scew_element_contents (scew_element_by_name (e, "variable-name"));
      /* Do the outputs include a variable with this name? */
      for (j = 0; j < self->outputs->len; j++)
        {
          output = (RPT_reporting_t *) g_ptr_array_index (self->outputs, j);
          if (strcmp (output->name, variable_name) == 0)
            break;
        }
      if (j == self->outputs->len)
        g_warning ("no output variable named \"%s\", ignoring", variable_name);
      else
        {
          RPT_reporting_set_frequency (output,
                                       RPT_string_to_frequency (scew_element_contents
                                                                (scew_element_by_name
                                                                 (e, "frequency"))));
#if DEBUG
          g_debug ("report \"%s\" %s", variable_name, RPT_frequency_name[output->frequency]);
#endif
        }
    }
  free (ee);

  /* Read zone and production type attributes to determine which
   * entries should be filled in. */
  if (scew_attribute_by_name (params, "zone") != NULL)
    zone = naadsm_read_zone_attribute (params, local_data->zones);
  else
    zone = NULL;
  production_type =
    naadsm_read_prodtype_attribute (params, "production-type", local_data->production_types);

  /* Copy the parameters into the selected zone and production types. */
  for (i = 0; i < nprod_types; i++)
    {
      if (production_type[i])
        {
          if (has_destruction_cost_params)
            {
              if (local_data->destruction_cost_params == NULL)
                {
                  local_data->destruction_cost_params =
                    g_new0 (destruction_cost_data_t *, nprod_types);
                  g_assert (local_data->destruction_cost_params != NULL);
                }
              if (local_data->destruction_cost_params[i] == NULL)
                {
                  local_data->destruction_cost_params[i] =
                    g_new0 (destruction_cost_data_t, 1);
                  g_assert (local_data->destruction_cost_params[i] != NULL);
                }
              memcpy (local_data->destruction_cost_params[i],
                      &destruction_cost_params,
                      sizeof (destruction_cost_data_t));
            }
          if (has_vaccination_cost_params)
            {
              if (local_data->vaccination_cost_params == NULL)
                {
                  local_data->vaccination_cost_params =
                    g_new0 (vaccination_cost_data_t *, nprod_types);
                  g_assert (local_data->vaccination_cost_params != NULL);
                }
              if (local_data->vaccination_cost_params[i] == NULL)
                {
                  local_data->vaccination_cost_params[i] =
                    g_new0 (vaccination_cost_data_t, 1);
                  g_assert (local_data->vaccination_cost_params[i] != NULL);
                }
              memcpy (local_data->vaccination_cost_params[i],
                      &vaccination_cost_params,
                      sizeof (vaccination_cost_data_t));
            }

          if (has_surveillance_cost_param)
            {
              if (zone)
                {
                  for (j = 0; j < nzones; j++)
                    {
                      if (zone[j])
                        {
                          if (local_data->surveillance_cost_param == NULL)
                            {
                              local_data->surveillance_cost_param =
                                g_new0 (double *, nzones);
                              g_assert (local_data->surveillance_cost_param != NULL);
                            }
                          if (local_data->surveillance_cost_param[j] == NULL)
                            {
                              local_data->surveillance_cost_param[j] =
                                g_new0 (double, nprod_types);
                              g_assert (local_data->surveillance_cost_param[j] != NULL);
                            }
                          local_data->surveillance_cost_param[j][i] = surveillance_cost_param;
                        }
                    }
                }