Skip to content

Commit

Permalink
Calculate unit information for state derivatives
Browse files Browse the repository at this point in the history
See ticket:2750
  • Loading branch information
lochel authored and OpenModelica-Hudson committed May 24, 2016
1 parent 7271c6f commit 22d2ccb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 63 deletions.
41 changes: 38 additions & 3 deletions Compiler/BackEnd/Unit.mo
Expand Up @@ -37,10 +37,15 @@ encapsulated package Unit
authors: Jan Hagemann and Lennart Ochel (FH Bielefeld, Germany)"


public import DAE;
public
import DAE;
import System;

protected import ComponentReference;
public import System;
protected
import ComponentReference;
import HashTableStringToUnit;
import HashTableUnitToString;
import Util;


public uniontype Unit
Expand Down Expand Up @@ -116,9 +121,39 @@ public constant list<tuple<String, Unit>> LU_COMPLEXUNITS = {
//("degC", UNIT(1e0, 0, 0, 0, 0, 0, 1, 0, 273.15))};//°Celsius
/* fac, mol, cd, m, s, A, K, g*/

public function getKnownUnits
output HashTableStringToUnit.HashTable outKnownUnits;
algorithm
outKnownUnits := HashTableStringToUnit.emptyHashTableSized(Util.nextPrime(2 * listLength(LU_COMPLEXUNITS)));

for unit in LU_COMPLEXUNITS loop
outKnownUnits := BaseHashTable.add(unit, outKnownUnits);
end for;
end getKnownUnits;

public function getKnownUnitsInverse
output HashTableUnitToString.HashTable outKnownUnitsInverse;
protected
String s;
Unit ut;
algorithm
outKnownUnitsInverse := HashTableUnitToString.emptyHashTableSized(Util.nextPrime(2 * listLength(LU_COMPLEXUNITS)));

for unit in LU_COMPLEXUNITS loop
(s, ut) := unit;
outKnownUnitsInverse := BaseHashTable.add((ut, s), outKnownUnitsInverse);
end for;
end getKnownUnitsInverse;

public function isUnit
input Unit inUnit;
output Boolean b;
algorithm
b := match inUnit
case UNIT() then true;
else false;
end match;
end isUnit;

public function hashUnitMod
input Unit inKey;
Expand Down
89 changes: 36 additions & 53 deletions Compiler/BackEnd/UnitCheck.mo
Expand Up @@ -74,8 +74,6 @@ protected uniontype Token
record T_RPAREN end T_RPAREN;
end Token;



// =============================================================================
// section for preOptModule >>unitChecking<<
// The unit check module verifies the consistency of units.
Expand Down Expand Up @@ -104,8 +102,8 @@ algorithm
eqList := BackendEquation.equationList(syst.orderedEqs);

HtCr2U1 := HashTableCrToUnit.emptyHashTableSized(2053);
HtS2U := foldComplexUnits(HashTableStringToUnit.emptyHashTableSized(2053));
HtU2S := foldComplexUnits2(HashTableUnitToString.emptyHashTableSized(2053));
HtS2U := Unit.getKnownUnits();
HtU2S := Unit.getKnownUnitsInverse();

if Flags.isSet(Flags.DUMP_EQ_UNIT) then
BackendDump.dumpEquationList(eqList, "########### Equation-Liste: #########\n");
Expand Down Expand Up @@ -145,17 +143,6 @@ algorithm
end try;
end unitChecking;

//
//
protected function foldComplexUnits
input HashTableStringToUnit.HashTable inHtS2U;
output HashTableStringToUnit.HashTable outHtS2U;
algorithm
outHtS2U := List.fold(Unit.LU_COMPLEXUNITS, addUnit2HtS2U, inHtS2U);
end foldComplexUnits;

//
//
protected function addUnit2HtS2U
input tuple<String, Unit.Unit> inTpl;
input HashTableStringToUnit.HashTable inHtS2U;
Expand All @@ -164,17 +151,6 @@ algorithm
outHtS2U := BaseHashTable.add(inTpl,inHtS2U);
end addUnit2HtS2U;

//
//
protected function foldComplexUnits2
input HashTableUnitToString.HashTable inHtU2S;
output HashTableUnitToString.HashTable outHtU2S;
algorithm
outHtU2S := List.fold(Unit.LU_COMPLEXUNITS, addUnit2HtU2S, inHtU2S);
end foldComplexUnits2;

//
//
protected function addUnit2HtU2S
input tuple<String, Unit.Unit> inTpl;
input HashTableUnitToString.HashTable inHtU2S;
Expand Down Expand Up @@ -225,9 +201,9 @@ end returnVar;

//
//
protected function unit2String "transforms the unit in string"
public function unit2String "transforms the unit in string"
input Unit.Unit inUt;
input HashTableUnitToString.HashTable inHtU2S;
input HashTableUnitToString.HashTable inHtU2S = Unit.getKnownUnitsInverse();
output String outS;
algorithm
outS := matchcontinue(inUt, inHtU2S)
Expand Down Expand Up @@ -1211,6 +1187,23 @@ algorithm
end match;
end tokenList2string;

public function parseUnitString "author: lochel
The second argument is optional."
input String inUnitString;
input HashTableStringToUnit.HashTable inKnownUnits = Unit.getKnownUnits();
output Unit.Unit outUnit;
protected
list<String> charList;
list<Token> tokenList;
algorithm
if inUnitString == "" then
fail();
end if;
charList := stringListStringChar(inUnitString);
tokenList := lexer(charList);
outUnit := parser(tokenList, Unit.UPDATECREF, inKnownUnits);
end parseUnitString;

//
//
protected function parse "author: lochel"
Expand All @@ -1219,31 +1212,21 @@ protected function parse "author: lochel"
input HashTableStringToUnit.HashTable inHtS2U;
input HashTableUnitToString.HashTable inHtU2S;
output Unit.Unit outUnit;
output HashTableStringToUnit.HashTable outHtS2U;
output HashTableUnitToString.HashTable outHtU2S;
output HashTableStringToUnit.HashTable outHtS2U = inHtS2U;
output HashTableUnitToString.HashTable outHtU2S = inHtU2S;
protected
list<String> charList;
list<Token> tokenList;
algorithm
(outUnit, outHtS2U, outHtU2S):=matchcontinue(inUnitString, inCref, inHtS2U, inHtU2S)

local
Unit.Unit unit;
HashTableStringToUnit.HashTable HtS2U;
HashTableUnitToString.HashTable HtU2S;
list<String> charList;
list<Token> tokenList;


case (_, _, _, _) equation
unit=BaseHashTable.get(inUnitString, inHtS2U);
then (unit, inHtS2U, inHtU2S);

else equation
charList = stringListStringChar(inUnitString);
tokenList = lexer(charList);
unit = parser(tokenList, inCref, inHtS2U);
HtS2U = addUnit2HtS2U((inUnitString, unit), inHtS2U);
HtU2S = addUnit2HtU2S((inUnitString, unit), inHtU2S);
then (unit, HtS2U, HtU2S);
end matchcontinue;
try
outUnit := BaseHashTable.get(inUnitString, inHtS2U);
else
charList := stringListStringChar(inUnitString);
tokenList := lexer(charList);
outUnit := parser(tokenList, inCref, inHtS2U);
outHtS2U := addUnit2HtS2U((inUnitString, outUnit), inHtS2U);
outHtU2S := addUnit2HtU2S((inUnitString, outUnit), inHtU2S);
end try;
end parse;

//
Expand Down Expand Up @@ -1669,7 +1652,7 @@ end unitMul;

//
//
protected function unitDiv
public function unitDiv
input Unit.Unit inUt1;
input Unit.Unit inUt2;
output Unit.Unit outUt;
Expand Down
27 changes: 20 additions & 7 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -46,30 +46,31 @@ import DAE;
import FCore;
import FGraph;
import HashTable;
import HashTableCrILst;
import HashTableCrIListArray;
import HashTableCrILst;
import HashTableExpToIndex;
import SCode;
import SimCode;
import SimCodeVar;
import Tpl;
import Types;
import Unit;
import Values;
import SimCode;
import SimCodeVar;
import Vectorization;

// protected imports
protected
import Array;
import BackendDAEEXT;
import BackendDAEOptimize;
import BackendDAETransform;
import BackendDAEUtil;
import BackendDump;
import BackendEquation;
import BackendDAEEXT;
import BackendVariable;
import BackendVarTransform;
import BaseHashTable;
import BaseHashSet;
import BaseHashTable;
import Builtin;
import CheckModel;
import ClassInf;
Expand All @@ -92,19 +93,22 @@ import Flags;
import FMI;
import Graph;
import HashSet;
import HashTableStringToUnit;
import HashTableUnitToString;
import HpcOmSimCode;
import Inline;
import List;
import Matching;
import MetaModelica.Dangerous;
import PriorityQueue;
import SimCodeDump;
import TaskSystemDump;
import SimCodeFunctionUtil;
import SimCodeFunctionUtil.varName;
import Sorting;
import SymbolicJacobian;
import System;
import TaskSystemDump;
import UnitCheck;
import Util;
import ValuesUtil;
import VisualXML;
Expand Down Expand Up @@ -6735,11 +6739,20 @@ end addSimVar;
protected function derVarFromStateVar
input SimCodeVar.SimVar state;
output SimCodeVar.SimVar deriv = state;
protected
Unit.Unit unit;
algorithm
deriv.arrayCref := Util.applyOption(deriv.arrayCref, ComponentReference.crefPrefixDer);
deriv.name := ComponentReference.crefPrefixDer(deriv.name);
deriv.varKind := BackendDAE.STATE_DER();
deriv.unit := "";
try
unit := UnitCheck.parseUnitString(deriv.unit);
unit := UnitCheck.unitDiv(unit, Unit.UNIT(1e0, 0, 0, 0, 1, 0, 0, 0));
true := Unit.isUnit(unit);
deriv.unit := UnitCheck.unit2String(unit);
else
deriv.unit := "";
end try;
deriv.displayUnit := "";
deriv.initialValue := NONE();
deriv.aliasvar := SimCodeVar.NOALIAS();
Expand Down

0 comments on commit 22d2ccb

Please sign in to comment.