NEMoSys  0.63.0
A modular, extensible resource with robust automated mesh generation, mesh quality analysis, adaptive mesh refinement, and data transfer between arbitrary meshes.
ValSizeField.C
Go to the documentation of this file.
1 /*******************************************************************************
2 * Promesh *
3 * Copyright (C) 2022, IllinoisRocstar LLC. All rights reserved. *
4 * *
5 * Promesh is the property of IllinoisRocstar LLC. *
6 * *
7 * IllinoisRocstar LLC *
8 * Champaign, IL *
9 * www.illinoisrocstar.com *
10 * promesh@illinoisrocstar.com *
11 *******************************************************************************/
12 /*******************************************************************************
13 * This file is part of Promesh *
14 * *
15 * This version of Promesh is free software: you can redistribute it and/or *
16 * modify it under the terms of the GNU Lesser General Public License as *
17 * published by the Free Software Foundation, either version 3 of the License, *
18 * or (at your option) any later version. *
19 * *
20 * Promesh is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more *
23 * details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with this program. If not, see <https://www.gnu.org/licenses/>. *
27 * *
28 *******************************************************************************/
30 
31 #include <vtkCellData.h>
32 #include <vtkCellIterator.h>
33 #include <vtkDoubleArray.h>
34 #include <vtkIdList.h>
35 
36 #include "AuxiliaryFunctions.H"
37 
38 namespace NEM {
39 namespace ADP {
40 
41 // constructor
42 ValSizeField::ValSizeField(vtkDataSet *_ds, int arrayID, double _dev_mult,
43  bool _maxIsmin)
44  : SizeFieldBase(_ds, arrayID, _dev_mult, _maxIsmin, "ValueSF") {
45  std::cout << "ValSizeField constructed" << std::endl;
46 }
47 
48 // computes value of point data at a cell center using average of data
49 // at points defining cell
50 // NOTE: averaging is equivalent to using interpolation weights for cell center
51 std::vector<double> ValSizeField::computeValAtCell(vtkIdList *cell_point_ids,
52  vtkDataArray *da) {
53  int dim = da->GetNumberOfComponents();
54  std::vector<double> values(dim, 0.0);
55  int numPointsInCell = cell_point_ids->GetNumberOfIds();
56  // compute value of data at center of cell
57  for (int j = 0; j < numPointsInCell; ++j) {
58  std::vector<double> comps(dim);
59  da->GetTuple(cell_point_ids->GetId(j), comps.data());
60  for (int i = 0; i < dim; ++i) {
61  values[i] += comps[i] / numPointsInCell;
62  }
63  }
64 
65  return values;
66 }
67 
68 // compute value of point data at center of each cell
69 std::vector<std::vector<double>> ValSizeField::computeValAtAllCells(
70  vtkDataSet *ds, vtkDataArray *da) {
71  std::vector<std::vector<double>> result;
72  result.reserve(ds->GetNumberOfCells());
73 
74  vtkCellIterator *it = ds->NewCellIterator();
75  for (it->InitTraversal(); !it->IsDoneWithTraversal(); it->GoToNextCell()) {
76  result.emplace_back(computeValAtCell(it->GetPointIds(), da));
77  }
78  it->Delete();
79 
80  return result;
81 }
82 
83 // compute 2 norm of value of point data at center of each cell
84 std::vector<double> ValSizeField::computeL2ValAtAllCells(vtkDataSet *ds,
85  vtkDataArray *da) {
86  std::vector<double> result;
87  result.reserve(ds->GetNumberOfCells());
88 
89  vtkCellIterator *it = ds->NewCellIterator();
90  for (it->InitTraversal(); !it->IsDoneWithTraversal(); it->GoToNextCell()) {
91  result.emplace_back(
92  nemAux::l2_Norm(computeValAtCell(it->GetPointIds(), da)));
93  }
94  it->Delete();
95 
96  return result;
97 }
98 
99 // compute size field and insert as cell data into mesh's dataSet
100 void ValSizeField::computeSizeField(vtkDataArray *da) {
101  // populate vector with 2 norm of gradient/value of physical variable
102  std::vector<double> values = computeL2ValAtAllCells(ds, da);
103 
104  if (values.empty()) {
105  std::cerr << "size array hasn't been populated!" << std::endl;
106  exit(1);
107  }
108 
109  mutateValues(values);
110 
111  vtkSmartPointer<vtkDoubleArray> da2 = vtkSmartPointer<vtkDoubleArray>::New();
112  da2->SetName(sfname.c_str());
113  da2->SetNumberOfComponents(1);
114  for (vtkIdType i = 0; i < ds->GetNumberOfCells(); ++i)
115  da2->InsertNextTypedTuple(&values[i]);
116  ds->GetCellData()->AddArray(da2);
117 }
118 
119 } // namespace ADP
120 } // namespace NEM
vtkSmartPointer< vtkDataSet > ds
Definition: SizeFieldGen.H:76
geoMeshBase * New(MeshType meshType)
Create a new mesh object.
vtkSmartPointer< vtkDataArray > da
Definition: SizeFieldGen.H:79
static std::vector< double > computeValAtCell(vtkIdList *cell_point_ids, vtkDataArray *da)
Definition: ValSizeField.C:51
ValSizeField(vtkDataSet *_ds, int arrayID, double _dev_mult, bool _maxIsmin)
Definition: ValSizeField.C:42
void mutateValues(std::vector< double > &values) const
Definition: SizeFieldGen.C:112
static std::vector< std::vector< double > > computeValAtAllCells(vtkDataSet *ds, vtkDataArray *da)
Definition: ValSizeField.C:69
void computeSizeField(vtkDataArray *da) override
Definition: ValSizeField.C:100
static std::vector< double > computeL2ValAtAllCells(vtkDataSet *ds, vtkDataArray *da)
Definition: ValSizeField.C:84
T l2_Norm(const std::vector< T > &x)