Template:CMI Example CMI initialize C
From CSDMS
struct edu_csdms_models_Sedflux3D__data *this =
edu_csdms_models_Sedflux3D__get_data (self);
fprintf (stderr, "%s: Initializing.
", CMI_COMPONENT_NAME);
if (this->status >= CMI_STATUS_INITIALIZING)
return TRUE;
else
this->status = CMI_STATUS_INITIALIZING;
fprintf (stderr, "%s: Create port queue.
", CMI_COMPONENT_NAME);
{ // Create port queue.
edu_csdms_ports_CMIPort port = edu_csdms_ports_CMIPort__cast (self, _ex);
struct sidl_string__array* ports =
edu_csdms_tools_CMIConfigFile_get_array (this->cfg_file,
"CMI_PORT_NAMES", _ex);
this->ports = edu_csdms_tools_CMIPortQueue__create (_ex);
fprintf (stderr, "%s: Initialize port queue.
", CMI_COMPONENT_NAME);
edu_csdms_tools_CMIPortQueue_initialize (this->ports,
this->d_services, port, _ex);
fprintf (stderr, "%s: %s: Add ports to queue.
",
CMI_COMPONENT_NAME, CMI_PORT_NAMES);
edu_csdms_tools_CMIPortQueue_add_ports (this->ports, CMI_PORT_NAMES,
_ex);
fprintf (stderr, "%s: Connect ports.
", CMI_COMPONENT_NAME);
edu_csdms_tools_CMIPortQueue_connect_ports (this->ports, _ex);
}
fprintf (stderr, "%s: Open log.
", CMI_COMPONENT_NAME);
if (!this->log)
{
this->log = edu_csdms_tools_Verbose__create (_ex);
edu_csdms_tools_Verbose_set_log_level (this->log, 1, _ex);
}
fprintf (stderr, "%s: Read parameters.
", CMI_COMPONENT_NAME);
{ /* Read parameters from the config dialog. */
edu_csdms_tools_TemplateFiles template;
char **src;
char **dest;
const char *src_files = CMI_TEMPLATE_SOURCE_FILES;
const char *dest_files = CMI_TEMPLATE_DEST_FILES;
char * val;
//struct sidl_string__array* src_files =
// gov_cca_TypeMap_getStringArray (this->cmi_data,
// "CMI_TEMPLATE_SOURCE_FILES", NULL, _ex);
//sidl_string__array dst_files = gov_cca_TypeMap_getStringArray (
// this->cmi_data, "CMI_TEMPLATE_DEST_FILES", NULL, _ex);
template = edu_csdms_tools_TemplateFiles__create (_ex);
edu_csdms_tools_TemplateFiles_add_files (template, src_files,
dest_files, _ex);
val = gov_cca_TypeMap_getString (this->userinput,
"/Sedflux3D/Input/Var/InputDir", "GUI", _ex);
if (g_ascii_strcasecmp (val, "GUI")==0)
gov_cca_TypeMap_putString (this->userinput,
"/Sedflux3D/Input/Var/InputDir", ".", _ex);
val = gov_cca_TypeMap_getString (this->userinput,
"/Sedflux3D/Input/Var/InitializationFile", "GUI", _ex);
if (g_ascii_strcasecmp (val, "GUI")==0)
gov_cca_TypeMap_putString (this->userinput,
"/Sedflux3D/Input/Var/InitializationFile",
"${SimulationName}_init.kvf", _ex);
val = gov_cca_TypeMap_getString (this->userinput,
"/Sedflux3D/Input/Var/RiverFile", "GUI", _ex);
if (g_ascii_strcasecmp (val, "GUI")==0)
gov_cca_TypeMap_putString (this->userinput,
"/Sedflux3D/Input/Var/RiverFile",
"${SimulationName}_river.kvf", _ex);
val = gov_cca_TypeMap_getString (this->userinput,
"/Sedflux3D/Input/Var/SedimentFile", "GUI", _ex);
if (g_ascii_strcasecmp (val, "GUI")==0)
gov_cca_TypeMap_putString (this->userinput,
"/Sedflux3D/Input/Var/SedimentFile",
"${SimulationName}_sediment.kvf", _ex);
gov_cca_TypeMap_putString (this->userinput,
"/Sedflux3D/Input/Var/ProcessFile",
"${SimulationName}_process.kvf", _ex);
edu_csdms_tools_TemplateFiles_substitute (template,
this->userinput, "/"CMI_COMPONENT_NAME"/Input/Var/", ".",
_ex);
val = gov_cca_TypeMap_getString (this->userinput,
"/Sedflux3D/Input/Var/SimulationName", CMI_COMPONENT_NAME, _ex);
gov_cca_TypeMap_putString (this->userinput,
"/Sedflux3D/SimulationName", val, _ex);
}
/*
This is what Sedflux3D.txt will look like,
"sedflux -3 --silent --no-signals --init-file=${INIT_FILE} --input-dir=${INPUT_DIR} --working-dir=${WORKING_DIR}"
*/
fprintf (stderr, "%s: BMI initialize
", CMI_COMPONENT_NAME);
this->state = BMI_Initialize (CMI_COMPONENT_NAME"_command_line.txt");
fprintf (stderr, "%s: Set up PrintQueue.
", CMI_COMPONENT_NAME);
PRINT (2, "Set up PrintQueue");
{
edu_csdms_ports_CMIPort port = edu_csdms_ports_CMIPort__cast (self, _ex);
fprintf (stderr, "Create PrintQueue
"); fflush (stderr);
this->print_queue = edu_csdms_tools_PrintQueue__create (_ex);
fprintf (stderr, "Initialize PrintQueue
"); fflush (stderr);
edu_csdms_tools_PrintQueue_initialize (this->print_queue,
this->userinput, "/"CMI_COMPONENT_NAME, port, _ex);
fprintf (stderr, "Add file to PrintQueue
"); fflush (stderr);
edu_csdms_tools_PrintQueue_add_files_from_list (this->print_queue,
CMI_OUTPUT_FILE_NS, _ex);
fprintf (stderr, "Created PrintQueue
"); fflush (stderr);
}
fprintf (stderr, "%s: Initialize uses ports.
", CMI_COMPONENT_NAME);
PRINT (2, "Initialize model uses ports");
edu_csdms_tools_CMIPortQueue_initialize_ports (this->ports, NULL, _ex);
#if CMI_TURN_OFF_MAPPING
fprintf (stderr, "%s: Forgetting mappers.
", CMI_COMPONENT_NAME);
#else
fprintf (stderr, "%s: Set up mappers.
", CMI_COMPONENT_NAME);
edu_csdms_tools_CMIPortQueue_add_mappers (this->ports, CMI_MAPPERS, _ex);
#endif
//fprintf (stderr, "%s: Run mappers.
", CMI_COMPONENT_NAME);
//edu_csdms_tools_CMIPortQueue_run_mappers (this->ports, _ex);
fprintf (stderr, "%s: Initialized.
", CMI_COMPONENT_NAME);
this->status = CMI_STATUS_INITIALIZED;
return TRUE;