model.c 7.89 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "dmemory.h"
#include "cdi.h"
#include "stream_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
9
10
11
#include "pio_util.h"
#include "resource_handle.h"
#include "pio_rpc.h"
#include "namespace.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
13
14
15
16
17

#undef  UNDEFID
#define UNDEFID -1

int ECHAM4 = UNDEFID;
int ECHAM5 = UNDEFID;
18
int COSMO  = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
20
21

typedef struct
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
  int      self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23
24
  int      used;
  int      instID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
  int      modelgribID;
  char    *name;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
model_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30


Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
static int  MODEL_Debug = 0;   /* If set to 1, debugging */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
static int * modelInitializedNsp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33

Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
static void modelInit(void);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36


Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
41
static int    modelCompareP ( void * modelptr1, void * modelptr2 );
static void   modelDestroyP ( void * modelptr );
static void   modelPrintP   ( void * modelptr, FILE * fp );
#ifdef USE_MPI
static int    modelGetSizeP ( void * modelptr, MPI_Comm comm );
42
static void   modelPackP    ( void * modelptr, void * buff, int size, int *position, MPI_Comm comm);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
static int    modelTxCode   ( void );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
48
49
50
resOps modelOps = { modelCompareP, modelDestroyP, modelPrintP
#ifdef USE_MPI
                    , modelGetSizeP, modelPackP, modelTxCode
#endif
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51

Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
void modelDefaultValue ( model_t *modelptr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56
  modelptr->self        = UNDEFID;
  modelptr->used        = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
58
59
  modelptr->instID      = UNDEFID;
  modelptr->modelgribID = UNDEFID;
  modelptr->name        = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
61
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
63
model_t *modelNewEntry ( void )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
  model_t *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66

Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
69
  modelptr = (model_t *) xmalloc(sizeof(model_t));
  modelDefaultValue ( modelptr );
  modelptr->self = reshPut (( void * ) modelptr, &modelOps );
70
  modelptr->used = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
72
73
74
75
76
77

  return (modelptr);
}


int modelDef(int instID, int modelgribID, const char *name);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
void modelDefaultEntries ( void )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
82
  int instID, i;
  cdiResH resH[10];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
84

  instID  = institutInq(  0,   0, "ECMWF", NULL);
85
86
  /* (void)    modelDef(instID, 131, "ERA15"); */
  /* (void)    modelDef(instID, 199, "ERA40"); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
  instID  = institutInq(  0,   0, "MPIMET", NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88
89
90
91
92

  resH[0] = ECHAM5  = modelDef(instID,  64, "ECHAM5.4");
  resH[1] = modelDef(instID,  63, "ECHAM5.3");
  resH[2] = modelDef(instID,  62, "ECHAM5.2");
  resH[3] = modelDef(instID,  61, "ECHAM5.1");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
94

  instID  = institutInq( 98, 255, "MPIMET", NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96
97
  resH[4] = modelDef(instID,  60, "ECHAM5.0");
  resH[5] = ECHAM4  = modelDef(instID,  50, "ECHAM4");
  resH[6] = modelDef(instID, 110, "MPIOM1");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
99

  instID  = institutInq(  0,   0, "DWD", NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
  resH[7] = modelDef(instID, 149, "GME");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101

102
  instID  = institutInq(  0,   0, "MCH", NULL);
103
  //(void)  = modelDef(instID, 137, "COSMO");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
  resH[8] = COSMO   = modelDef(instID, 255, "COSMO");
105

Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
  instID  = institutInq(  0,   1, "NCEP", NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
109
110
111
  resH[9] = modelDef(instID,  80, "T62L28MRF");
  
  if ( ! namespaceHasLocalFile ( namespaceGetActive ()))
    for ( i = 0; i < 10 ; i++ )
      reshSetStatus ( resH[i], &modelOps, SUSPENDED );  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112
113
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
void modelFinalize ( void )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
  free (   modelInitializedNsp );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
void modelInit(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
  static int modelInitialized = 0;
  int nsp, nspc;
  char *env;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126

Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
  nspc = namespaceGetNumber ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130
131
132
133
134
135
136
  if ( !modelInitialized )
    {
      modelInitialized = 1;
      modelInitializedNsp = xcalloc ( 1, nspc * sizeof ( int ));
      atexit ( modelFinalize );
      env = getenv("MODEL_DEBUG");
      if ( env ) MODEL_Debug = atoi(env);  
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137

Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
139
140
141
142
143
144
145
  nsp = namespaceGetActive ();
  
  if ( modelInitializedNsp[nsp] ) return;
  
  modelInitializedNsp[nsp] = 1;
  
  modelDefaultEntries ();
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146

Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
148
149
int modelSize ( void )
{
  return reshCountType ( &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
151
152
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
155
156
157
int modelInq(int instID, int modelgribID, char *name)
{
  int modelID = UNDEFID;
  size_t len;
  int found;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
  int modelCount;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
  model_t *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
  int i, * modelResHs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161

Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163

Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
165
166
  modelCount = modelSize();
  modelResHs = xmalloc ( modelCount * sizeof ( int ));
  reshGetResHListOfType ( modelCount, modelResHs, &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167

Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
  for( i = 0; i < modelCount; i++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
      modelID = modelResHs[i];
      modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
173

      if ( modelptr->used )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
        {
175
          if ( name && *name )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178
179
180
181
182
183
184
185
            {
              found = 1;
              if ( instID      != -1 && modelptr->instID      != instID )      found = 0;
              if ( modelgribID !=  0 && modelptr->modelgribID != modelgribID ) found = 0;

              if ( found )
                {
                  if ( modelptr->name )
                    {
                      len = strlen(modelptr->name);
186
                      if ( strncmp(modelptr->name, name, len) == 0 ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
                      len = strlen(name);
188
                      if ( strncmp(modelptr->name, name, len) == 0 ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
190
191
192
193
194
195
196
197
                    }
                }
            }
          else
            {
              if ( modelptr->instID      == instID &&
                   modelptr->modelgribID == modelgribID ) break;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
  if ( i == modelCount ) modelID = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202

  if ( modelResHs ) free ( modelResHs );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
204
205
206

  return (modelID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
207

Uwe Schulzweida's avatar
Uwe Schulzweida committed
208
209
int modelDef(int instID, int modelgribID, const char *name)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
  model_t *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211

Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213

Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
  modelptr = modelNewEntry();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215

Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
217
  modelptr->instID      = instID;
  modelptr->modelgribID = modelgribID;
218
  if ( name && *name ) modelptr->name = strdupx(name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219

Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
  return modelptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
223
224
225
}


int modelInqInstitut(int modelID)
{
226
  model_t *modelptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227

Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229

230
231
  if ( modelID != UNDEFID )
    modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232

Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
  return modelptr ? modelptr->instID : UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
235
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
236

Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
int modelInqGribID(int modelID)
{
239
  model_t *modelptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240

Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242

243
244
  if ( modelID != UNDEFID )
    modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245

Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
  return modelptr ? modelptr->modelgribID : UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
249

Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
251
char *modelInqNamePtr(int modelID)
{
252
  model_t *modelptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253

Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255

256
257
  if ( modelID != UNDEFID )
    modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258

Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  return modelptr ? modelptr->name : NULL;
}


int  modelCompareP ( void * modelptr1, void * modelptr2 )
{
  return 0;
}


void modelDestroyP ( void * modelptr )
{
}


void modelPrintP   ( void * modelptr, FILE * fp )
{
  model_t * mp = ( model_t * ) modelptr;

  if ( !mp ) return;

  fprintf ( fp, "#\n");
  fprintf ( fp, "# modelID %d\n", mp->self);
  fprintf ( fp, "#\n");
  fprintf ( fp, "self          = %d\n", mp->self );
  fprintf ( fp, "used          = %d\n", mp->used );
  fprintf ( fp, "instID        = %d\n", mp->instID );
  fprintf ( fp, "modelgribID   = %d\n", mp->modelgribID );
  fprintf ( fp, "name          = %s\n", mp->name ? mp->name : "NN" );
}


#ifdef USE_MPI
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292

Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295
296
static int
modelTxCode ( void )
{
  return MODEL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

enum {
  model_nints = 4,
};


static
int modelGetSizeP ( void * modelptr, MPI_Comm comm )
{
  model_t *p = modelptr;
  int txsize = 0, txinc;
  xmpi(MPI_Pack_size(model_nints, MPI_INT, comm, &txsize));
  xmpi(MPI_Pack_size(strlen(p->name) + 1, MPI_CHAR, comm, &txinc));
  txsize += txinc;
  return txsize;
}


static
void modelPackP ( void * modelptr, void * buf, int size,
                  int *position, MPI_Comm comm )
{
  model_t *p = modelptr;
  int tempbuf[model_nints];
  tempbuf[0] = p->self;
  tempbuf[1] = p->instID;
  tempbuf[2] = p->modelgribID;
  tempbuf[3] = (int)strlen(p->name) + 1;
  xmpi(MPI_Pack(tempbuf, model_nints, MPI_INT, buf, size, position, comm));
  xmpi(MPI_Pack(p->name, tempbuf[3], MPI_CHAR, buf, size, position, comm));
}

int
modelUnpack(void *buf, int size, int *position, int nspTarget,
            MPI_Comm comm)
{
  int tempbuf[model_nints];
  int modelID;
  char *name;
  xmpi(MPI_Unpack(buf, size, position, tempbuf, model_nints, MPI_INT, comm));
  name = xmalloc(tempbuf[3]);
  xmpi(MPI_Unpack(buf, size, position, name, tempbuf[3], MPI_CHAR, comm));
  modelID = modelDef( namespaceAdaptKey ( tempbuf[1], nspTarget ), tempbuf[2], name);
  // FIXME: this should work, once all types are transferred
  //assert(modelID == tempbuf[0]);
  return modelID;
}

#endif
347
348
349
350
351
352
353
354
355
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */