Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 52646f2

Browse files
Jan ŠilarOpenModelica-Hudson
authored andcommitted
PDEModelica - GhostCells
almost finished but crashing
1 parent a0d00d8 commit 52646f2

File tree

1 file changed

+126
-34
lines changed

1 file changed

+126
-34
lines changed

Compiler/FrontEnd/InstUtil.mo

Lines changed: 126 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8217,12 +8217,11 @@ public function addGhostCells
82178217
input list<SCode.Equation> inEqs;
82188218
output list<tuple<SCode.Element, DAE.Mod>> outCompelts;
82198219
protected list<Absyn.Ident> fieldNamesP;
8220+
protected list<tuple<SCode.Element, DAE.Mod>> ghostCompelts;
82208221
algorithm
82218222
fieldNamesP := List.fold(inEqs, fieldsInPderEq, {});
8222-
//TODO: implement
8223-
8224-
8225-
outCompelts := inCompelts;
8223+
ghostCompelts := List.fold1(inCompelts,addGhostCells2,fieldNamesP,{});
8224+
outCompelts := listAppend(inCompelts,ghostCompelts);
82268225
end addGhostCells;
82278226

82288227
function fieldsInPderEq
@@ -8270,6 +8269,82 @@ algorithm
82708269
outExp := inExp;
82718270
end fieldInPderExp;
82728271

8272+
function addGhostCells2
8273+
//if name os given variable is in the given array
8274+
//adds ghost cells for it
8275+
input tuple<SCode.Element, DAE.Mod> inCompelt;
8276+
input list<Absyn.Ident> fieldNamesP;
8277+
input list<tuple<SCode.Element, DAE.Mod>> inGhosts;
8278+
output list<tuple<SCode.Element, DAE.Mod>> outGhosts;
8279+
algorithm
8280+
outGhosts := matchcontinue inCompelt
8281+
local
8282+
SCode.Ident name;
8283+
SCode.Prefixes prefixes;
8284+
SCode.Attributes attributes;
8285+
Absyn.TypeSpec typeSpec;
8286+
SCode.Mod modifications;
8287+
SCode.Comment comment;
8288+
Option<Absyn.Exp> condition;
8289+
SCode.SourceInfo info;
8290+
8291+
Absyn.ArrayDim arrayDims;
8292+
SCode.ConnectorType connectorType;
8293+
SCode.Parallelism parallelism;
8294+
SCode.Variability variability;
8295+
Absyn.Direction direction;
8296+
8297+
SCode.Final finalPrefix "final prefix";
8298+
SCode.Each eachPrefix "each prefix";
8299+
list<SCode.SubMod> subModLst;
8300+
Option<Absyn.Exp> binding;
8301+
SCode.SourceInfo info2;
8302+
8303+
DAE.Mod daeMod;
8304+
8305+
tuple<SCode.Element, DAE.Mod> ghostL, ghostR;
8306+
8307+
case (SCode.COMPONENT(name, prefixes,
8308+
SCode.ATTR(arrayDims,connectorType,parallelism,
8309+
variability, direction, Absyn.FIELD()
8310+
), typeSpec,
8311+
SCode.MOD(finalPrefix, eachPrefix,subModLst,binding,info2)/*modifications*/,
8312+
comment, condition, info),daeMod)
8313+
equation
8314+
listMember(name, fieldNamesP) = true;
8315+
8316+
//remove domain from the subModLst:
8317+
subModLst = List.filterOnFalse(subModLst,isSubModDomainOrStart);
8318+
8319+
ghostL = (SCode.COMPONENT(stringAppend(name,".ghostL"), prefixes,
8320+
SCode.ATTR(arrayDims,connectorType,parallelism,
8321+
variability, direction, Absyn.NONFIELD()), typeSpec,
8322+
SCode.MOD(finalPrefix, eachPrefix,subModLst,binding,info2),
8323+
comment, condition, info),daeMod);
8324+
ghostR = (SCode.COMPONENT(stringAppend(name,".ghostR"), prefixes,
8325+
SCode.ATTR(arrayDims,connectorType,parallelism,
8326+
variability, direction, Absyn.NONFIELD()), typeSpec,
8327+
SCode.MOD(finalPrefix, eachPrefix,subModLst,binding,info2),
8328+
comment, condition, info),daeMod);
8329+
then
8330+
ghostL::ghostR::inGhosts;
8331+
else
8332+
inGhosts;
8333+
end matchcontinue;
8334+
end addGhostCells2;
8335+
8336+
function isSubModDomainOrStart
8337+
input SCode.SubMod subMod;
8338+
output Boolean isNotDomain;
8339+
algorithm
8340+
isNotDomain := match subMod
8341+
case SCode.NAMEMOD(ident="domain") then true;
8342+
case SCode.NAMEMOD(ident="start") then true;
8343+
else false;
8344+
end match;
8345+
end isSubModDomainOrStart;
8346+
8347+
82738348
public function elabField
82748349
//For field variables: finds the "domain" modifier,
82758350
//finds domain.N - length of discretized field array
@@ -8304,7 +8379,7 @@ algorithm
83048379
equation
83058380
// DAE.MOD(finalPrefix = finalPrefix, eachPrefix = eachPrefix, subModLst = subModLst, eqModOption = eqModOption) = inMod;
83068381
//get N from the domain and remove domain from the subModLst:
8307-
(N, subModLst, SOME(dcr)) = List.fold30(subModLst,domainSearchFun,-1,{},NONE());
8382+
(N, subModLst, SOME(dcr)) = List.fold30(subModLst,domainSearchFunDAE,-1,{},NONE());
83088383
if (N == -1) then Error.addSourceMessageAndFail(Error.PDEModelica_ERROR,
83098384
{"Domain of the field variable '", name, "' not found."}, inInfo);
83108385
end if;
@@ -8317,7 +8392,7 @@ algorithm
83178392
end match;
83188393
end elabField;
83198394

8320-
protected function domainSearchFun
8395+
protected function domainSearchFunDAE
83218396
"fold function to find domain modifier in modifiers list"
83228397
//TODO: simplify this function, perhaps not use fold
83238398
input DAE.SubMod subMod;
@@ -8364,7 +8439,7 @@ protected function domainSearchFun
83648439
fail();
83658440
else (subMod::inSubModLst,inN,inCrOpt);
83668441
end matchcontinue;
8367-
end domainSearchFun;
8442+
end domainSearchFunDAE;
83688443

83698444
protected function findN
83708445
"a map function to find N in domain class modifiers"
@@ -8481,7 +8556,7 @@ algorithm
84818556
equation
84828557
(N,fieldLst) = getDomNFields(inDomFieldLst,domainCr,info);
84838558
// then list(newEQFun(i, lhs_exp, rhs_exp, domainCr, comment, info, fieldLst) for i in 2:N-1);
8484-
then creatFieldEqs(lhs_exp, rhs_exp, domainCr, N, comment, info, fieldLst);
8559+
then creatFieldEqs(lhs_exp, rhs_exp, domainCr, N, fieldLst, comment, info);
84858560
//same as previous but with ".interior"
84868561
case SCode.EQUATION(SCode.EQ_PDE(expLeft = lhs_exp, expRight = rhs_exp,
84878562
domain = domainCr as Absyn.CREF_QUAL(name, subscripts, Absyn.CREF_IDENT(name="interior")),
@@ -8490,7 +8565,7 @@ algorithm
84908565
domainCr1 = Absyn.CREF_IDENT(name, subscripts);
84918566
(N,fieldLst) = getDomNFields(inDomFieldLst,domainCr1,info);
84928567
// then list(newEQFun(i, lhs_exp, rhs_exp, domainCr1, comment, info, fieldLst) for i in 2:N-1);
8493-
then creatFieldEqs(lhs_exp, rhs_exp, domainCr, N, comment, info, fieldLst);
8568+
then creatFieldEqs(lhs_exp, rhs_exp, domainCr, N, fieldLst, comment, info);
84948569

84958570
//left boundary extrapolation
84968571
/* case SCode.EQUATION(SCode.EQ_PDE(expLeft = lhs_exp, expRight = rhs_exp,
@@ -8525,11 +8600,11 @@ algorithm
85258600
comment = comment, info = info))
85268601
equation
85278602
domainCr1 = Absyn.CREF_IDENT(name, subscripts);
8528-
(_,fieldLst) = getDomNFields(inDomFieldLst,domainCr1,info);
8603+
(N,fieldLst) = getDomNFields(inDomFieldLst,domainCr1,info);
85298604
(lhs_exp, _) = Absyn.traverseExp(lhs_exp, extrapFieldTraverseFun, 1);
85308605
(rhs_exp, _) = Absyn.traverseExp(rhs_exp, extrapFieldTraverseFun, 1);
85318606
then
8532-
{newEQFun(1, lhs_exp, rhs_exp, domainCr1, comment, info, fieldLst)};
8607+
{newEQFun(1, lhs_exp, rhs_exp, domainCr1, N, true, fieldLst, comment, info)};
85338608
//right boundary condition or extrapolation
85348609
case SCode.EQUATION(SCode.EQ_PDE(expLeft = lhs_exp, expRight = rhs_exp,
85358610
domain = Absyn.CREF_QUAL(name, subscripts, Absyn.CREF_IDENT(name="right")),
@@ -8540,7 +8615,7 @@ algorithm
85408615
(lhs_exp, _) = Absyn.traverseExp(lhs_exp, extrapFieldTraverseFun, N);
85418616
(rhs_exp, _) = Absyn.traverseExp(rhs_exp, extrapFieldTraverseFun, N);
85428617
then
8543-
{newEQFun(N, lhs_exp, rhs_exp, domainCr1, comment, info, fieldLst)};
8618+
{newEQFun(N, lhs_exp, rhs_exp, domainCr1, N, true, fieldLst, comment, info)};
85448619
end match;
85458620

85468621
outDiscretizedEQs := listAppend(inDiscretizedEQs, newDiscretizedEQs);
@@ -8588,7 +8663,7 @@ end extrapFieldTraverseFun;
85888663

85898664

85908665

8591-
8666+
/*
85928667
85938668
//TODO: remove never called:
85948669
protected function matchExtrapAndField
@@ -8615,7 +8690,7 @@ algorithm
86158690
end match;
86168691
86178692
end matchExtrapAndField;
8618-
8693+
*/
86198694
protected function getDomNFields
86208695
input DomainFieldsLst inDomFieldLst;
86218696
input Absyn.ComponentRef inDomainCr;
@@ -8718,9 +8793,9 @@ protected function creatFieldEqs "creates list of equations for fields. If the e
87188793
input Absyn.Exp rhs_exp;
87198794
input Absyn.ComponentRef domainCr;
87208795
input Integer N;
8796+
input List<Absyn.ComponentRef> fieldLst;
87218797
input SCode.Comment comment;
87228798
input SCode.SourceInfo info;
8723-
input List<Absyn.ComponentRef> fieldLst;
87248799
output List<SCode.Equation> outDiscretizedEQs;
87258800
protected Boolean bl, br;
87268801
algorithm
@@ -8731,12 +8806,13 @@ algorithm
87318806
//case ((_,false),(_,false)) //no pder()
87328807
case (false,false) //no pder()
87338808
then
8734-
list(newEQFun(i, lhs_exp, rhs_exp, domainCr, comment, info, fieldLst) for i in 1:N);
8809+
list(newEQFun(i, lhs_exp, rhs_exp, domainCr, N, false, fieldLst, comment, info) for i in 1:N);
87358810
else //contains some pder()
8736-
list(newEQFun(i, lhs_exp, rhs_exp, domainCr, comment, info, fieldLst) for i in 2:N-1);
8811+
list(newEQFun(i, lhs_exp, rhs_exp, domainCr, N, false, fieldLst, comment, info) for i in 1:N);
87378812
end match;
87388813
end creatFieldEqs;
87398814

8815+
87408816
protected function hasPderTraverseFun
87418817
input Absyn.Exp inExp;
87428818
input Boolean inHasPder;
@@ -8757,32 +8833,34 @@ protected function newEQFun
87578833
input Absyn.Exp inLhs_exp;
87588834
input Absyn.Exp inRhs_exp;
87598835
input Absyn.ComponentRef domainCr;
8836+
input Integer N;
8837+
input Boolean isBC; //-1 left ghost cell, 0 no ghost cell, 1 right ghost cell
8838+
input list<Absyn.ComponentRef> fieldLst;
87608839
input SCode.Comment comment;
87618840
input SCode.SourceInfo info;
8762-
input list<Absyn.ComponentRef> fieldLst;
87638841
output SCode.Equation outEQ;
87648842
protected Absyn.Exp outLhs_exp, outRhs_exp;
87658843
algorithm
8766-
(outLhs_exp, _) := Absyn.traverseExpTopDown(inLhs_exp,discretizeTraverseFun,(i,fieldLst,domainCr,info,false));
8767-
(outRhs_exp, _) := Absyn.traverseExpTopDown(inRhs_exp,discretizeTraverseFun,(i,fieldLst,domainCr,info,false));
8844+
(outLhs_exp, _) := Absyn.traverseExpTopDown(inLhs_exp,discretizeTraverseFun,(i,fieldLst,domainCr,info,false,N,isBC));
8845+
(outRhs_exp, _) := Absyn.traverseExpTopDown(inRhs_exp,discretizeTraverseFun,(i,fieldLst,domainCr,info,false,N,isBC));
87688846
outEQ := SCode.EQUATION(SCode.EQ_EQUALS(outLhs_exp, outRhs_exp, comment, info));
87698847
end newEQFun;
87708848

87718849
protected function discretizeTraverseFun
87728850
input Absyn.Exp inExp;
8773-
input tuple<Integer, list<Absyn.ComponentRef>, Absyn.ComponentRef, SCode.SourceInfo,Boolean> inTup;
8851+
input tuple<Integer, list<Absyn.ComponentRef>, Absyn.ComponentRef, SCode.SourceInfo,Boolean,Integer,Boolean> inTup;
87748852
output Absyn.Exp outExp;
8775-
output tuple<Integer, list<Absyn.ComponentRef>, Absyn.ComponentRef, SCode.SourceInfo,Boolean> outTup;
8776-
protected Integer i;
8853+
output tuple<Integer, list<Absyn.ComponentRef>, Absyn.ComponentRef, SCode.SourceInfo,Boolean,Integer,Boolean> outTup;
8854+
protected Integer i,N;
87778855
// protected String eqDomainName;
87788856
protected list<Absyn.ComponentRef> fieldLst;
87798857
protected SCode.SourceInfo info;
8780-
protected Boolean skip, failVar;
8858+
protected Boolean skip, failVar,isBC;
87818859
protected Absyn.ComponentRef domainCr;
87828860
protected Absyn.Ident domName;
87838861
algorithm
87848862
failVar := false;
8785-
(i, fieldLst, domainCr, info, skip) := inTup;
8863+
(i, fieldLst, domainCr, info, skip, N, isBC) := inTup;
87868864
Absyn.CREF_IDENT(name = domName) := domainCr;
87878865
if skip then
87888866
outExp := inExp;
@@ -8794,6 +8872,7 @@ algorithm
87948872
Absyn.Ident name, fieldDomainName;
87958873
list<Absyn.Subscript> subscripts;
87968874
Absyn.ComponentRef fieldCr;
8875+
Absyn.Exp exp, leftVar, rightVar;
87978876
case Absyn.CREF(Absyn.CREF_QUAL(name = domName, subscripts = {}, componentRef=Absyn.CREF_IDENT(name="x",subscripts={})))
87988877
//coordinate x
87998878
then
@@ -8802,23 +8881,36 @@ algorithm
88028881
//field
88038882
equation
88048883
true = List.isMemberOnTrue(fieldCr,fieldLst,Absyn.crefEqual);
8805-
then
8806-
Absyn.CREF(Absyn.CREF_IDENT(name, Absyn.SUBSCRIPT(Absyn.INTEGER(i))::subscripts));
8884+
exp = (if isBC and i == 1 then
8885+
Absyn.CREF(Absyn.CREF_IDENT(stringAppend(name,".ghostL"), subscripts)) //left BC
8886+
elseif isBC and i == N then
8887+
Absyn.CREF(Absyn.CREF_IDENT(stringAppend(name,".ghostR"), subscripts)) //right BC
8888+
else
8889+
Absyn.CREF(Absyn.CREF_IDENT(name, Absyn.SUBSCRIPT(Absyn.INTEGER(i))::subscripts)) //no BC
8890+
);
8891+
then
8892+
exp;
88078893
case Absyn.CALL(Absyn.CREF_IDENT("pder",{}),Absyn.FUNCTIONARGS({Absyn.CREF(fieldCr as Absyn.CREF_IDENT(name, subscripts)),Absyn.CREF(Absyn.CREF_IDENT(name="x"))},_))
88088894
//pder
88098895
equation
88108896
if not List.isMemberOnTrue(fieldCr,fieldLst,Absyn.crefEqual) then
88118897
failVar = true;
88128898
Error.addSourceMessageAndFail(Error.COMPILER_ERROR,{"Field variable '" + name + "' has different domain than the equation or is not a field." }, info);
88138899
end if;
8814-
skip = true;
8900+
skip = true;
8901+
leftVar = (if i == 1 then
8902+
Absyn.CREF(Absyn.CREF_IDENT(stringAppend(name,".ghostL"), subscripts))
8903+
else
8904+
Absyn.CREF(Absyn.CREF_IDENT(name, Absyn.SUBSCRIPT(Absyn.INTEGER(i+1))::subscripts))
8905+
);
8906+
rightVar = (if i == N then
8907+
Absyn.CREF(Absyn.CREF_IDENT(stringAppend(name,".ghostR"), subscripts))
8908+
else
8909+
Absyn.CREF(Absyn.CREF_IDENT(name, Absyn.SUBSCRIPT(Absyn.INTEGER(i+1))::subscripts))
8910+
);
88158911
then
88168912
Absyn.BINARY(
8817-
Absyn.BINARY(
8818-
Absyn.CREF(Absyn.CREF_IDENT(name, Absyn.SUBSCRIPT(Absyn.INTEGER(i+1))::subscripts)),
8819-
Absyn.SUB(),
8820-
Absyn.CREF(Absyn.CREF_IDENT(name, Absyn.SUBSCRIPT(Absyn.INTEGER(i-1))::subscripts))
8821-
),
8913+
Absyn.BINARY(leftVar, Absyn.SUB(), rightVar),
88228914
Absyn.DIV(),
88238915
Absyn.BINARY(
88248916
Absyn.INTEGER(2),
@@ -8843,7 +8935,7 @@ algorithm
88438935
if failVar then
88448936
fail();
88458937
end if;
8846-
outTup := (i, fieldLst, domainCr, info, skip);
8938+
outTup := (i, fieldLst, domainCr, info, skip, N, isBC);
88478939
end discretizeTraverseFun;
88488940

88498941
protected function findDomF<T>

0 commit comments

Comments
 (0)