MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
module-udunits.cc File Reference
#include "mbconfig.h"
#include "mathp.h"
#include "dataman.h"
#include <udunits.h>
Include dependency graph for module-udunits.cc:

Go to the source code of this file.

Classes

class  UDUnitsNameSpace
 

Functions

static int unit_convert (const MathParser::MathArgs &args)
 
int module_init (const char *module_name, void *pdm, void *php)
 This function registers our user defined element for the math parser. More...
 

Function Documentation

int module_init ( const char *  module_name,
void *  pdm,
void *  php 
)

This function registers our user defined element for the math parser.

It is called when the "module load" statement appears in the input file.

Definition at line 226 of file module-udunits.cc.

References IncludeParser::GetFileName(), HighParser::GetMathParser(), HighParser::IsArg(), path, pHP, and MathParser::RegisterNameSpace().

227 {
228  MBDynParser *pHP = (MBDynParser *)php;
229 
230  const char *path = 0;
231  if (pHP->IsArg()) {
232  path = pHP->GetFileName();
233  }
234 
235  /* registers unit conversion namespace */
236  MathParser::NameSpace *pNS = new UDUnitsNameSpace(path);
237  int rc = pHP->GetMathParser().RegisterNameSpace(pNS);
238  if (rc != 0) {
239  delete pNS;
240  }
241  return rc;
242 }
int RegisterNameSpace(NameSpace *ns)
Definition: mathp.cc:4602
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
Definition: parsinc.cc:673
virtual MathParser & GetMathParser(void)
Definition: parser.cc:668
virtual bool IsArg(void)
Definition: parser.cc:807
static std::stack< const HighParser * > pHP
Definition: parser.cc:598
const char * path
Definition: autopilot.c:141

Here is the call graph for this function:

static int unit_convert ( const MathParser::MathArgs args)
static

Definition at line 62 of file module-udunits.cc.

References MathParser::AF_OPTIONAL_NON_PRESENT, ASSERT, MathParser::AT_REAL, MathParser::AT_STRING, MathParser::MathArg_t::IsFlag(), and MBDYN_EXCEPT_ARGS.

Referenced by UDUnitsNameSpace::UDUnitsNameSpace().

63 {
64  ASSERT(args.size() == 1 + 2 + 1);
65  ASSERT(args[0]->Type() == MathParser::AT_REAL);
66  ASSERT(args[1]->Type() == MathParser::AT_STRING);
67  ASSERT(args[2]->Type() == MathParser::AT_STRING);
68  ASSERT(args[3]->Type() == MathParser::AT_REAL);
69 
70  MathParser::MathArgReal_t *out = dynamic_cast<MathParser::MathArgReal_t *>(args[0]);
71  ASSERT(out != 0);
72 
73  MathParser::MathArgString_t *arg1 = dynamic_cast<MathParser::MathArgString_t *>(args[1]);
74  ASSERT(arg1 != 0);
75 
76  MathParser::MathArgString_t *arg2 = dynamic_cast<MathParser::MathArgString_t *>(args[2]);
77  ASSERT(arg2 != 0);
78 
79  MathParser::MathArgReal_t *arg3 = dynamic_cast<MathParser::MathArgReal_t *>(args[3]);
80  ASSERT(arg3 != 0);
81 
82  int rc;
83 
84  utUnit u_from;
85  rc = utScan((*arg1)().c_str(), &u_from);
86  switch (rc) {
87  case 0:
88  break;
89 
90  default:
91  /* TODO: handle specific errors */
92  silent_cerr("module-udunits: utScan could not interpret "
93  "unit \"" << (*arg1)() << "\" in first arg"
94  << std::endl);
96  }
97 
98  utUnit u_to;
99  rc = utScan((*arg2)().c_str(), &u_to);
100  switch (rc) {
101  case 0:
102  break;
103 
104  default:
105  silent_cerr("module-udunits: utScan could not interpret "
106  "unit \"" << (*arg2)() << "\" in second arg"
107  << std::endl);
109  }
110 
111  double slope, intercept;
112  rc = utConvert(&u_from, &u_to, &slope, &intercept);
113  switch (rc) {
114  case 0:
115  break;
116 
117  default:
118  silent_cerr("module-udunits: utConvert failed (rc=" << rc << ")" << std::endl);
120  }
121 
123  if (intercept != 0) {
124  silent_cerr("module-udunits: conversion between \"" << (*arg1)() << "\" "
125  "and \"" << (*arg2)() << "\" "
126  "has non-zero intercept" << std::endl);
128  }
129 
130  *out = slope;
131 
132  } else {
133  *out = (*arg3)()*slope + intercept;
134  }
135 
136  return 0;
137 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
#define ASSERT(expression)
Definition: colamd.c:977
bool IsFlag(const MathParser::ArgFlag f) const
Definition: mathp.h:98

Here is the call graph for this function: