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 CTA_MODEL_CREATE_SIZE ( 0) 00042 #define CTA_MODEL_CREATE_INIT ( 1) 00043 #define CTA_MODEL_FREE ( 2) 00044 #define CTA_MODEL_COMPUTE ( 3) 00045 #define CTA_MODEL_SET_STATE ( 4) 00046 #define 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 CTA_MODEL_EXPORT (23) 00070 #define 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 #define CTA_MODEL_NUMFUNC (28) 00078 00079 #ifdef __cplusplus 00080 extern "C" { 00081 #endif 00082 00083 00084 /** \brief Create a model instance 00085 * 00086 * \param hmodcl I model class of new instance 00087 * \param userdata IO user data needed for creation (depends on modelclass) 00088 * \param hmodel O receives handle of new model instance 00089 * \return error status: CTA_OK if successful 00090 */ 00091 CTAEXPORT int CTA_Model_Create(CTA_ModelClass hmodcl, CTA_Handle userdata, CTA_Model *hmodel); 00092 00093 /** \brief Compute model for given timespan 00094 * 00095 * \param hmodel IO handle of model instance 00096 * \param htime I timespan for which to compute 00097 * \return error status: CTA_OK if successful 00098 */ 00099 CTAEXPORT int CTA_Model_Compute(CTA_Model hmodel, CTA_Time htime); 00100 00101 /** \brief Add noise during during the given timespan at 00102 * the Compute 00103 * 00104 * \note Noise is added in the compute-method 00105 * \param hmodel IO handle of model instance 00106 * \param htime I timespan for which to compute adding noise 00107 * \return error status: CTA_OK if successful 00108 */ 00109 CTAEXPORT int CTA_Model_AddNoise(CTA_Model hmodel, CTA_Time htime); 00110 00111 /** \brief Set the internal state of the model. 00112 * 00113 * \note A copy of the state is set 00114 * 00115 * \param hmodel IO handle of model instance 00116 * \param hstate I handle of new state 00117 * \return error status: CTA_OK if successful 00118 */ 00119 CTAEXPORT int CTA_Model_SetState(CTA_Model hmodel, CTA_TreeVector hstate); 00120 00121 /** \brief Get a copy of the internal state. 00122 * 00123 * \note Optionally a tree-vector is created. In that case the caller of this 00124 * method is responsible for freeing that tree-vector. The input state must be compatible 00125 * (same size and or composition) as the models internal state. 00126 * \note If *hstate == CTA_NULL a new object is created, user is responsible for freeing this object. 00127 * 00128 * \param hmodel I handle of model instance 00129 * \param hstate IO receives state of the model, *hstate can be CTA_NULL on calling (see note) 00130 * \return error status: CTA_OK if successful 00131 */ 00132 CTAEXPORT int CTA_Model_GetState(CTA_Model hmodel, CTA_TreeVector *hstate); 00133 00134 /** \brief Perform axpy operation on the internal state. 00135 * 00136 * \note AXPY: y=alpha*x+y. y corresponds to the models 00137 * internal state and x can be a state vector or a model 00138 00139 * \param hmodel IO handle of model instance (y) 00140 * \param alpha I alpha 00141 * \param hx I handle of x (state or model) 00142 * \return error status: CTA_OK if successful 00143 */ 00144 CTAEXPORT int CTA_Model_AxpyState(CTA_Model hmodel, double alpha, CTA_Handle hx); 00145 00146 /** \brief Get element-wise scaling for model state 00147 * 00148 * The values in the state-vector are compared on "importance" in various 00149 * algorithms like RRSQRT and COFFEE. The model state holds in general 00150 * various quantities like concentration, velicity, location etc in 00151 * arbitrary units. The scaling vector (that can be model state dependend) 00152 * makes it possible to meaningfull compare elements in the state-vector 00153 * for importance. Various methods are available like a transformation to 00154 * enery. 00155 * 00156 * The scaling vector represents a diagonal scaling matrix but is 00157 * respresented by a tree-vector. 00158 * 00159 * \note The elementwise scaling is returned in the form of a tree-vector 00160 * with same build-up as the tree-vector of the model state. The scaling vector 00161 * is created whenever hscale==CTA_NULL on input, the caller is 00162 * responsible for freeing this object. 00163 * 00164 * \param hmodel I handle of model instance 00165 * \param hstate IO receives state scaling vector for the model state, 00166 *hstate can be CTA_NULL on calling (see note) 00167 * \return error status: CTA_OK if successful 00168 */ 00169 CTAEXPORT int CTA_Model_GetStateScaling(CTA_Model hmodel, CTA_TreeVector *hscale); 00170 00171 00172 /** \brief Set the models forcings. 00173 * 00174 * \note Set the forcings (constant) for the given timespan. 00175 * The model will fall back to its own forcings definition 00176 * outside the given timespan. 00177 * 00178 * \param hmodel IO handle of model instance 00179 * \param tspan I time span on which to set the forcing values 00180 * \param hforc I handle of vector with new forcings 00181 * \return error status: CTA_OK if successful 00182 */ 00183 CTAEXPORT int CTA_Model_SetForc(CTA_Model hmodel, CTA_Time tspan, CTA_TreeVector hforc); 00184 00185 /** \brief Get a copy of the values of the models forcings 00186 * 00187 * \note Optionally a tree-vector is created in that case the caller of this 00188 * method is responsible for freeing that tree-vector. The input tree-vector 00189 * must be compatible (same size and or composition) as the models 00190 * internal tree-vector representing the forcings. 00191 * If the forcings of the model are not constant for the given timespan 00192 * the result is dependent on the model-implementation 00193 * \note If *hforc == CTA_NULL a new object is created, user is responsible for freeing this object. 00194 * 00195 * \param hmodel I handle of model instance 00196 * \param tspan I timespan for wich the given forcings are valid 00197 * \param hforc IO receives models forcings, *hforc can be CTA_NULL on calling (see note) 00198 * \return error status: CTA_OK if successful 00199 */ 00200 CTAEXPORT int CTA_Model_GetForc(CTA_Model hmodel, CTA_Time tspan, CTA_TreeVector *hforc); 00201 00202 /** \brief Perform axpy operation on the models forcings. 00203 * 00204 * \note AXPY: y=alpha*x+y. y corresponds to the models 00205 * internal forcings. 00206 * The adjustment to the forcings (alpha*x) is only valid for the given 00207 * time span. Note that the model will use y(t)+x for the given time span 00208 * where y(t) denotes the default forcings of the model. 00209 * 00210 * \param hmodel IO handle of model instance (y) 00211 * \param tspan I time span for wich the given forcings are valid 00212 * \param alpha I scalar 00213 * \param hx I handle of forcings tree-vector x 00214 * \return error status: CTA_OK if successful 00215 */ 00216 CTAEXPORT int CTA_Model_AxpyForc( CTA_Model hmodel, CTA_Time tspan, double alpha, CTA_TreeVector hx); 00217 00218 /** \brief Set parameters of the model. 00219 * 00220 * \param hmodel IO handle of model instance 00221 * \param hparam I handle of parameters vector 00222 * \return error status: CTA_OK if successful 00223 */ 00224 CTAEXPORT int CTA_Model_SetParam(CTA_Model hmodel, CTA_TreeVector hparam); 00225 00226 /** \brief Get a copy of the parameters of the model. 00227 * 00228 * \note Optionally a tree-vector is created in that case the caller of this 00229 * method is responsible for freeing that tree-vector. The input tree-vector 00230 * must be compatible (same size and or composition) as the models 00231 * internal tree-vector representing the parameters. 00232 * \note If *hforc == CTA_NULL a new object is created, user is responsible for freeing this object. 00233 * 00234 * \param hmodel I handle of model instance 00235 * \param hparam IO receives model forcings, *hforc can equal CTA_NULL on calling (see note) 00236 * \return error status: CTA_OK if successful 00237 */ 00238 CTAEXPORT int CTA_Model_GetParam(CTA_Model hmodel, CTA_TreeVector *hparam); 00239 00240 /** \brief Perform axpy operation on the models parameters. 00241 * 00242 * \note AXPY: y=alpha*x+y where y corresponds to the models 00243 * internal parameters. 00244 * 00245 * \param hmodel IO handle of model instance (y) 00246 * \param alpha I alpha 00247 * \param hx I handle of treevector of parameters (x) 00248 * \return error status: CTA_OK if successful 00249 */ 00250 CTAEXPORT int CTA_Model_AxpyParam( CTA_Model hmodel, double alpha, CTA_TreeVector hx); 00251 00252 /** \brief Return the timehorizon on the model. 00253 * The time horizon is the initial overal simulation span for which the mode is configured 00254 * 00255 * \param hmodel I handle of model instance 00256 * \param tHorizon I time horizon of model 00257 * \return error status: CTA_OK if successful 00258 */ 00259 CTAEXPORT int CTA_Model_GetTimeHorizon( CTA_Model hmodel, CTA_Time tHorizon); 00260 00261 /** \brief Return the current time of the model. 00262 * 00263 * \param hmodel I handle of model instance 00264 * \param tHorizon I time corresponding the the model state 00265 * \return error status: CTA_OK if successful 00266 */ 00267 CTAEXPORT int CTA_Model_GetCurrentTime( CTA_Model hmodel, CTA_Time tCurrent); 00268 00269 /** \brief Get covariance matrix of noise parameters. 00270 * 00271 * \note ONLY for Stochastic models. 00272 * The covariance matrix is represented by an array 00273 * of tree-vectors (columns of the matrix) 00274 * optionally a tree-vector is created in that case the caller of this 00275 * method is responsible for freeing that tree-vector. The input tree-vector 00276 * must be compatible (same size and or composition) as the models 00277 * internal tree-vector. 00278 * \note If hstmat[icol] == CTA_NULL a new object is created, user is responsible for freeing this object. 00279 * 00280 * \param hmodel I handle of model instance 00281 * \param hstmat O receives array of tree-vectors, *hstmat can equal CTA_NULL on calling (see note) 00282 * \return error status: CTA_OK if successful 00283 */ 00284 CTAEXPORT int CTA_Model_GetNoiseCovar(CTA_Model hmodel, CTA_TreeVector *hstmat); 00285 00286 /** \brief Get number of noise parameters: the number of columns of the noise covariance matrix. 00287 * 00288 * \note ONLY for Stochastic models. 00289 * 00290 * \param hmodel I handle of model instance 00291 * \param nnoise O receives number of noise parameters 00292 * \return error status: CTA_OK if successful 00293 */ 00294 CTAEXPORT int CTA_Model_GetNoiseCount(CTA_Model hmodel,int *nnoise); 00295 00296 /** \brief Free model instance. 00297 * 00298 * \note ONLY for Stochastic models. 00299 * 00300 * \param hmodel IO handle of model instance, replaced by CTA_NULL on return 00301 * \return error status: CTA_OK if successful 00302 */ 00303 CTAEXPORT int CTA_Model_Free(CTA_Model *hmodel); 00304 00305 /** \brief Announce to the model what observations will be requested. 00306 * 00307 * Before the compute method this method is used to announce what 00308 * obeservation will be requested after the CTA_Model_Compute using the 00309 * CTA_Model_GetObsvalues method. 00310 * 00311 * For some simulation models it is more efficient to do a single simulation 00312 * (a single CTA_Model_Compute call) for a particular simulation span then 00313 * simulating the same simulation span in a number of steps (multiple 00314 * CTA_Model_Compute calls). 00315 * 00316 * This method can be used to announce for what observations the model 00317 * must provide a prediction in advance. This method must be called prior 00318 * to the CTA_Compute method and makes it possible to perform simulations 00319 * over a longer time interval without the need to interupt the computations 00320 * in order to get the predictions at intermediate time instances. 00321 * 00322 * Notes on the behavior of the method: 00323 * - The observation description used in the first CTA_Model_GetObsValues 00324 * after the compute MUST be the same as the observation description 00325 * used in the announce. 00326 * - All observations that are announced MUST be in the timespan of the 00327 * following CTA_Model_Compute. 00328 * - The announced observations can only be retreved ONCE after the 00329 * CTA_Model_Compute. 00330 * - A CTA_Model_SetState or CTA_Model_AxpyState will reset the announced 00331 * CTA_Model_AnnounceObsValues administration (since stored predictions 00332 * might not be valid anymore) 00333 * 00334 * \param hmodel I handle of model instance 00335 * \param hdescr I observation description component 00336 * \return error status: CTA_OK if successful 00337 */ 00338 CTAEXPORT int CTA_Model_AnnounceObsValues(CTA_Model hmodel, CTA_ObsDescr hdescr); 00339 00340 /** \brief Get (interpolate) the models internal state to the 00341 * observations described as specified in the observation 00342 * description component. 00343 * 00344 * \note The interface supports a the time instance for time-interpolation. 00345 * It depends on the model whether and how this is supported. 00346 * 00347 * \param hmodel I handle of model instance 00348 * \param htime I time instance (for checking and time-interpolation if 00349 * supported by model) 00350 * \param hdescr I observation description component 00351 * \param values O receives values of the models internal state corresponding to 00352 * observations as described in hdescr 00353 * \return error status: CTA_OK if successful 00354 */ 00355 CTAEXPORT int CTA_Model_GetObsValues(CTA_Model hmodel, CTA_Time htime, 00356 CTA_ObsDescr hdescr, CTA_Vector values); 00357 00358 00359 /** \brief Get a query for the stochastic observer in order to 00360 * filter out the observations that can actually be provided by the model. 00361 * 00362 * \param hmodel I handle of model instance 00363 * \param htime I time instance 00364 * \param hdescr I observation description component 00365 * \param sselect O receives a query to filter out the observations, must exist before calling 00366 * \return error status: CTA_OK if successful 00367 */ 00368 CTAEXPORT int CTA_Model_GetObsSelect(CTA_Model hmodel, CTA_Time htime, 00369 CTA_ObsDescr hdescr, CTA_String sselect); 00370 00371 /** \brief Export the whole internal state of a model 00372 * This export function will export the whole state of the model such that 00373 * a so called "restart" start from this point yielding the same results. 00374 * There are no ruled on the format that is used to store the data. 00375 * Various extra otions are valid but a model will in most cases support an export 00376 * to a file and to a COSTA pack object. 00377 * 00378 * 00379 * \param hmodel I handle of model instance 00380 * \param hexport I target for export e.g. CTA_File or CTA_Pack 00381 * \return error status: CTA_OK if successful 00382 */ 00383 int CTA_Model_Export( CTA_Model hmodel, CTA_Handle hexport); 00384 00385 /** \brief Import the whole internal state of a model 00386 * After the inport the models internal state is exactly the same as the point that 00387 * the export was created using CTA_Model_Export. 00388 * 00389 * 00390 * \param hmodel I handle of model instance 00391 * \param himport I handle with data created by CTA_MODEL_Export e.g. CTA_File or CTA_Pack 00392 * \return error status: CTA_OK if successful 00393 */ 00394 int CTA_Model_Import( CTA_Model hmodel, CTA_Handle himport); 00395 00396 /** \brief 00397 * bla 00398 * HOMEWORK FOR JULIUS! 00399 * 00400 * \param hmodel I handle of model instance 00401 * 00402 * \return error status: CTA_OK if successful 00403 */ 00404 int CTA_Model_AdjSetointForc(CTA_Model hmodel, CTA_ObsDescr hdescr, CTA_Vector vforc); 00405 00406 /** \brief 00407 * bla 00408 * HOMEWORK FOR JULIUS! 00409 * 00410 * \param hmodel I handle of model instance 00411 * 00412 * \return error status: CTA_OK if successful 00413 */ 00414 int CTA_Model_AdjPrepare(CTA_Model hmodel, CTA_Time time); 00415 00416 /** \brief 00417 * bla 00418 * HOMEWORK FOR JULIUS! 00419 * 00420 * \param hmodel I handle of model instance 00421 * 00422 * \return error status: CTA_OK if successful 00423 */ 00424 int CTA_Model_AdjCompute(CTA_Model hmodel, CTA_Time time); 00425 00426 #ifdef __cplusplus 00427 } 00428 #endif 00429 00430 00431 #endif 00432