00001 /* 00002 COSTA: Problem solving environment for data assimilation 00003 Copyright (C) 2005 Nils van Velzen 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 */ 00019 00020 /** 00021 \file cta_model.h 00022 \brief Interface description of the COSTA default model component. For user implementation see cta_usr_model.h. 00023 00024 Functions for creating and working with models. CTA_Model is the default class implementation for models. 00025 */ 00026 00027 #ifndef CTA_MODEL_H 00028 #define CTA_MODEL_H 00029 #include "cta_handles.h" 00030 #include "cta_datatypes.h" 00031 #include "cta_functions.h" 00032 #include "cta_treevector.h" 00033 #include "cta_time.h" 00034 #include "cta_obsdescr.h" 00035 00036 /* Function Handle */ 00037 typedef CTA_Handle CTA_Model; 00038 typedef CTA_Handle CTA_ModelClass; 00039 00040 /* parameters for different user functions */ 00041 #define I_CTA_MODEL_CREATE_SIZE ( 0) 00042 #define I_CTA_MODEL_CREATE_INIT ( 1) 00043 #define I_CTA_MODEL_FREE ( 2) 00044 #define I_CTA_MODEL_COMPUTE ( 3) 00045 #define I_CTA_MODEL_SET_STATE ( 4) 00046 #define I_CTA_MODEL_GET_STATE ( 5) 00047 #define CTA_MODEL_AXPY_STATE ( 6) 00048 #define CTA_MODEL_AXPY_MODEL ( 7) 00049 #define CTA_MODEL_SET_FORC ( 8) 00050 #define CTA_MODEL_GET_FORC ( 9) 00051 #define CTA_MODEL_AXPY_FORC (10) 00052 #define CTA_MODEL_SET_PARAM (11) 00053 #define CTA_MODEL_GET_PARAM (12) 00054 #define CTA_MODEL_AXPY_PARAM (13) 00055 #define CTA_MODEL_GET_STATESCALING (14) 00056 #define CTA_MODEL_GET_TIMEHORIZON (15) 00057 #define CTA_MODEL_GET_CURRENTTIME (16) 00058 00059 /* stochastic functions */ 00060 #define CTA_MODEL_GET_NOISE_COUNT (17) 00061 #define CTA_MODEL_GET_NOISE_COVAR (18) 00062 00063 /* handling of observations */ 00064 #define CTA_MODEL_GET_OBSVALUES (19) 00065 #define CTA_MODEL_GET_OBSSELECT (20) 00066 #define CTA_MODEL_ANNOUNCE_OBSVALUES (21) 00067 00068 #define CTA_MODEL_ADD_NOISE (22) 00069 #define I_CTA_MODEL_EXPORT (23) 00070 #define I_CTA_MODEL_IMPORT (24) 00071 00072 /* Methods for adjoint models */ 00073 #define CTA_MODEL_ADJ_SET_FORC (25) 00074 #define CTA_MODEL_ADJ_COMPUTE (26) 00075 #define CTA_MODEL_ADJ_PREPARE (27) 00076 00077 /* Localization */ 00078 #define I_CTA_MODEL_GETOBSLOCALIZATION (28) 00079 00080 /* Loading and restoring of state */ 00081 #define CTA_MODEL_SAVE_INTERNALSTATE (30) 00082 #define CTA_MODEL_RESTORE_INTERNALSTATE (31) 00083 #define CTA_MODEL_RELEASE_INTERNALSTATE (32) 00084 #define CTA_MODEL_SAVE_PERSISTENTSTATE (33) 00085 #define CTA_MODEL_LOAD_PERSISTENTSTATE (29) 00086 00087 #define CTA_MODEL_NUMFUNC (34) 00088 00089 #ifdef __cplusplus 00090 extern "C" { 00091 #endif 00092 00093 /** \brief Create a model instance 00094 * 00095 * \param hmodcl I model class of new instance 00096 * \param userdata IO user data needed for creation (depends on modelclass) 00097 * \param hmodel O receives handle of new model instance 00098 * \return error status: CTA_OK if successful 00099 */ 00100 CTAEXPORT int CTA_Model_Create(CTA_ModelClass hmodcl, CTA_Handle userdata, CTA_Model *hmodel); 00101 00102 /** \brief Compute model for given timespan 00103 * 00104 * \param hmodel IO handle of model instance 00105 * \param htime I timespan for which to compute 00106 * \return error status: CTA_OK if successful 00107 */ 00108 CTAEXPORT int CTA_Model_Compute(CTA_Model hmodel, CTA_Time htime); 00109 00110 /** \brief Add noise during during the given timespan at 00111 * the Compute 00112 * 00113 * \note Noise is added in the compute-method 00114 * \param hmodel IO handle of model instance 00115 * \param htime I timespan for which to compute adding noise 00116 * \return error status: CTA_OK if successful 00117 */ 00118 CTAEXPORT int CTA_Model_AddNoise(CTA_Model hmodel, CTA_Time htime); 00119 00120 /** \brief Set the internal state of the model. 00121 * 00122 * \note A copy of the state is set 00123 * 00124 * \param hmodel IO handle of model instance 00125 * \param hstate I handle of new state 00126 * \return error status: CTA_OK if successful 00127 */ 00128 CTAEXPORT int CTA_Model_SetState(CTA_Model hmodel, CTA_TreeVector hstate); 00129 00130 /** \brief Get a copy of the internal state. 00131 * 00132 * \note Optionally a tree-vector is created. In that case the caller of this 00133 * method is responsible for freeing that tree-vector. The input state must be compatible 00134 * (same size and or composition) as the models internal state. 00135 * \note If *hstate == CTA_NULL a new object is created, user is responsible for freeing this object. 00136 * 00137 * \param hmodel I handle of model instance 00138 * \param hstate IO receives state of the model, *hstate can be CTA_NULL on calling (see note) 00139 * \return error status: CTA_OK if successful 00140 */ 00141 CTAEXPORT int CTA_Model_GetState(CTA_Model hmodel, CTA_TreeVector *hstate); 00142 00143 /** \brief Perform axpy operation on the internal state. 00144 * 00145 * \note AXPY: y=alpha*x+y. y corresponds to the models 00146 * internal state and x can be a state vector or a model 00147 00148 * \param hmodel IO handle of model instance (y) 00149 * \param alpha I alpha 00150 * \param hx I handle of x (state or model) 00151 * \return error status: CTA_OK if successful 00152 */ 00153 CTAEXPORT int CTA_Model_AxpyState(CTA_Model hmodel, double alpha, CTA_Handle hx); 00154 00155 /** \brief Get element-wise scaling for model state 00156 * 00157 * The values in the state-vector are compared on "importance" in various 00158 * algorithms like RRSQRT and COFFEE. The model state holds in general 00159 * various quantities like concentration, velicity, location etc in 00160 * arbitrary units. The scaling vector (that can be model state dependend) 00161 * makes it possible to meaningfull compare elements in the state-vector 00162 * for importance. Various methods are available like a transformation to 00163 * enery. 00164 * 00165 * The scaling vector represents a diagonal scaling matrix but is 00166 * respresented by a tree-vector. 00167 * 00168 * \note The elementwise scaling is returned in the form of a tree-vector 00169 * with same build-up as the tree-vector of the model state. The scaling vector 00170 * is created whenever hscale==CTA_NULL on input, the caller is 00171 * responsible for freeing this object. 00172 * 00173 * \param hmodel I handle of model instance 00174 * \param hscale IO receives state scaling vector for the model state, 00175 *hstate can be CTA_NULL on calling (see note) 00176 * \return error status: CTA_OK if successful 00177 */ 00178 CTAEXPORT int CTA_Model_GetStateScaling(CTA_Model hmodel, CTA_TreeVector *hscale); 00179 00180 00181 /** \brief Set the models forcings. 00182 * 00183 * \note Set the forcings (constant) for the given timespan. 00184 * The model will fall back to its own forcings definition 00185 * outside the given timespan. 00186 * 00187 * \param hmodel IO handle of model instance 00188 * \param tspan I time span on which to set the forcing values 00189 * \param hforc I handle of vector with new forcings 00190 * \return error status: CTA_OK if successful 00191 */ 00192 CTAEXPORT int CTA_Model_SetForc(CTA_Model hmodel, CTA_Time tspan, CTA_TreeVector hforc); 00193 00194 /** \brief Get a copy of the values of the models forcings 00195 * 00196 * \note Optionally a tree-vector is created in that case the caller of this 00197 * method is responsible for freeing that tree-vector. The input tree-vector 00198 * must be compatible (same size and or composition) as the models 00199 * internal tree-vector representing the forcings. 00200 * If the forcings of the model are not constant for the given timespan 00201 * the result is dependent on the model-implementation 00202 * \note If *hforc == CTA_NULL a new object is created, user is responsible for freeing this object. 00203 * 00204 * \param hmodel I handle of model instance 00205 * \param tspan I timespan for wich the given forcings are valid 00206 * \param hforc IO receives models forcings, *hforc can be CTA_NULL on calling (see note) 00207 * \return error status: CTA_OK if successful 00208 */ 00209 CTAEXPORT int CTA_Model_GetForc(CTA_Model hmodel, CTA_Time tspan, CTA_TreeVector *hforc); 00210 00211 /** \brief Perform axpy operation on the models forcings. 00212 * 00213 * \note AXPY: y=alpha*x+y. y corresponds to the models 00214 * internal forcings. 00215 * The adjustment to the forcings (alpha*x) is only valid for the given 00216 * time span. Note that the model will use y(t)+x for the given time span 00217 * where y(t) denotes the default forcings of the model. 00218 * 00219 * \param hmodel IO handle of model instance (y) 00220 * \param tspan I time span for wich the given forcings are valid 00221 * \param alpha I scalar 00222 * \param hx I handle of forcings tree-vector x 00223 * \return error status: CTA_OK if successful 00224 */ 00225 CTAEXPORT int CTA_Model_AxpyForc( CTA_Model hmodel, CTA_Time tspan, double alpha, CTA_TreeVector hx); 00226 00227 /** \brief Set parameters of the model. 00228 * 00229 * \param hmodel IO handle of model instance 00230 * \param hparam I handle of parameters vector 00231 * \return error status: CTA_OK if successful 00232 */ 00233 CTAEXPORT int CTA_Model_SetParam(CTA_Model hmodel, CTA_TreeVector hparam); 00234 00235 /** \brief Get a copy of the parameters of the model. 00236 * 00237 * \note Optionally a tree-vector is created in that case the caller of this 00238 * method is responsible for freeing that tree-vector. The input tree-vector 00239 * must be compatible (same size and or composition) as the models 00240 * internal tree-vector representing the parameters. 00241 * \note If *hforc == CTA_NULL a new object is created, user is responsible for freeing this object. 00242 * 00243 * \param hmodel I handle of model instance 00244 * \param hparam IO receives model forcings, *hforc can equal CTA_NULL on calling (see note) 00245 * \return error status: CTA_OK if successful 00246 */ 00247 CTAEXPORT int CTA_Model_GetParam(CTA_Model hmodel, CTA_TreeVector *hparam); 00248 00249 /** \brief Perform axpy operation on the models parameters. 00250 * 00251 * \note AXPY: y=alpha*x+y where y corresponds to the models 00252 * internal parameters. 00253 * 00254 * \param hmodel IO handle of model instance (y) 00255 * \param alpha I alpha 00256 * \param hx I handle of treevector of parameters (x) 00257 * \return error status: CTA_OK if successful 00258 */ 00259 CTAEXPORT int CTA_Model_AxpyParam( CTA_Model hmodel, double alpha, CTA_TreeVector hx); 00260 00261 /** \brief Return the timehorizon on the model. 00262 * The time horizon is the initial overal simulation span for which the mode is configured 00263 * 00264 * \param hmodel I handle of model instance 00265 * \param tHorizon I time horizon of model 00266 * \return error status: CTA_OK if successful 00267 */ 00268 CTAEXPORT int CTA_Model_GetTimeHorizon( CTA_Model hmodel, CTA_Time tHorizon); 00269 00270 /** \brief Return the current time of the model. 00271 * 00272 * \param hmodel I handle of model instance 00273 * \param tCurrent I time corresponding the the model state 00274 * \return error status: CTA_OK if successful 00275 */ 00276 CTAEXPORT int CTA_Model_GetCurrentTime( CTA_Model hmodel, CTA_Time tCurrent); 00277 00278 /** \brief Get covariance matrix of noise parameters. 00279 * 00280 * \note ONLY for Stochastic models. 00281 * The covariance matrix is represented by an array 00282 * of tree-vectors (columns of the matrix) 00283 * optionally a tree-vector is created in that case the caller of this 00284 * method is responsible for freeing that tree-vector. The input tree-vector 00285 * must be compatible (same size and or composition) as the models 00286 * internal tree-vector. 00287 * \note If hstmat[icol] == CTA_NULL a new object is created, user is responsible for freeing this object. 00288 * 00289 * \param hmodel I handle of model instance 00290 * \param hstmat O receives array of tree-vectors, *hstmat can equal CTA_NULL on calling (see note) 00291 * \return error status: CTA_OK if successful 00292 */ 00293 CTAEXPORT int CTA_Model_GetNoiseCovar(CTA_Model hmodel, CTA_TreeVector *hstmat); 00294 00295 /** \brief Get number of noise parameters: the number of columns of the noise covariance matrix. 00296 * 00297 * \note ONLY for Stochastic models. 00298 * 00299 * \param hmodel I handle of model instance 00300 * \param nnoise O receives number of noise parameters 00301 * \return error status: CTA_OK if successful 00302 */ 00303 CTAEXPORT int CTA_Model_GetNoiseCount(CTA_Model hmodel,int *nnoise); 00304 00305 /** \brief Free model instance. 00306 * 00307 * \note ONLY for Stochastic models. 00308 * 00309 * \param hmodel IO handle of model instance, replaced by CTA_NULL on return 00310 * \return error status: CTA_OK if successful 00311 */ 00312 CTAEXPORT int CTA_Model_Free(CTA_Model *hmodel); 00313 00314 /** \brief Announce to the model what observations will be requested. 00315 * 00316 * Before the compute method this method is used to announce what 00317 * obeservation will be requested after the CTA_Model_Compute using the 00318 * CTA_Model_GetObsvalues method. 00319 * 00320 * For some simulation models it is more efficient to do a single simulation 00321 * (a single CTA_Model_Compute call) for a particular simulation span then 00322 * simulating the same simulation span in a number of steps (multiple 00323 * CTA_Model_Compute calls). 00324 * 00325 * This method can be used to announce for what observations the model 00326 * must provide a prediction in advance. This method must be called prior 00327 * to the CTA_Compute method and makes it possible to perform simulations 00328 * over a longer time interval without the need to interupt the computations 00329 * in order to get the predictions at intermediate time instances. 00330 * 00331 * Notes on the behavior of the method: 00332 * - The observation description used in the first CTA_Model_GetObsValues 00333 * after the compute MUST be the same as the observation description 00334 * used in the announce. 00335 * - All observations that are announced MUST be in the timespan of the 00336 * following CTA_Model_Compute. 00337 * - The announced observations can only be retreved ONCE after the 00338 * CTA_Model_Compute. 00339 * - A CTA_Model_SetState or CTA_Model_AxpyState will reset the announced 00340 * CTA_Model_AnnounceObsValues administration (since stored predictions 00341 * might not be valid anymore) 00342 * 00343 * \param hmodel I handle of model instance 00344 * \param hdescr I observation description component 00345 * \return error status: CTA_OK if successful 00346 */ 00347 CTAEXPORT int CTA_Model_AnnounceObsValues(CTA_Model hmodel, CTA_ObsDescr hdescr); 00348 00349 /** \brief Get (interpolate) the models internal state to the 00350 * observations described as specified in the observation 00351 * description component. 00352 * 00353 * \note The interface supports a the time instance for time-interpolation. 00354 * It depends on the model whether and how this is supported. 00355 * 00356 * \param hmodel I handle of model instance 00357 * \param htime I time instance (for checking and time-interpolation if 00358 * supported by model) 00359 * \param hdescr I observation description component 00360 * \param values O receives values of the models internal state corresponding to 00361 * observations as described in hdescr 00362 * \return error status: CTA_OK if successful 00363 */ 00364 CTAEXPORT int CTA_Model_GetObsValues(CTA_Model hmodel, CTA_Time htime, 00365 CTA_ObsDescr hdescr, CTA_Vector values); 00366 00367 00368 /** \brief Get for each observation a localization scaling vector 00369 * 00370 * \param hmodel I handle of model instance 00371 * \param hdescr I observation description for which we want localization scaling vectors 00372 * \param distance I characteristic distance 00373 * \param locVecs O costa vector of handles to treevectors (scaling vectors). The treevectors 00374 * are created when the indices are CTA_NULL on entry 00375 * 00376 * \return error status: CTA_OK if successful 00377 */ 00378 CTAEXPORT int CTA_Model_GetObsLocalization( CTA_Model hmodel, 00379 CTA_ObsDescr hdescr, double distance, CTA_Vector locVecs); 00380 00381 00382 /** \brief Get a query for the stochastic observer in order to 00383 * filter out the observations that can actually be provided by the model. 00384 * 00385 * \param hmodel I handle of model instance 00386 * \param htime I time instance 00387 * \param hdescr I observation description component 00388 * \param sselect O receives a query to filter out the observations, must exist before calling 00389 * \return error status: CTA_OK if successful 00390 */ 00391 CTAEXPORT int CTA_Model_GetObsSelect(CTA_Model hmodel, CTA_Time htime, 00392 CTA_ObsDescr hdescr, CTA_String sselect); 00393 00394 /* \brief Save the current state of the model in a treevector 00395 * 00396 * \param hmodel I handle of model instance 00397 * \param instanceID O ID to retrieve the CTA_Handle to the treevector 00398 * \return error status: CTA_OK if successful 00399 */ 00400 CTAEXPORT int CTA_Model_SaveInternalState(CTA_Model hmodel, CTA_String *instanceID); 00401 00402 /* \brief Restore the state of the model to a previously saved state 00403 * 00404 * \param hmodel I handle of model instance 00405 * \param instanceID O ID to retrieve the CTA_Handle to the treevector with the previously saved state 00406 * \return error status: CTA_OK if successful 00407 */ 00408 CTAEXPORT int CTA_Model_RestoreInternalState(CTA_Model hmodel, CTA_String instanceID); 00409 00410 /* \brief Free memory of a previously saved state 00411 * 00412 * \param hmodel I handle of model instance 00413 * \param instanceID I ID to retrieve the CTA_Handle to the treevector to release 00414 * \return error status: CTA_OK if successful 00415 */ 00416 CTAEXPORT int CTA_Model_ReleaseInternalState(CTA_Model hmodel, CTA_String instanceID); 00417 00418 /* \brief Save a previously saved internal state to file 00419 * 00420 * \param hmodel I handle of model instance 00421 * \param filename I name of file to be written 00422 * \param instanceID I ID to retrieve the CTA_Handle to the treevector to export to file 00423 * \return error status: CTA_OK if successful 00424 */ 00425 CTAEXPORT int CTA_Model_SavePersistentState(CTA_Model hmodel, CTA_String filename, CTA_String instanceID); 00426 00427 /* \brief Load a model state from file and save it as an internal state 00428 * 00429 * \param hmodel I handle of model instance 00430 * \param filename I name of file to read state from 00431 * \param instanceID O ID to retrieve the CTA_Handle to the treevector that holds the state 00432 * \return error status: CTA_OK if successful 00433 */ 00434 CTAEXPORT int CTA_Model_LoadPersistentState(CTA_Model hmodel, CTA_String filename, CTA_String *instanceID); 00435 00436 /** \brief Export the whole internal state of a model 00437 * This export function will export the whole state of the model such that 00438 * a so called "restart" start from this point yielding the same results. 00439 * There are no ruled on the format that is used to store the data. 00440 * Various extra otions are valid but a model will in most cases support an export 00441 * to a file and to a COSTA pack object. 00442 * 00443 * 00444 * \param hmodel I handle of model instance 00445 * \param hexport I target for export e.g. CTA_File or CTA_Pack 00446 * \return error status: CTA_OK if successful 00447 */ 00448 int CTA_Model_Export( CTA_Model hmodel, CTA_Handle hexport); 00449 00450 /** \brief Import the whole internal state of a model 00451 * After the inport the models internal state is exactly the same as the point that 00452 * the export was created using CTA_Model_Export. 00453 * 00454 * 00455 * \param hmodel I handle of model instance 00456 * \param himport I handle with data created by CTA_MODEL_Export e.g. CTA_File or CTA_Pack 00457 * \return error status: CTA_OK if successful 00458 */ 00459 int CTA_Model_Import( CTA_Model hmodel, CTA_Handle himport); 00460 00461 /** \brief 00462 * bla 00463 * HOMEWORK FOR JULIUS! 00464 * 00465 * \param hmodel I handle of model instance 00466 * 00467 * \return error status: CTA_OK if successful 00468 */ 00469 int CTA_Model_AdjSetointForc(CTA_Model hmodel, CTA_ObsDescr hdescr, CTA_Vector vforc); 00470 00471 /** \brief 00472 * bla 00473 * HOMEWORK FOR JULIUS! 00474 * 00475 * \param hmodel I handle of model instance 00476 * 00477 * \return error status: CTA_OK if successful 00478 */ 00479 int CTA_Model_AdjPrepare(CTA_Model hmodel, CTA_Time time); 00480 00481 /** \brief 00482 * bla 00483 * HOMEWORK FOR JULIUS! 00484 * 00485 * \param hmodel I handle of model instance 00486 * 00487 * \return error status: CTA_OK if successful 00488 */ 00489 int CTA_Model_AdjCompute(CTA_Model hmodel, CTA_Time time); 00490 00491 /*Initernal non-user routines */ 00492 int CTAI_Model_PerformTimesteps( 00493 CTA_Model hmodel, /* handle of model */ 00494 CTA_Function *function, /* Function that must be called */ 00495 CTA_Time htime, /* timespan to compute */ 00496 int mindBarrier /* Flag to activate barrier check fixed timestepping */ 00497 ); 00498 #ifdef __cplusplus 00499 } 00500 #endif 00501 00502 00503 #endif 00504