Example #1
0
/*
 * Print model, including the aliased terms
 * - one line per term
 * - if model->has_alias is true, then the value of all terms in
 *   the alias table is displayed
 * - if model->has_alias is false, then this is the same as model_print
 */
void model_print_full(FILE *f, model_t *model) {
  evaluator_t eval;
  ivector_t v;
  term_t *a;
  uint32_t n;

  if (model->has_alias && model->alias_map != NULL) {
    init_evaluator(&eval, model);

    // collect all terms that have a name
    init_ivector(&v, 0);
    model_collect_terms(model, true, model->terms, term_to_print, &v);

    n = v.size;
    a = v.data;
    eval_print_bool_assignments(f, &eval, a, n);
    eval_print_arithmetic_assignments(f, &eval, a, n);
    eval_print_bitvector_assignments(f, &eval, a, n);
    eval_print_constant_assignments(f, &eval, a, n);
    eval_print_tuple_assignments(f, &eval, a, n);
    eval_print_function_assignments(f, &eval, a, n);
    vtbl_print_queued_functions(f, &model->vtbl, true);
    delete_evaluator(&eval);
    delete_ivector(&v);
  } else {
    model_print(f, model);
  }
}
Example #2
0
model* model_create(enkfprm* prm)
{
    model* m = calloc(1, sizeof(model));
    char* modelprm = prm->modelprm;
    char* gridprm = prm->gridprm;

    model_setgrids(m, gridprm);

    /*
     * read model parameter file
     */
    {
        FILE* f = NULL;
        char buf[MAXSTRLEN];
        int line;
        variable* now = NULL;

        /*
         * get model tag, type and variables
         */
        f = enkf_fopen(modelprm, "r");
        line = 0;
        while (fgets(buf, MAXSTRLEN, f) != NULL) {
            char seps[] = " =\t\n";
            char* token = NULL;

            line++;
            if (buf[0] == '#')
                continue;
            if ((token = strtok(buf, seps)) == NULL)
                continue;
            if (strcasecmp(token, "NAME") == 0) {
                if ((token = strtok(NULL, seps)) == NULL)
                    enkf_quit("%s, l.%d: NAME not specified", modelprm, line);
                else if (m->name != NULL)
                    enkf_quit("%s, l.%d: NAME specified twice", modelprm, line);
                else
                    m->name = strdup(token);
            } else if (strncasecmp(token, "VAR", 3) == 0) {
                int i;

                if ((token = strtok(NULL, seps)) == NULL)
                    enkf_quit("%s, l.%d: VAR not specified", modelprm, line);
                for (i = 0; i < m->nvar; ++i)
                    if (strcasecmp(m->vars[i].name, token) == 0)
                        enkf_quit("%s, l.%d: VAR \"%s\" already specified", modelprm, line, token);
                if (m->nvar % NVAR_INC == 0)
                    m->vars = realloc(m->vars, (m->nvar + NVAR_INC) * sizeof(variable));
                now = &m->vars[m->nvar];
                variable_new(now, m->nvar, token);
                m->nvar++;
            } else if (strcasecmp(token, "GRID") == 0) {
                int i;

                if (now == NULL)
                    enkf_quit("%s, l.%d: VAR not specified", modelprm, line);
                if (now->gridid >= 0)
                    enkf_quit("%s, l.%d: GRID already specified for \"%s\"", modelprm, line, now->name);
                if ((token = strtok(NULL, seps)) == NULL)
                    enkf_quit("%s, l.%d: GRID not specified", modelprm, line);
                for (i = 0; i < m->ngrid; ++i)
                    if (strcasecmp(token, grid_getname(m->grids[i])) == 0) {
                        now->gridid = i;
                        break;
                    }
                if (i == m->ngrid)
                    enkf_quit("%s, l.%d: grid \"%s\" not specified", modelprm, line, token);
            } else if (strcasecmp(token, "INFLATION") == 0) {
                if (now == NULL)
                    enkf_quit("%s, l.%d: VAR not specified", modelprm, line);
                if (!isnan(now->inflation))
                    enkf_quit("%s, l.%d: INFLATION already specified for \"%s\"", modelprm, line, now->name);
                if ((token = strtok(NULL, seps)) == NULL)
                    enkf_quit("%s, l.%d: INFLATION not specified", modelprm, line);
                if (!str2double(token, &now->inflation))
                    enkf_quit("%s, l.%d: could not convert \"%s\" to double", modelprm, line, token);
                if ((token = strtok(NULL, seps)) != NULL) {
                    if (!str2double(token, &now->inf_ratio))
                        enkf_quit("%s, l.%d: could not convert \"%s\" to double", modelprm, line, token);
                }
            } else
                enkf_quit("%s, l.%d: unknown token \"%s\"", modelprm, line, token);
        }                       /* while reading modelprm */
        fclose(f);
        assert(m->name != NULL);
        assert(m->nvar > 0);
        {
            int i;

            for (i = 0; i < m->nvar; ++i)
                if (m->vars[i].gridid == -1) {
                    if (m->ngrid == 1)
                        m->vars[i].gridid = 0;
                    else
                        enkf_quit("%s: grid not specified for variable \"%s\"\n", modelprm, m->vars[i].name);
                }
        }
    }

    /*
     * set inflations
     */
    {
        int i;

        for (i = 0; i < m->nvar; ++i)
            if (isnan(m->vars[i].inflation)) {
                m->vars[i].inflation = prm->inflation_base;
                m->vars[i].inf_ratio = prm->inf_ratio;
            }
        prm->inflation_base = NaN;
        prm->inf_ratio = NaN;
    }

    model_print(m, "    ");

    assert(m->ngrid > 0);

    return m;
}
void generateModel(model *mo, int noStates) {
# define CUR_PROC "generateModel"

  state *states;
  int i, j;

  /* flags indicating whether a state is silent */
  int *silent_array;

  /*allocate memory for states and array of silent flags*/
  ARRAY_MALLOC(states, noStates);
  silent_array = (int*)malloc(sizeof(int)*noStates);

  /* initialize all states as none silent*/
  for (i=0; i < noStates; i++) {
    silent_array[i] = 0;
  }
 
  mo->N = noStates;
  mo->M = 4;
  mo->maxorder = noStates-1;
  mo->prior = -1;
  /* Model has Higher order Emissions and labeled states*/
  mo->model_type =  kLabeledStates;
  if (mo->maxorder>0)
    mo->model_type += kHigherOrderEmissions;
  /* kHigherOrderEmissions + kHasBackgroundDistributions*/

  /* allocate memory for pow look-up table and fill it */
  ARRAY_MALLOC(mo->pow_lookup, mo->maxorder+1)
  
  mo->pow_lookup[0] = 1;
  for (i=1; i<mo->maxorder+1; i++)
    mo->pow_lookup[i] =  mo->pow_lookup[i-1] * mo->M;

  /*initialize states*/
  for (i=0; i < mo->N; i++) {
    states[i].pi = (0==i ? 1.0:0.0);
    states[i].fix = 0;
    states[i].label = i%3;
    states[i].order = i%2;
    states[i].out_states = 2;
    states[i].in_states = 2;

    /* allocate memory for the a, the out- and incoming States and b array for higher emmission order states*/
    states[i].b = (double*)malloc(sizeof(double) * pow(mo->M, (states[i].order+1) ));
    states[i].out_id = (int*)malloc(sizeof(int)*states[i].out_states);
    states[i].in_id = (int*)malloc(sizeof(int)*states[i].in_states);
    states[i].out_a = (double*)malloc(sizeof(double)*states[i].out_states);
    states[i].in_a = (double*)malloc(sizeof(double)*states[i].in_states);

    for (j = 0; j < pow(mo->M,states[i].order+1); j++){
      states[i].b[j] = ( (0==(i+j)%mo->M) ? .6 : .4 / (mo->M-1));
    }

    if ((mo->N-1)==i) {
      states[i].out_id[0] = 0;
      states[i].out_id[1] = i;
    }
    else {
      states[i].out_id[0] = i;
      states[i].out_id[1] = i+1;
    }

    if (0==i) {
      states[i].in_id[0]  = i;
      states[i].in_id[1]  = mo->N-1;
    }
    else {
      states[i].in_id[1]  = i-1;
      states[i].in_id[0]  = i;
    }

    states[i].out_a[0] = 0.5;
    states[i].out_a[1] = 0.5;
    states[i].in_a[0]  = 0.5;
    states[i].in_a[1]  = 0.5;

#ifdef DEBUG
    printf("State %d goto    : %d, %d\n", i, states[i].out_id[0], states[i].out_id[1]);
    printf("State %d comefrom: %d, %d\n", i, states[i].in_id[0],  states[i].in_id[1]);
    printf("State %d goto    : %g, %g\n", i, states[i].out_a[0], states[i].out_a[1]);
    printf("State %d comefrom: %g, %g\n", i, states[i].in_a[0],  states[i].in_a[1]);
#endif
  }

  mo->s = states;
  mo->silent = silent_array;

#ifdef DEBUG
  for (i = 0; i < mo->N; i++) {
    printf("\n State %d:\n", i);
    for (j = 0; j < pow(mo->M,states[i].order+1); j++){
      printf("%g ",mo->s[i].b[j]);
    }
  }
#endif
  model_print(stdout, mo);

STOP:
  printf("\n");

# undef CUR_PROC
}
Example #4
0
static int py_model_print(PyObject * _self, FILE *fi, int flags)
{
    ModelObject*self = (ModelObject*)_self;
    model_print(self->model);
    return 0;
}