File: Scal2Tens.cpp

package info (click to toggle)
gmsh 4.8.4%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 87,812 kB
  • sloc: cpp: 378,014; ansic: 99,669; yacc: 7,216; python: 6,680; java: 3,486; lisp: 659; lex: 621; perl: 571; makefile: 470; sh: 440; xml: 415; javascript: 113; pascal: 35; modula3: 32
file content (145 lines) | stat: -rw-r--r-- 4,677 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/gmsh/gmsh/issues.

#include "Scal2Tens.h"
#include "PViewOptions.h"
#include "shapeFunctions.h"

StringXNumber Scal2TensOptions_Number[] = {
  {GMSH_FULLRC, "NumberOfComponents", nullptr, 9},
  {GMSH_FULLRC, "View0", nullptr, -1},
  {GMSH_FULLRC, "View1", nullptr, -1},
  {GMSH_FULLRC, "View2", nullptr, -1},
  {GMSH_FULLRC, "View3", nullptr, -1},
  {GMSH_FULLRC, "View4", nullptr, -1},
  {GMSH_FULLRC, "View5", nullptr, -1},
  {GMSH_FULLRC, "View6", nullptr, -1},
  {GMSH_FULLRC, "View7", nullptr, -1},
  {GMSH_FULLRC, "View8", nullptr, -1}};

StringXString Scal2TensOptions_String[] = {
  {GMSH_FULLRC, "NameNewView", nullptr, "NewView"}};

extern "C" {
GMSH_Plugin *GMSH_RegisterScal2TensPlugin()
{
  return new GMSH_Scal2TensPlugin();
}
}

std::string GMSH_Scal2TensPlugin::getHelp() const
{
  return "Plugin(Scal2Tens) converts some scalar fields into a tensor field. "
         "The number of components must be given (max. 9). "
         "The new view 'NameNewView' contains the new tensor field. If the "
         "number "
         "of a view is -1, the value of the corresponding component is 0.";
}

int GMSH_Scal2TensPlugin::getNbOptions() const
{
  return sizeof(Scal2TensOptions_Number) / sizeof(StringXNumber);
}

StringXNumber *GMSH_Scal2TensPlugin::getOption(int iopt)
{
  return &Scal2TensOptions_Number[iopt];
}

int GMSH_Scal2TensPlugin::getNbOptionsStr() const
{
  return sizeof(Scal2TensOptions_String) / sizeof(StringXString);
}

StringXString *GMSH_Scal2TensPlugin::getOptionStr(int iopt)
{
  return &Scal2TensOptions_String[iopt];
}

PView *GMSH_Scal2TensPlugin::execute(PView *v)
{
  // Load options
  int numComp = (int)Scal2TensOptions_Number[0].def;
  if((numComp < 1) || (numComp > 9)) {
    Msg::Error(
      "Scal2Tens plugin: NumberOfComponents must be between 1 and 9 (not '%i')",
      numComp);
    return v;
  }
  int iView[9];
  for(int comp = 0; comp < numComp; comp++)
    iView[comp] = (int)Scal2TensOptions_Number[comp + 1].def;

  // Load data
  PView *vRef = nullptr, *vComp[9];
  for(int comp = 0; comp < numComp; comp++) {
    if(iView[comp] < 0)
      vComp[comp] = nullptr;
    else {
      vComp[comp] = getView(iView[comp], v);
      if(!vComp[comp]) {
        Msg::Error("Scal2Tens plugin could not find View '%i'", iView[comp]);
        return v;
      }
      if(!vRef) vRef = vComp[comp];
    }
  }
  if(!vRef) {
    Msg::Error("Scal2Tens plugin could not find any view.");
    return v;
  }
  PViewData *dataRef = vRef->getData();

  // Initialize the new view
  PView *vNew = new PView();
  PViewDataList *dataNew = getDataList(vNew);

  int step0 = dataRef->getFirstNonEmptyTimeStep();
  for(int ent = 0; ent < dataRef->getNumEntities(step0); ent++) {
    for(int ele = 0; ele < dataRef->getNumElements(step0, ent); ele++) {
      if(dataRef->skipElement(step0, ent, ele)) continue;
      int type = dataRef->getType(step0, ent, ele);
      int numNodes = dataRef->getNumNodes(step0, ent, ele);
      std::vector<double> *out = dataNew->incrementList(
        numComp, type, numNodes); // Pointer in data of the new view
      if(!out) continue;
      double x[8], y[8], z[8];
      for(int nod = 0; nod < numNodes; nod++)
        dataRef->getNode(step0, ent, ele, nod, x[nod], y[nod], z[nod]);
      int dim = dataRef->getDimension(step0, ent, ele);
      elementFactory factory;
      element *element = factory.create(numNodes, dim, x, y, z);
      if(!element) continue;
      for(int nod = 0; nod < numNodes; nod++)
        out->push_back(x[nod]); // Save coordinates (x,y,z)
      for(int nod = 0; nod < numNodes; nod++) out->push_back(y[nod]);
      for(int nod = 0; nod < numNodes; nod++) out->push_back(z[nod]);
      for(int step = step0; step < dataRef->getNumTimeSteps(); step++) {
        if(!dataRef->hasTimeStep(step)) continue;
        for(int nod = 0; nod < numNodes; nod++) {
          for(int comp = 0; comp < numComp; comp++) {
            double val = 0.;
            if(vComp[comp])
              vComp[comp]->getData()->getValue(step, ent, ele, nod, 0, val);
            out->push_back(val); // Save value
          }
        }
      }
      delete element;
    }
  }

  for(int step = step0; step < dataRef->getNumTimeSteps(); step++) {
    if(!dataRef->hasTimeStep(step)) continue;
    dataNew->Time.push_back(dataRef->getTime(step));
  }

  std::string nameNewView = Scal2TensOptions_String[0].def;
  dataNew->setName(nameNewView);
  dataNew->setFileName(nameNewView + ".pos");
  dataNew->finalize();

  return vNew;
}