Beispiel #1
0
int
wr_dpi(Dpi *d,
       char *filename,
       int verbosity)
{
  int err = 0;
  int status = 0;
  int u = 0;			/* short hand for unit... */

  struct Shadow_Identifiers si;

  memset(&si, 0, sizeof(struct Shadow_Identifiers));

  /*
   * From the C interface guide the basic calling sequence is given
   * for the case of adding new dimensions, variables and attributes to
   * an existing netCDF dataset.
   *
   *  nc_open();
   *  nc_redef();
   *    nc_def_dim();
   *    nc_def_var();
   *    nc_put_att();
   *  nc_enddef();
   *  nc_put_var();
   *  nc_close();
   *
   */

  /*
   * Open the file.
   */

#ifdef NETCDF_3
  err = nc_open(filename, NC_WRITE, &u);
  if ( err != NC_NOERR )
    {
      EH(-1, "nc_open() problem.");
    }
#endif
#ifdef NETCDF_2
  err = ncopen(filename, NC_WRITE);
  EH(err, "ncopen() problem.");
  u   = err;
#endif

  /*
   * Go into define mode.
   */

#ifdef NETCDF_3
  err = nc_redef(u);
  if ( err != NC_NOERR )
    {
      EH(-1, "nc_redef() problem.");
    }
#endif
#ifdef NETCDF_2
  err = ncredef(u);
  EH(err, "ncredef() problem.");
#endif

  /*
   * Define each of the netCDF dimensions that will be needed to describe
   * the extent of netCDF variables that are arrays.
   */
 
  define_dimension(u, DIM_LEN_EB_NUM_PRIVATE_ELEMS, 
		   d->len_eb_num_private_elems,
		   &si.len_eb_num_private_elems);

  define_dimension(u, DIM_LEN_ELEM_VAR_TAB_GLOBAL,
		   d->len_elem_var_tab_global,
		   &si.len_elem_var_tab_global);

  define_dimension(u, DIM_LEN_ELEM_ELEM_LIST, /* new e-e */
		   d->len_elem_elem_list,
		   &si.len_elem_elem_list);

  define_dimension(u, DIM_LEN_NODE_DESCRIPTION,
		   d->len_node_description,
		   &si.len_node_description);

  define_dimension(u, DIM_LEN_NS_NODE_LIST,
		   d->len_ns_node_list,
		   &si.len_ns_node_list);

  define_dimension(u, DIM_LEN_NS_DISTFACT_LIST,
		   d->len_ns_distfact_list,
		   &si.len_ns_distfact_list);

  define_dimension(u, DIM_LEN_SS_ELEM_LIST,
		   d->len_ss_elem_list,
		   &si.len_ss_elem_list);

  define_dimension(u, DIM_LEN_SS_DISTFACT_LIST,
		   d->len_ss_distfact_list,
		   &si.len_ss_distfact_list);

  define_dimension(u, DIM_LEN_STRING,
		   d->len_string,
		   &si.len_string);

  define_dimension(u, DIM_LEN_PTR_SET_MEMBERSHIP,
		   d->len_ptr_set_membership,
		   &si.len_ptr_set_membership);

  define_dimension(u, DIM_LEN_SET_MEMBERSHIP,
		   d->len_set_membership,
		   &si.len_set_membership);

  define_dimension(u, DIM_NUM_ELEM_BLOCKS,
		   d->num_elem_blocks,
		   &si.num_elem_blocks);

  define_dimension(u, DIM_NUM_ELEM_BLOCKS_GLOBAL,
		   d->num_elem_blocks_global,
		   &si.num_elem_blocks_global);

  define_dimension(u, DIM_NUM_ELEMS,
		   d->num_elems,
		   &si.num_elems);

  define_dimension(u, DIM_NUM_GLOBAL_NODE_DESCRIPTIONS,
		   d->num_global_node_descriptions,
		   &si.num_global_node_descriptions);

  define_dimension(u, DIM_NUM_NEIGHBORS,
		   d->num_neighbors,
		   &si.num_neighbors);

  define_dimension(u, DIM_NUM_NODE_SETS,
		   d->num_node_sets,
		   &si.num_node_sets);

  define_dimension(u, DIM_NUM_NODE_SETS_GLOBAL,
		   d->num_node_sets_global,
		   &si.num_node_sets_global);

  define_dimension(u, DIM_NUM_NODES,
		   d->num_nodes,
		   &si.num_nodes);

  define_dimension(u, DIM_NUM_PROPS_EB, d->num_props_eb,
		   &si.num_props_eb);

      define_dimension(u, DIM_NUM_PROPS_NS,
		       d->num_props_ns,
		       &si.num_props_ns);

      define_dimension(u, DIM_NUM_PROPS_SS,
		       d->num_props_ss,
		       &si.num_props_ss);

  define_dimension(u, DIM_NUM_SIDE_SETS,
		   d->num_side_sets,
		   &si.num_side_sets);

  define_dimension(u, DIM_NUM_SIDE_SETS_GLOBAL,
		   d->num_side_sets_global,
		   &si.num_side_sets_global);

  define_dimension(u, DIM_NUM_UNIVERSE_NODES,
		   d->num_universe_nodes,
		   &si.num_universe_nodes);

  if (d->num_side_sets_global > 0) {
    define_dimension(u, DIM_LEN_SS_BLOCK_INDEX_GLOBAL,
                     d->num_side_sets_global + 1,
                     &si.len_ss_block_index_global);
    
    define_dimension(u, DIM_LEN_SS_BLOCK_LIST_GLOBAL,
                     d->ss_block_index_global[d->num_side_sets_global],
                     &si.len_ss_block_list_global);
  } else {
      define_dimension(u, DIM_LEN_SS_BLOCK_INDEX_GLOBAL,
                       0,
                       &si.len_ss_block_index_global);
      define_dimension(u, DIM_LEN_SS_BLOCK_LIST_GLOBAL,
                       0,
                       &si.len_ss_block_list_global);
  }
  
  /*
   * Define variables. Arrays only get defined if their respective dimensions
   * are greater than zero.
   *
   * Also, this handy routine uses two arguments for the possibility of
   * up to 2D arrays. Dummy arguments of "-1" are inserted for 1D arrays 
   * or for scalar variables ( zero dimensional arrays).
   */
  
  define_variable(u, VAR_DPI_VERSION_STRING, NC_CHAR, 1,
		  si.len_string, -1,
		  d->len_string, -1,
		  &si.dpi_version_string);

  define_variable(u, VAR_EB_ELEM_TYPE_GLOBAL, NC_CHAR, 2, 
		  si.num_elem_blocks_global, si.len_string,
		  d->num_elem_blocks_global, d->len_string,
		  &si.eb_elem_type_global);

  define_variable(u, VAR_EB_ID_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks_global, -1,
		  d->num_elem_blocks_global, -1,
		  &si.eb_id_global);

  define_variable(u, VAR_EB_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks, -1,
		  d->num_elem_blocks, -1,
		  &si.eb_index_global);

  define_variable(u, VAR_EB_NUM_ATTR_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks_global, -1,
		  d->num_elem_blocks_global, -1,
		  &si.eb_num_attr_global);

  define_variable(u, VAR_EB_NUM_ELEMS_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks_global, -1,
		  d->num_elem_blocks_global, -1,
		  &si.eb_num_elems_global);

  define_variable(u, VAR_EB_NUM_NODES_PER_ELEM_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks_global, -1,
		  d->num_elem_blocks_global, -1,
		  &si.eb_num_nodes_per_elem_global);

  define_variable(u, VAR_EB_NUM_PRIVATE_ELEMS, NC_INT, 1, 
		  si.num_elem_blocks, -1,
		  d->num_elem_blocks, -1,
		  &si.eb_num_private_elems);

  if ( d->num_props_eb > 1 )	/* Properties are weird, recall. */
    {
      define_variable(u, VAR_EB_PROP_GLOBAL, NC_INT, 2,
		      si.num_props_eb, si.num_elem_blocks_global,
		      d->num_props_eb, d->num_elem_blocks_global,
		      &si.eb_prop_global);
    }

  if ( d->num_elems > 0 )
    {
      define_variable(u, VAR_ELEM_INDEX_GLOBAL, NC_INT, 1,
		      si.num_elems, -1,
		      d->num_elems, -1,
		      &si.elem_index_global);
    }

  if ( d->len_elem_var_tab_global > 0 )
    {
      define_variable(u, VAR_ELEM_VAR_TAB_GLOBAL, NC_INT, 1,
		      si.len_elem_var_tab_global, -1,
		      d->len_elem_var_tab_global, -1,
		      &si.elem_var_tab_global);
    }

  if ( d->len_elem_elem_list > 0 )
    {
      define_variable(u, VAR_ELEM_OWNER, NC_INT, 1,
		      si.num_elems, -1,
		      d->num_elems, -1,
		      &si.elem_owner);

      define_variable(u, VAR_ELEM_ELEM_LIST_GLOBAL, NC_INT, 1,
		      si.len_elem_elem_list, -1,
		      d->len_elem_elem_list, -1,
		      &si.elem_elem_list_global);

      define_variable(u, VAR_ELEM_ELEM_TWST_GLOBAL, NC_INT, 1,
		      si.len_elem_elem_list, -1,
		      d->len_elem_elem_list, -1,
		      &si.elem_elem_twst_global);

      define_variable(u, VAR_ELEM_ELEM_FACE_GLOBAL, NC_INT, 1,
		      si.len_elem_elem_list, -1,
		      d->len_elem_elem_list, -1,
		      &si.elem_elem_face_global);

      define_variable(u, VAR_ELEM_ELEM_PROC_GLOBAL, NC_INT, 1,
		      si.len_elem_elem_list, -1,
		      d->len_elem_elem_list, -1,
		      &si.elem_elem_proc_global);
    }

  define_variable(u, VAR_GLOBAL_NODE_DESCRIPTION, NC_INT, 2, 
		  si.num_global_node_descriptions, si.len_node_description,
		  d->num_global_node_descriptions, d->len_node_description,
		  &si.global_node_description);

  define_variable(u, VAR_MY_NAME, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.my_name);

  define_variable(u, VAR_NEIGHBOR, NC_INT, 1, 
		  si.num_neighbors, -1,
		  d->num_neighbors, -1,
		  &si.neighbor);

  if ( d->num_nodes > 0 )
    {
      define_variable(u, VAR_NODE_INDEX_GLOBAL, NC_INT, 1,
		      si.num_nodes, -1,
		      d->num_nodes, -1,
		      &si.node_index_global);
    }

  define_variable(u, VAR_NS_DISTFACT_INDEX_GLOBAL, NC_INT, 1,
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_distfact_index_global);

  define_variable(u, VAR_NS_DISTFACT_LEN_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.ns_distfact_len_global);

  define_variable(u, VAR_NS_DISTFACT_LIST_INDEX_GLOBAL, NC_INT, 1,
		  si.len_ns_distfact_list, -1,
		  d->len_ns_distfact_list, -1,
		  &si.ns_distfact_list_index_global);

  define_variable(u, VAR_NS_ID_GLOBAL, NC_INT, 1, 
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_id_global);

  define_variable(u, VAR_NS_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_node_sets, -1,
		  d->num_node_sets, -1,
		  &si.ns_index_global);

  define_variable(u, VAR_NS_NODE_INDEX_GLOBAL, NC_INT, 1,
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_node_index_global);


  define_variable(u, VAR_NS_NODE_LEN_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.ns_node_len_global);

  define_variable(u, VAR_NS_NODE_LIST_INDEX_GLOBAL, NC_INT, 1,
		  si.len_ns_node_list, -1,
		  d->len_ns_node_list, -1,
		  &si.ns_node_list_index_global);

  define_variable(u, VAR_NS_NUM_DISTFACTS_GLOBAL, NC_INT, 1, 
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_num_distfacts_global);

  define_variable(u, VAR_NS_NUM_NODES_GLOBAL, NC_INT, 1, 
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_num_nodes_global);

  if ( d->num_props_ns > 1 )
    {
      define_variable(u, VAR_NS_PROP_GLOBAL, NC_INT, 2, 
		      si.num_props_ns, si.num_node_sets_global,
		      d->num_props_ns, d->num_node_sets_global,
		      &si.ns_prop_global);
    }

  define_variable(u, VAR_NUM_BOUNDARY_NODES, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_boundary_nodes);

  define_variable(u, VAR_NUM_DOFS_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_dofs_global);

  define_variable(u, VAR_NUM_ELEMS_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_elems_global);

  define_variable(u, VAR_NUM_EXTERNAL_NODES, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_external_nodes);

  define_variable(u, VAR_NUM_INTERNAL_NODES, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_internal_nodes);

  define_variable(u, VAR_NUM_NODES_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_nodes_global);

  define_variable(u, VAR_PTR_SET_MEMBERSHIP, NC_INT, 1, 
		  si.len_ptr_set_membership, -1,
		  d->len_ptr_set_membership, -1,
		  &si.ptr_set_membership);

  define_variable(u, VAR_SET_MEMBERSHIP, NC_INT, 1, 
		  si.len_set_membership, -1,
		  d->len_set_membership, -1,
		  &si.set_membership);

  define_variable(u, VAR_SS_DISTFACT_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_distfact_index_global);

  define_variable(u, VAR_SS_DISTFACT_LIST_INDEX_GLOBAL, NC_INT, 1, 
		  si.len_ss_distfact_list, -1,
		  d->len_ss_distfact_list, -1,
		  &si.ss_distfact_list_index_global);

  define_variable(u, VAR_SS_DISTFACT_LEN_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.ss_distfact_len_global);

  define_variable(u, VAR_SS_ELEM_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_elem_index_global);

  define_variable(u, VAR_SS_ELEM_LEN_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.ss_elem_len_global);

  define_variable(u, VAR_SS_ELEM_LIST_INDEX_GLOBAL, NC_INT, 1, 
		  si.len_ss_elem_list, -1,
		  d->len_ss_elem_list, -1,
		  &si.ss_elem_list_index_global);

  define_variable(u, VAR_SS_ID_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_id_global);

  define_variable(u, VAR_SS_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_side_sets, -1,
		  d->num_side_sets, -1,
		  &si.ss_index_global);

  define_variable(u, VAR_SS_NUM_DISTFACTS_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_num_distfacts_global);

  define_variable(u, VAR_SS_NUM_SIDES_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_num_sides_global);

  if ( d->num_props_ss > 1 )
    {
      define_variable(u, VAR_SS_PROP_GLOBAL, NC_INT, 2, 
		      si.num_props_ss, si.num_side_sets_global,
		      d->num_props_ss, d->num_side_sets_global,
		      &si.ss_prop_global);
    }
    
  if ( d->num_side_sets_global > 0 )
    {
      define_variable(u, VAR_SS_INTERNAL_GLOBAL, NC_INT, 1,
                      si.num_side_sets_global, -1,
                      d->num_side_sets_global, -1,
                      &si.ss_internal_global);

      define_variable(u, VAR_SS_BLOCK_INDEX_GLOBAL, NC_INT, 1,
                      si.len_ss_block_index_global, -1,
                      d->num_side_sets_global + 1, -1,
                      &si.ss_block_index_global);

      define_variable(u, VAR_SS_BLOCK_LIST_GLOBAL, NC_INT, 1,
                      si.len_ss_block_list_global, -1,
                      d->ss_block_index_global[d->num_side_sets_global], -1,
                      &si.ss_block_list_global);
    }

  define_variable(u, VAR_UNDEFINED_BASIC_EQNVAR_ID, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.undefined_basic_eqnvar_id);


  /*
   * Leave define mode.
   */

#ifdef NETCDF_3
  err = nc_enddef(u);
  if ( err != NC_NOERR )
    {
      EH(-1, "nc_enddef() problem.");
    }
#endif
#ifdef NETCDF_2
  err = ncendef(u);
  EH(err, "ncendef() problem.");
#endif

  /*
   * Put variable values.
   *
   * This form is good for scalars, 1d arrays and 2d arrays. Any more and
   * you'll need to add another argument to the list for the backward
   * compatible to netCDF implementation to work properly. We'll assume
   * that start[] arrays that ncvarput() uses will be full of zeroes.
   * If not, then you'll need to do that case by hand.
   */

  put_variable(u, NC_CHAR, 1, 
	       d->len_string,			-1, 
	       si.dpi_version_string,		d->dpi_version_string);

  put_variable(u, NC_CHAR, 2, 
	       d->num_elem_blocks_global,	d->len_string,
	       si.eb_elem_type_global, &(d->eb_elem_type_global[0][0]));

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks_global,	-1, 
	       si.eb_id_global,			d->eb_id_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks,		-1, 
	       si.eb_index_global,		d->eb_index_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks_global,	-1, 
	       si.eb_num_attr_global,		d->eb_num_attr_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks_global,	-1, 
	       si.eb_num_elems_global,		d->eb_num_elems_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks_global,	-1, 
	       si.eb_num_nodes_per_elem_global,	
	       d->eb_num_nodes_per_elem_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks,		-1, 
	       si.eb_num_private_elems,		d->eb_num_private_elems);

  if ( d->num_props_eb > 1 )
    {
      put_variable(u, NC_INT, 2,
		   d->num_props_eb, d->num_elem_blocks_global,
		   si.eb_prop_global, &(d->eb_prop_global[0][0]));
    }

  if ( d->num_elems > 0 )
    {
      put_variable(u, NC_INT, 1, 
		   d->num_elems, -1, 
		   si.elem_index_global,	d->elem_index_global);

      put_variable(u, NC_INT, 1, d->num_elems,	-1, si.elem_owner,
		   d->elem_owner);

    }

  if ( d->len_elem_var_tab_global > 0 )
    {
      put_variable(u, NC_INT, 1, 
		   d->len_elem_var_tab_global,	-1, 
		   si.elem_var_tab_global,	d->elem_var_tab_global);
    }

  if ( d->len_elem_elem_list > 0 )
    {
      put_variable(u, NC_INT, 1, d->len_elem_elem_list,	-1, 
		   si.elem_elem_list_global, d->elem_elem_list_global);
      put_variable(u, NC_INT, 1, d->len_elem_elem_list,	-1, 
		   si.elem_elem_face_global, d->elem_elem_face_global);
      put_variable(u, NC_INT, 1, d->len_elem_elem_list,	-1, 
		   si.elem_elem_twst_global, d->elem_elem_twst_global);
      put_variable(u, NC_INT, 1, d->len_elem_elem_list,	-1, 
		   si.elem_elem_proc_global, d->elem_elem_proc_global);
    }

  put_variable(u, NC_INT, 2, 
	       d->num_global_node_descriptions,	d->len_node_description, 
	       si.global_node_description,&(d->global_node_description[0][0]));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.my_name,		&(d->my_name));

  put_variable(u, NC_INT, 1, 
	       d->num_neighbors,	-1, 
	       si.neighbor,		d->neighbor);

  if ( d->num_nodes > 0 )
    {
      put_variable(u, NC_INT, 1, 
		   d->num_nodes,	-1, 
		   si.node_index_global,	d->node_index_global);
    }

  put_variable(u, NC_INT, 0, 
	       -1, 	-1, 
	       si.ns_distfact_len_global, &(d->ns_distfact_len_global));

  put_variable(u, NC_INT, 0, 
	       -1, 	-1, 
	       si.ns_node_len_global, &(d->ns_node_len_global));

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,	-1, 
	       si.ns_id_global,		d->ns_id_global);

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets,	-1, 
	       si.ns_index_global,	d->ns_index_global);

  put_variable(u, NC_INT, 1, 
	       d->len_ns_distfact_list,	-1, 
	       si.ns_distfact_list_index_global, 
	       d->ns_distfact_list_index_global);

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,		-1, 
	       si.ns_distfact_index_global,	d->ns_distfact_index_global);

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,		-1, 
	       si.ns_node_index_global,	d->ns_node_index_global);

  put_variable(u, NC_INT, 1, 
	       d->len_ns_node_list,	-1, 
	       si.ns_node_list_index_global, 
	       d->ns_node_list_index_global);



  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,		-1, 
	       si.ns_num_distfacts_global,	d->ns_num_distfacts_global);

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,	-1, 
	       si.ns_num_nodes_global,	d->ns_num_nodes_global);

  if ( d->num_props_ns > 1 )
    {
      put_variable(u, NC_INT, 2,
		   d->num_props_ns,		d->num_node_sets_global,
		   si.ns_prop_global,	&(d->ns_prop_global[0][0]));
    }

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_boundary_nodes,	&(d->num_boundary_nodes));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_dofs_global,	&(d->num_dofs_global));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_elems_global,	&(d->num_elems_global));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_external_nodes,	&(d->num_external_nodes));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_internal_nodes,	&(d->num_internal_nodes));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_nodes_global,	&(d->num_nodes_global));

  put_variable(u, NC_INT, 1, 
	       d->len_ptr_set_membership,	-1, 
	       si.ptr_set_membership,	d->ptr_set_membership);

  put_variable(u, NC_INT, 1, 
	       d->len_set_membership,	-1, 
	       si.set_membership,	d->set_membership);

  put_variable(u, NC_INT, 1, d->num_side_sets_global, -1, 
	       si.ss_distfact_index_global, d->ss_distfact_index_global);

  put_variable(u, NC_INT, 0, -1, -1, 
	       si.ss_distfact_len_global, &(d->ss_distfact_len_global));

  if ( d->len_ss_distfact_list > 0 )
    {
      put_variable(u, NC_INT, 1, d->len_ss_distfact_list, -1, 
		   si.ss_distfact_list_index_global, 
		   d->ss_distfact_list_index_global);
    }

  put_variable(u, NC_INT, 1, d->num_side_sets_global, -1, 
	       si.ss_elem_index_global, d->ss_elem_index_global);

  put_variable(u, NC_INT, 0, -1, -1, 
	       si.ss_elem_len_global, &(d->ss_elem_len_global));

  if ( d->len_ss_elem_list > 0 )
    {
      put_variable(u, NC_INT, 1, d->len_ss_elem_list, -1, 
		   si.ss_elem_list_index_global, d->ss_elem_list_index_global);
    }

  put_variable(u, NC_INT, 1, 
	       d->num_side_sets_global,	-1, 
	       si.ss_id_global,		d->ss_id_global);

  if ( d->num_side_sets > 0 )
    {
      put_variable(u, NC_INT, 1, 
		   d->num_side_sets,	-1, 
		   si.ss_index_global,	d->ss_index_global);
    }

  put_variable(u, NC_INT, 1, 
	       d->num_side_sets_global,		-1, 
	       si.ss_num_distfacts_global,	d->ss_num_distfacts_global);

  put_variable(u, NC_INT, 1, 
	       d->num_side_sets_global,		-1, 
	       si.ss_num_sides_global,		d->ss_num_sides_global);

  if ( d->num_props_ss > 1 )
    {
      put_variable(u, NC_INT, 2,
		   d->num_props_ss,		d->num_side_sets_global,
		   si.ss_prop_global,		&(d->ss_prop_global[0][0]));
    }

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.undefined_basic_eqnvar_id, &(d->undefined_basic_eqnvar_id));
  
  if (d->num_side_sets_global > 0) {
      put_variable(u, NC_INT, 1,
                   d->num_side_sets_global, -1,
                   si.ss_internal_global, d->ss_internal_global);

      put_variable(u, NC_INT, 1,
                   d->num_side_sets_global+1, -1,
                   si.ss_block_index_global, d->ss_block_index_global);

      put_variable(u, NC_INT, 1,
                   d->ss_block_index_global[d->num_side_sets_global], -1,
                   si.ss_block_list_global, d->ss_block_list_global);
  }  
  /*
   * Close the file (flush buffers).
   */

#ifdef NETCDF_3
  err = nc_close(u);
  if ( err != NC_NOERR )
    {
      EH(-1, "nc_close() problem.");
    }
#endif
#ifdef NETCDF_2
  err = ncclose(u);
  EH(err, "ncclose()");
#endif

  return(status);
}
void agent_act(unsigned char * img_bytes, int img_width, int img_height, bool_t img_is_belly, int pass_button,
	            navdata_unpacked_t * navdata, commands_t * commands) {

	// Need engine
	if (!ep) {
		return;
	}

	// Create flat matrix big enough to hold WIDTH X HEIGHT X 3 (RGB) image data
	mxArray * img =  create_numeric_array(img_height*img_width*3, mxUINT8_CLASS);

	// Copy image bytes to matrix in order appropriate for reshaping
	unsigned char * p = (unsigned char *)mxGetChars(img);
	int rgb, row, col;;
	for (rgb=0; rgb<3; ++rgb) {
		for (col=0; col<img_width; ++col) {
			for (row=0; row<img_height; ++row) {
				*p = img_bytes[2-rgb + 3*(row * img_width +  col)]; 
				p++;
			}
		}
	}

	// Put the image variable into the Matlab environment
	put_variable("img", img);

	// Create a variable for passing navigation data to Matlab function
	mxArray * mx_navdata = create_numeric_array(10, mxDOUBLE_CLASS);
	
	// Build command using reshaped IMG variable and constants from navdata structure
	char cmd[200];
	double * np = (double *)mxGetData(mx_navdata);

    navdata_demo_t demo = navdata->navdata_demo;

	*np++ = (double)(img_is_belly?1:0);
	*np++ = (double)pass_button;
	*np++ = (double)demo.ctrl_state; 	     
	*np++ = (double)demo.vbat_flying_percentage;

	*np++ = demo.theta;                
	*np++ = demo.phi;                 
	*np++ = demo.psi;                

    *np++ = (double)navdata->navdata_altitude.altitude_raw;

    navdata_vision_raw_t vision_raw = navdata->navdata_vision_raw;

	*np++ = (double)vision_raw.vision_tx_raw;
	*np++ = (double)vision_raw.vision_ty_raw;

	// Put the navdata variable into the Matlab environment
	put_variable("navdata",   mx_navdata);

	// Build and evaluate command
	sprintf(cmd,"commands = %s(reshape(img, %d, %d, 3), navdata);", FUNCTION_NAME, img_height, img_width);
	if (engEvalString(ep, cmd)) {
		fprintf(stderr, "Error evaluating command: %s\n", cmd);
	}


	// Get output variables
	double *cp = (double *)mxGetData(engGetVariable(ep, "commands"));

	commands->zap     = (int)cp[0];
	commands->phi     = cp[1];
	commands->theta   = cp[2];
	commands->gaz     = cp[3];
	commands->yaw     = cp[4];

	// Deallocate memory
	mxDestroyArray(img);
	mxDestroyArray(mx_navdata);
}
Beispiel #3
0
int wam_run(wam_t *wam)
{
	wam->failed = 1;
	wam->opcnt = 0;
	wam->bpcnt = 0;

	int time = 0;
	int halted;
	while (wam->pc >= 0) {
		halted = 0;
		time += 1;

		wam->failed = 0;
		stmt_t *stmt = wam->prog->stmts[wam->pc];
		if (wam->opcnt > wam->maxopcnt) {
			printf("panic: maximum opcnt reached\n");
			wam->failed = 1;
			break;
		}

		if (time > 10000) {
			printf("> 10000\n");
			stmt->op = OP_HALT;
			wam->failed = 1;
			break;
		}

		switch (stmt->op) {
			case OP_ALLOC:
				allocate(wam); break;
			case OP_DEALLOC:
				deallocate(wam); break;
			case OP_CALL:
				wam_call(wam, stmt->jump); break;
			case OP_CREATE_VAR:
				create_variable(wam, stmt->args[0], stmt->args[1]); break;
			case OP_GET_CONST:
				get_constant(wam, stmt->args[0], stmt->args[1]); break;
			case OP_GET_VAL:
				get_value(wam, stmt->args[0], stmt->args[1]); break;
			case OP_GET_VAR:
				get_variable(wam, stmt->args[0], stmt->args[1]); break;
			case OP_PUT_VAL:
				put_value(wam, stmt->args[0], stmt->args[1]); break;
			case OP_PUT_VAR:
				put_variable(wam, stmt->args[0], stmt->args[1]); break;
			case OP_PUT_CONST:
				put_constant(wam, stmt->args[0], stmt->args[1]); break;
			case OP_HALT:
				halted = 1;
			case OP_NOOP:
				wam->pc += 1; break;
			case OP_PROCEED:
				proceed(wam); break;
			case OP_RTRY_ME_ELSE:
			case OP_TRY_ME_ELSE:
				try_me_else(wam, stmt->jump); break;
			case OP_TRUST_ME:
				wam->pc++; break;
			case OP_UNI_STRUC:
				unify_struc(wam, stmt->args[0], stmt->args[1], stmt->args[2]); break;
			case OP_UNI_VAR:
				unify_variable(wam, stmt->args[0], stmt->args[1]); break;
			case OP_UNI_LIST:
				unify_list(wam, stmt->args[0], stmt->args[1], stmt->args[2]); break;
			default:
				printf("unknown wam op in line %d!\n", wam->pc); wam_backtrack(wam); break;
		}
		if (halted) break;
	}
	if (halted) return 0;
	if (wam->failed) {
		while (wam->cp != NULL) wam_backtrack(wam);
		wam_backtrack(wam);
	}
	return 0;
}