gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
MathEval.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include "GmshConfig.h"
7 #include "GmshDefines.h"
8 #include "MathEval.h"
9 #include "mathEvaluator.h"
10 #include "OctreePost.h"
11 #include "GEntity.h"
12 #include <algorithm>
13 
15  {GMSH_FULLRC, "TimeStep", nullptr, -1.},
16  {GMSH_FULLRC, "View", nullptr, -1.},
17  {GMSH_FULLRC, "OtherTimeStep", nullptr, -1.},
18  {GMSH_FULLRC, "OtherView", nullptr, -1.},
19  {GMSH_FULLRC, "ForceInterpolation", nullptr, 0.},
20  {GMSH_FULLRC, "PhysicalRegion", nullptr, -1.}};
21 
23  {GMSH_FULLRC, "Expression0", nullptr, "Sqrt(v0^2+v1^2+v2^2)"},
24  {GMSH_FULLRC, "Expression1", nullptr, ""},
25  {GMSH_FULLRC, "Expression2", nullptr, ""},
26  {GMSH_FULLRC, "Expression3", nullptr, ""},
27  {GMSH_FULLRC, "Expression4", nullptr, ""},
28  {GMSH_FULLRC, "Expression5", nullptr, ""},
29  {GMSH_FULLRC, "Expression6", nullptr, ""},
30  {GMSH_FULLRC, "Expression7", nullptr, ""},
31  {GMSH_FULLRC, "Expression8", nullptr, ""}};
32 
33 extern "C" {
35 }
36 
37 std::string GMSH_MathEvalPlugin::getHelp() const
38 {
39  return "Plugin(MathEval) creates a new view using "
40  "data from the time step `TimeStep' in the view "
41  "`View'.\n\n"
42  "If only `Expression0' is given (and `Expression1', "
43  "..., `Expression8' are all empty), the plugin "
44  "creates a scalar view. If `Expression0', `Expression1' "
45  "and/or `Expression2' are given (and `Expression3', "
46  "..., `Expression8' are all empty) the plugin creates "
47  "a vector view. Otherwise the plugin creates a tensor "
48  "view.\n\n"
49  "In addition to the usual mathematical functions "
50  "(Exp, Log, Sqrt, Sin, Cos, Fabs, etc.) and operators "
51  "(+, -, *, /, ^), all expressions can contain:\n\n"
52  "- the symbols v0, v1, v2, ..., vn, which represent "
53  "the n components in `View';\n\n"
54  "- the symbols w0, w1, w2, ..., wn, which represent "
55  "the n components of `OtherView', at time step "
56  "`OtherTimeStep';\n\n"
57  "- the symbols x, y and z, which represent the three "
58  "spatial coordinates.\n\n"
59  "If `TimeStep' < 0, the plugin extracts data from all "
60  "the time steps in the view.\n\n"
61  "If `View' < 0, the plugin is run on the current view.\n\n"
62  "Plugin(MathEval) creates one new view."
63  "If `PhysicalRegion' < 0, the plugin is run "
64  "on all physical regions.\n\n"
65  "Plugin(MathEval) creates one new list-based view.";
66 }
67 
69 {
70  return sizeof(MathEvalOptions_Number) / sizeof(StringXNumber);
71 }
72 
74 {
75  return &MathEvalOptions_Number[iopt];
76 }
77 
79 {
80  return sizeof(MathEvalOptions_String) / sizeof(StringXString);
81 }
82 
84 {
85  return &MathEvalOptions_String[iopt];
86 }
87 
89 {
90  int timeStep = (int)MathEvalOptions_Number[0].def;
91  int iView = (int)MathEvalOptions_Number[1].def;
92  int otherTimeStep = (int)MathEvalOptions_Number[2].def;
93  int iOtherView = (int)MathEvalOptions_Number[3].def;
94  int forceInterpolation = (int)MathEvalOptions_Number[4].def;
95  int physicalRegion = (int)MathEvalOptions_Number[5].def;
96  std::vector<std::string> expr(9);
97  for(int i = 0; i < 9; i++) expr[i] = MathEvalOptions_String[i].def;
98 
99  PView *v1 = getView(iView, view);
100  if(!v1) return view;
101  PViewData *data1 = getPossiblyAdaptiveData(v1);
102 
103  if(data1->hasMultipleMeshes()) {
104  Msg::Error("MathEval plugin cannot be applied to multi-mesh views");
105  return view;
106  }
107 
108  PView *otherView = v1;
109  if(iOtherView >= 0) {
110  otherView = getView(iOtherView, view);
111  if(!otherView) {
112  Msg::Error("MathEval plugin could not find other view %i", iOtherView);
113  return view;
114  }
115  }
116 
117  PViewData *otherData = getPossiblyAdaptiveData(otherView);
118  if(otherData->hasMultipleMeshes()) {
119  Msg::Error("MathEval plugin cannot be applied to multi-mesh views");
120  return view;
121  }
122 
123  if(otherTimeStep < 0 &&
124  otherData->getNumTimeSteps() != data1->getNumTimeSteps()) {
125  Msg::Error("Number of time steps don't match: using step 0");
126  otherTimeStep = 0;
127  }
128  else if(otherTimeStep > otherData->getNumTimeSteps() - 1) {
129  Msg::Error("Invalid time step (%d) in View[%d]: using step 0 instead",
130  otherTimeStep, otherView->getIndex());
131  otherTimeStep = 0;
132  }
133 
134  int numComp2;
135  if(expr[3].size() || expr[4].size() || expr[5].size() || expr[6].size() ||
136  expr[7].size() || expr[8].size()) {
137  numComp2 = 9;
138  for(int i = 0; i < 9; i++)
139  if(expr[i].empty()) expr[i] = "0";
140  }
141  else if(expr[1].size() || expr[2].size()) {
142  numComp2 = 3;
143  for(int i = 0; i < 3; i++)
144  if(expr[i].empty()) expr[i] = "0";
145  }
146  else {
147  numComp2 = 1;
148  }
149  expr.resize(numComp2);
150 
151  const char *names[] = {"x", "y", "z", "v0", "v1", "v2", "v3",
152  "v4", "v5", "v6", "v7", "v8", "w0", "w1",
153  "w2", "w3", "w4", "w5", "w6", "w7", "w8"};
154  std::size_t numVariables = sizeof(names) / sizeof(names[0]);
155  std::vector<std::string> variables(numVariables);
156  for(std::size_t i = 0; i < numVariables; i++) variables[i] = names[i];
157  mathEvaluator f(expr, variables);
158  if(expr.empty()) return view;
159  std::vector<double> values(numVariables), res(numComp2);
160 
161  OctreePost *octree = nullptr;
162  if(forceInterpolation ||
163  (data1->getNumEntities() != otherData->getNumEntities()) ||
164  (data1->getNumElements() != otherData->getNumElements())) {
165  Msg::Info("Other view based on different grid: interpolating...");
166  octree = new OctreePost(otherView);
167  }
168 
169  PView *v2 = new PView();
170  PViewDataList *data2 = getDataList(v2);
171 
172  if(timeStep < 0) { timeStep = -data1->getNumTimeSteps(); }
173  else if(timeStep > data1->getNumTimeSteps() - 1) {
174  Msg::Error("Invalid time step (%d) in View[%d]: using all steps instead",
175  timeStep, v1->getIndex());
176  timeStep = -data1->getNumTimeSteps();
177  }
178 
179  int firstNonEmptyStep = data1->getFirstNonEmptyTimeStep();
180  int timeBeg = (timeStep < 0) ? firstNonEmptyStep : timeStep;
181  int timeEnd = (timeStep < 0) ? -timeStep : timeStep + 1;
182  for(int ent = 0; ent < data1->getNumEntities(timeBeg); ent++) {
183  bool ok = (physicalRegion <= 0);
184  if(physicalRegion > 0) {
185  GEntity *ge = data1->getEntity(timeBeg, ent);
186  if(ge) {
187  auto it =
188  std::find(ge->physicals.begin(), ge->physicals.end(), physicalRegion);
189  ok = (it != ge->physicals.end());
190  }
191  }
192  if(!ok) continue;
193  for(int ele = 0; ele < data1->getNumElements(timeBeg, ent); ele++) {
194  if(data1->skipElement(timeBeg, ent, ele)) continue;
195  int numNodes = data1->getNumNodes(timeBeg, ent, ele);
196  int type = data1->getType(timeBeg, ent, ele);
197  int numComp = data1->getNumComponents(timeBeg, ent, ele);
198  int otherNumComp = (!otherData || octree) ?
199  9 :
200  otherData->getNumComponents(timeBeg, ent, ele);
201  std::vector<double> *out = data2->incrementList(numComp2, type, numNodes);
202  std::vector<double> v(std::max(9, numComp), 0.);
203  std::vector<double> w(std::max(9, otherNumComp), 0.);
204  std::vector<double> x(numNodes), y(numNodes), z(numNodes);
205  for(int nod = 0; nod < numNodes; nod++)
206  data1->getNode(timeBeg, ent, ele, nod, x[nod], y[nod], z[nod]);
207  for(int nod = 0; nod < numNodes; nod++) out->push_back(x[nod]);
208  for(int nod = 0; nod < numNodes; nod++) out->push_back(y[nod]);
209  for(int nod = 0; nod < numNodes; nod++) out->push_back(z[nod]);
210  for(int step = timeBeg; step < timeEnd; step++) {
211  if(!data1->hasTimeStep(step)) continue;
212  int step2 = (otherTimeStep < 0) ? step : otherTimeStep;
213  for(int nod = 0; nod < numNodes; nod++) {
214  for(int comp = 0; comp < numComp; comp++)
215  data1->getValue(step, ent, ele, nod, comp, v[comp]);
216  if(otherData) {
217  if(octree) {
218  int qn = forceInterpolation ? numNodes : 0;
219  if(!octree->searchScalar(x[nod], y[nod], z[nod], &w[0], step2,
220  nullptr, qn, &x[0], &y[0], &z[0]))
221  if(!octree->searchVector(x[nod], y[nod], z[nod], &w[0], step2,
222  nullptr, qn, &x[0], &y[0], &z[0]))
223  octree->searchTensor(x[nod], y[nod], z[nod], &w[0], step2,
224  nullptr, qn, &x[0], &y[0], &z[0]);
225  }
226  else
227  for(int comp = 0; comp < otherNumComp; comp++)
228  otherData->getValue(step2, ent, ele, nod, comp, w[comp]);
229  }
230  values[0] = x[nod];
231  values[1] = y[nod];
232  values[2] = z[nod];
233  for(int i = 0; i < 9; i++) values[3 + i] = v[i];
234  for(int i = 0; i < 9; i++) values[12 + i] = w[i];
235  if(f.eval(values, res)) {
236  for(int i = 0; i < numComp2; i++) out->push_back(res[i]);
237  }
238  else {
239  goto end;
240  }
241  }
242  }
243  }
244  }
245 
246 end:
247  if(octree) delete octree;
248 
249  if(timeStep < 0) {
250  for(int i = firstNonEmptyStep; i < data1->getNumTimeSteps(); i++) {
251  if(!data1->hasTimeStep(i)) continue;
252  data2->Time.push_back(data1->getTime(i));
253  }
254  }
255  else
256  data2->Time.push_back(data1->getTime(timeStep));
257 
258  data2->setName(data1->getName() + "_MathEval");
259  data2->setFileName(data1->getName() + "_MathEval.pos");
260  data2->finalize();
261 
262  return v2;
263 }
StringXString
Definition: Options.h:910
GMSH_MathEvalPlugin::getHelp
std::string getHelp() const
Definition: MathEval.cpp:37
PView
Definition: PView.h:27
mathEvaluator
Definition: mathEvaluator.h:37
OctreePost::searchScalar
bool searchScalar(double x, double y, double z, double *values, int step=-1, double *size=nullptr, int qn=0, double *qx=nullptr, double *qy=nullptr, double *qz=nullptr, bool grad=false, int dim=-1)
Definition: OctreePost.cpp:578
PViewData::skipElement
virtual bool skipElement(int step, int ent, int ele, bool checkVisibility=false, int samplingRate=1)
Definition: PViewData.cpp:90
mathEvaluator.h
GMSH_Plugin
Definition: Plugin.h:26
Msg::Info
static void Info(const char *fmt,...)
Definition: GmshMessage.cpp:587
PViewData::getNumTimeSteps
virtual int getNumTimeSteps()=0
GEntity.h
MathEval.h
OctreePost.h
PViewDataList
Definition: PViewDataList.h:17
PViewData::getNode
virtual int getNode(int step, int ent, int ele, int nod, double &x, double &y, double &z)
Definition: PViewData.h:141
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
PViewDataList::incrementList
std::vector< double > * incrementList(int numComp, int type, int numNodes=0)
Definition: PViewDataList.cpp:1243
GEntity::physicals
std::vector< int > physicals
Definition: GEntity.h:65
LegendrePolynomials::f
void f(int n, double u, double *val)
Definition: orthogonalBasis.cpp:77
StringXNumber
Definition: Options.h:918
PViewData::getValue
virtual void getValue(int step, int ent, int ele, int idx, double &val)
Definition: PViewData.h:159
OctreePost
Definition: BoundaryLayers.cpp:23
PView::getIndex
int getIndex()
Definition: PView.h:92
GMSH_RegisterMathEvalPlugin
GMSH_Plugin * GMSH_RegisterMathEvalPlugin()
Definition: MathEval.cpp:34
PViewData::hasMultipleMeshes
virtual bool hasMultipleMeshes()
Definition: PViewData.h:216
PViewData::getNumEntities
virtual int getNumEntities(int step=-1)
Definition: PViewData.h:127
GEntity
Definition: GEntity.h:31
PViewData::setFileName
virtual void setFileName(const std::string &val)
Definition: PViewData.h:75
GMSH_MathEvalPlugin::getNbOptionsStr
int getNbOptionsStr() const
Definition: MathEval.cpp:78
PViewData::getTime
virtual double getTime(int step)
Definition: PViewData.h:94
PViewData::getType
virtual int getType(int step, int ent, int ele)
Definition: PViewData.h:183
GMSH_MathEvalPlugin::getOption
StringXNumber * getOption(int iopt)
Definition: MathEval.cpp:73
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
GMSH_MathEvalPlugin::execute
PView * execute(PView *)
Definition: MathEval.cpp:88
PViewData::hasTimeStep
virtual bool hasTimeStep(int step)
Definition: PViewData.h:211
PViewData::getNumNodes
virtual int getNumNodes(int step, int ent, int ele)
Definition: PViewData.h:137
GmshDefines.h
GMSH_MathEvalPlugin::getOptionStr
StringXString * getOptionStr(int iopt)
Definition: MathEval.cpp:83
PViewDataList::finalize
bool finalize(bool computeMinMax=true, const std::string &interpolationScheme="")
Definition: PViewDataList.cpp:81
PViewData::setName
virtual void setName(const std::string &val)
Definition: PViewData.h:71
GMSH_MathEvalPlugin::getNbOptions
int getNbOptions() const
Definition: MathEval.cpp:68
GMSH_MathEvalPlugin
Definition: MathEval.h:15
PViewData
Definition: PViewData.h:29
MathEvalOptions_String
StringXString MathEvalOptions_String[]
Definition: MathEval.cpp:22
PViewData::getNumComponents
virtual int getNumComponents(int step, int ent, int ele)
Definition: PViewData.h:152
OctreePost::searchVector
bool searchVector(double x, double y, double z, double *values, int step=-1, double *size=nullptr, int qn=0, double *qx=nullptr, double *qy=nullptr, double *qz=nullptr, bool grad=false, int dim=-1)
Definition: OctreePost.cpp:634
z
const double z
Definition: GaussQuadratureQuad.cpp:56
GMSH_PostPlugin::getView
virtual PView * getView(int index, PView *view)
Definition: Plugin.cpp:81
GMSH_PostPlugin::getPossiblyAdaptiveData
virtual PViewData * getPossiblyAdaptiveData(PView *view)
Definition: Plugin.cpp:94
PViewDataList::Time
std::vector< double > Time
Definition: PViewDataList.h:25
PViewData::getNumElements
virtual int getNumElements(int step=-1, int ent=-1)
Definition: PViewData.h:131
PViewData::getName
virtual std::string getName()
Definition: PViewData.h:70
MathEvalOptions_Number
StringXNumber MathEvalOptions_Number[]
Definition: MathEval.cpp:14
GMSH_PostPlugin::getDataList
virtual PViewDataList * getDataList(PView *view, bool showError=true)
Definition: Plugin.cpp:107
OctreePost::searchTensor
bool searchTensor(double x, double y, double z, double *values, int step=-1, double *size=nullptr, int qn=0, double *qx=nullptr, double *qy=nullptr, double *qz=nullptr, bool grad=false, int dim=-1)
Definition: OctreePost.cpp:690
PViewData::getEntity
virtual GEntity * getEntity(int step, int entity)
Definition: PViewData.cpp:142
PViewData::getFirstNonEmptyTimeStep
virtual int getFirstNonEmptyTimeStep(int start=0)
Definition: PViewData.h:91