gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
ModifyComponents.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 <vector>
7 #include <algorithm>
8 #include "GmshConfig.h"
9 #include "ModifyComponents.h"
10 #include "OctreePost.h"
11 #include "mathEvaluator.h"
12 
14  {GMSH_FULLRC, "TimeStep", nullptr, -1.},
15  {GMSH_FULLRC, "View", nullptr, -1.},
16  {GMSH_FULLRC, "OtherTimeStep", nullptr, -1.},
17  {GMSH_FULLRC, "OtherView", nullptr, -1.},
18  {GMSH_FULLRC, "ForceInterpolation", nullptr, 0.}};
19 
21  {GMSH_FULLRC, "Expression0", nullptr, "v0 * Sin(x)"},
22  {GMSH_FULLRC, "Expression1", nullptr, ""},
23  {GMSH_FULLRC, "Expression2", nullptr, ""},
24  {GMSH_FULLRC, "Expression3", nullptr, ""},
25  {GMSH_FULLRC, "Expression4", nullptr, ""},
26  {GMSH_FULLRC, "Expression5", nullptr, ""},
27  {GMSH_FULLRC, "Expression6", nullptr, ""},
28  {GMSH_FULLRC, "Expression7", nullptr, ""},
29  {GMSH_FULLRC, "Expression8", nullptr, ""}};
30 
31 extern "C" {
33 {
34  return new GMSH_ModifyComponentsPlugin();
35 }
36 }
37 
39 {
40  return "Plugin(ModifyComponents) modifies the components of "
41  "the `TimeStep'-th time step in the view `View', using the "
42  "expressions provided in `Expression0', ..., `Expression8'. "
43  "If an expression is empty, the corresponding component in "
44  "the view is not modified.\n\n"
45  "The expressions can contain:\n\n"
46  "- the usual mathematical functions (Log, Sqrt, "
47  "Sin, Cos, Fabs, ...) and operators (+, -, *, /, ^);\n\n"
48  "- the symbols x, y and z, to retrieve the "
49  "coordinates of the current node;\n\n"
50  "- the symbols Time and TimeStep, to retrieve the "
51  "current time and time step values;\n\n"
52  "- the symbols v0, v1, v2, ..., v8, to retrieve each "
53  "component of the field in `View' at the "
54  "`TimeStep'-th time step;\n\n"
55  "- the symbols w0, w1, w2, ..., w8, to retrieve each "
56  "component of the field in `OtherView' at the "
57  "`OtherTimeStep'-th time step. If `OtherView' "
58  "and `View' are based on different spatial grids, "
59  "or if their data types are different, `OtherView' "
60  "is interpolated onto `View'.\n\n"
61  "If `TimeStep' < 0, the plugin automatically loops "
62  "over all the time steps in `View' and evaluates "
63  "the expressions for each one.\n\n"
64  "If `OtherTimeStep' < 0, the plugin uses `TimeStep' "
65  "instead.\n\n"
66  "If `View' < 0, the plugin is run on the current view.\n\n"
67  "If `OtherView' < 0, the plugin uses `View' instead.\n\n"
68  "Plugin(ModifyComponents) is executed in-place.";
69 }
70 
72 {
73  return sizeof(ModifyComponentsOptions_Number) / sizeof(StringXNumber);
74 }
75 
77 {
78  return &ModifyComponentsOptions_Number[iopt];
79 }
80 
82 {
83  return sizeof(ModifyComponentsOptions_String) / sizeof(StringXString);
84 }
85 
87 {
88  return &ModifyComponentsOptions_String[iopt];
89 }
90 
92 {
93  int timeStep = (int)ModifyComponentsOptions_Number[0].def;
94  int iView = (int)ModifyComponentsOptions_Number[1].def;
95  int otherTimeStep = (int)ModifyComponentsOptions_Number[2].def;
96  int otherView = (int)ModifyComponentsOptions_Number[3].def;
97  int forceInterpolation = (int)ModifyComponentsOptions_Number[4].def;
98 
99  PView *v1 = getView(iView, view);
100  if(!v1) return view;
101 
102  PViewData *data1 = v1->getData();
103 
104  if(timeStep > data1->getNumTimeSteps() - 1) {
105  Msg::Error("Invalid time step (%d) in View[%d]: using step 0 instead",
106  timeStep, v1->getIndex());
107  timeStep = 0;
108  }
109 
110  PView *v2 = v1;
111 
112  if(otherView >= 0) {
113  if(otherView < (int)PView::list.size())
114  v2 = PView::list[otherView];
115  else
116  Msg::Error("View[%d] does not exist: using self", otherView);
117  }
118 
119  PViewData *data2 = getPossiblyAdaptiveData(v2);
120 
121  if(otherTimeStep < 0 &&
122  data2->getNumTimeSteps() != data1->getNumTimeSteps()) {
123  Msg::Error("Number of time steps don't match: using step 0");
124  otherTimeStep = 0;
125  }
126  else if(otherTimeStep > data2->getNumTimeSteps() - 1) {
127  Msg::Error("Invalid time step (%d) in View[%d]: using step 0 instead",
128  otherTimeStep, v2->getIndex());
129  otherTimeStep = 0;
130  }
131 
132  std::vector<std::string> expressions(9), expressions0(9);
133  for(int i = 0; i < 9; i++) {
134  expressions[i] = ModifyComponentsOptions_String[i].def;
135  if(expressions[i].size())
136  expressions0[i] = expressions[i];
137  else
138  expressions0[i] = "0.";
139  }
140 
141  const char *names[] = {"x", "y", "z", "Time", "TimeStep", "v0", "v1", "v2",
142  "v3", "v4", "v5", "v6", "v7", "v8", "w0", "w1",
143  "w2", "w3", "w4", "w5", "w6", "w7", "w8"};
144  std::size_t numVariables = sizeof(names) / sizeof(names[0]);
145  std::vector<std::string> variables(numVariables);
146  for(std::size_t i = 0; i < numVariables; i++) variables[i] = names[i];
147  mathEvaluator f(expressions0, variables);
148 
149  std::vector<double> values(numVariables), res(9);
150 
151  OctreePost *octree = nullptr;
152  if(forceInterpolation ||
153  (data1->getNumEntities() != data2->getNumEntities()) ||
154  (data1->getNumElements() != data2->getNumElements())) {
155  Msg::Info("Other view based on different grid: interpolating...");
156  octree = new OctreePost(v2);
157  }
158 
159  for(int step = 0; step < data1->getNumTimeSteps(); step++) {
160  if(timeStep >= 0 && timeStep != step) continue;
161 
162  double time = data1->getTime(step);
163  int step2 = (otherTimeStep < 0) ? step : otherTimeStep;
164 
165  if(data1->isNodeData()) {
166  // tag all the nodes with "0" (the default tag)
167  for(int ent = 0; ent < data1->getNumEntities(step); ent++) {
168  for(int ele = 0; ele < data1->getNumElements(step, ent); ele++) {
169  if(data1->skipElement(step, ent, ele)) continue;
170  for(int nod = 0; nod < data1->getNumNodes(step, ent, ele); nod++)
171  data1->tagNode(step, ent, ele, nod, 0);
172  }
173  }
174  }
175 
176  for(int ent = 0; ent < data1->getNumEntities(step); ent++) {
177  for(int ele = 0; ele < data1->getNumElements(step, ent); ele++) {
178  if(data1->skipElement(step, ent, ele)) continue;
179  int numComp = data1->getNumComponents(step, ent, ele);
180  int numComp2 = octree ? 9 : data2->getNumComponents(step2, ent, ele);
181  int numNodes = data1->getNumNodes(step, ent, ele);
182  std::vector<double> x(numNodes), y(numNodes), z(numNodes);
183  std::vector<int> tag(numNodes);
184  for(int nod = 0; nod < numNodes; nod++)
185  tag[nod] =
186  data1->getNode(step, ent, ele, nod, x[nod], y[nod], z[nod]);
187  for(int nod = 0; nod < numNodes; nod++) {
188  if(data1->isNodeData() && tag[nod])
189  continue; // node has already been modified
190  std::vector<double> v(std::max(9, numComp), 0.);
191  for(int comp = 0; comp < numComp; comp++)
192  data1->getValue(step, ent, ele, nod, comp, v[comp]);
193  std::vector<double> w(std::max(9, numComp2), 0.);
194  if(octree) {
195  int qn = forceInterpolation ? numNodes : 0;
196  if(!octree->searchScalar(x[nod], y[nod], z[nod], &w[0], step2,
197  nullptr, qn, &x[0], &y[0], &z[0]))
198  if(!octree->searchVector(x[nod], y[nod], z[nod], &w[0], step2,
199  nullptr, qn, &x[0], &y[0], &z[0]))
200  octree->searchTensor(x[nod], y[nod], z[nod], &w[0], step2,
201  nullptr, qn, &x[0], &y[0], &z[0]);
202  }
203  else {
204  for(int comp = 0; comp < numComp2; comp++)
205  data2->getValue(step2, ent, ele, nod, comp, w[comp]);
206  }
207  values[0] = x[nod];
208  values[1] = y[nod];
209  values[2] = z[nod];
210  values[3] = time;
211  values[4] = step;
212  for(int i = 0; i < 9; i++) values[5 + i] = v[i];
213  for(int i = 0; i < 9; i++) values[14 + i] = w[i];
214  if(f.eval(values, res)) {
215  for(int comp = 0; comp < numComp; comp++) {
216  if(expressions[comp].size()) {
217  data1->setValue(step, ent, ele, nod, comp, res[comp]);
218  }
219  }
220  }
221  if(data1->isNodeData()) data1->tagNode(step, ent, ele, nod, 1);
222  }
223  }
224  }
225  }
226 
227  if(octree) delete octree;
228 
229  data1->finalize();
230  v1->setChanged(true);
231 
232  return v1;
233 }
StringXString
Definition: Options.h:910
GMSH_ModifyComponentsPlugin::getNbOptionsStr
int getNbOptionsStr() const
Definition: ModifyComponents.cpp:81
ModifyComponentsOptions_Number
StringXNumber ModifyComponentsOptions_Number[]
Definition: ModifyComponents.cpp:13
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
GMSH_ModifyComponentsPlugin::getNbOptions
int getNbOptions() const
Definition: ModifyComponents.cpp:71
mathEvaluator.h
GMSH_Plugin
Definition: Plugin.h:26
GMSH_ModifyComponentsPlugin::execute
PView * execute(PView *)
Definition: ModifyComponents.cpp:91
Msg::Info
static void Info(const char *fmt,...)
Definition: GmshMessage.cpp:587
PViewData::getNumTimeSteps
virtual int getNumTimeSteps()=0
PViewData::setValue
virtual void setValue(int step, int ent, int ele, int nod, int comp, double val)
Definition: PViewData.cpp:130
OctreePost.h
PViewData::isNodeData
virtual bool isNodeData()
Definition: PViewData.h:218
GMSH_ModifyComponentsPlugin::getHelp
std::string getHelp() const
Definition: ModifyComponents.cpp:38
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
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_RegisterModifyComponentsPlugin
GMSH_Plugin * GMSH_RegisterModifyComponentsPlugin()
Definition: ModifyComponents.cpp:32
PViewData::getNumEntities
virtual int getNumEntities(int step=-1)
Definition: PViewData.h:127
PView::setChanged
void setChanged(bool val)
Definition: PView.cpp:241
PViewData::getTime
virtual double getTime(int step)
Definition: PViewData.h:94
PViewData::tagNode
virtual void tagNode(int step, int ent, int ele, int nod, int tag)
Definition: PViewData.h:148
ModifyComponents.h
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
PView::getData
PViewData * getData(bool useAdaptiveIfAvailable=false)
Definition: PView.cpp:233
PViewData::getNumNodes
virtual int getNumNodes(int step, int ent, int ele)
Definition: PViewData.h:137
GMSH_ModifyComponentsPlugin
Definition: ModifyComponents.h:15
StringXString::def
std::string def
Definition: Options.h:914
PViewData
Definition: PViewData.h:29
PViewData::getNumComponents
virtual int getNumComponents(int step, int ent, int ele)
Definition: PViewData.h:152
GMSH_ModifyComponentsPlugin::getOption
StringXNumber * getOption(int iopt)
Definition: ModifyComponents.cpp:76
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
GMSH_ModifyComponentsPlugin::getOptionStr
StringXString * getOptionStr(int iopt)
Definition: ModifyComponents.cpp:86
PViewData::getNumElements
virtual int getNumElements(int step=-1, int ent=-1)
Definition: PViewData.h:131
ModifyComponentsOptions_String
StringXString ModifyComponentsOptions_String[]
Definition: ModifyComponents.cpp:20
PView::list
static std::vector< PView * > list
Definition: PView.h:112
PViewData::finalize
virtual bool finalize(bool computeMinMax=true, const std::string &interpolationScheme="")
Definition: PViewData.cpp:30
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