FreeFOAM The Cross-Platform CFD Toolkit
cellSet.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  cellSet
26 
27 Description
28  Selects a cell set through a dictionary.
29 
30 Usage
31 
32  - cellSet [OPTIONS]
33 
34  @param -case <dir>\n
35  Case directory.
36 
37  @param -parallel \n
38  Run in parallel.
39 
40  @param -help \n
41  Display help message.
42 
43  @param -doc \n
44  Display Doxygen API documentation page for this application.
45 
46  @param -srcDoc \n
47  Display Doxygen source documentation page for this application.
48 
49 \*---------------------------------------------------------------------------*/
50 
51 #include <OpenFOAM/argList.H>
52 #include <OpenFOAM/Time.H>
53 #include <OpenFOAM/polyMesh.H>
55 #include <meshTools/cellSet.H>
56 
57 using namespace Foam;
58 
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 
61 // Copy set
62 void backup
63 (
64  const polyMesh& mesh,
65  const word& fromName,
66  const topoSet& fromSet,
67  const word& toName
68 )
69 {
70  Info<< "Backing up " << fromName << " into " << toName << endl;
71 
72  topoSet backupSet(mesh, toName, fromSet);
73 
74  backupSet.write();
75 }
76 
77 
78 // Read and copy set
79 void backup
80 (
81  const polyMesh& mesh,
82  const word& fromName,
83  const word& toName
84 )
85 {
86  topoSet fromSet(mesh, fromName, IOobject::READ_IF_PRESENT);
87 
88  backup(mesh, fromName, fromSet, toName);
89 }
90 
91 
92 // Main program:
93 
94 int main(int argc, char *argv[])
95 {
96 # include <OpenFOAM/setRootCase.H>
97 # include <OpenFOAM/createTime.H>
98 # include <OpenFOAM/createPolyMesh.H>
99 
100  Info<< "Reading cellSetDict\n" << endl;
101 
102  IOdictionary cellSetDict
103  (
104  IOobject
105  (
106  "cellSetDict",
107  runTime.system(),
108  mesh,
111  )
112  );
113 
114 
115  word setName(cellSetDict.lookup("name"));
116 
117  word actionName(cellSetDict.lookup("action"));
118 
120 
121 
122  // Create topoSetSources
123  PtrList<topoSetSource> topoSetSources
124  (
125  cellSetDict.lookup("topoSetSources"),
126  topoSetSource::iNew(mesh)
127  );
128 
129 
130  // Load set to work
131  autoPtr<topoSet> currentSetPtr(NULL);
133 
134  if ((action == topoSetSource::NEW) || (action == topoSetSource::CLEAR))
135  {
136  r = IOobject::NO_READ;
137 
138  backup(mesh, setName, setName + "_old");
139 
140  currentSetPtr.reset
141  (
142  new cellSet
143  (
144  mesh,
145  setName,
146  mesh.nCells()/10+1 // Reasonable size estimate.
147  )
148  );
149  }
150  else
151  {
153 
154  currentSetPtr.reset
155  (
156  new cellSet
157  (
158  mesh,
159  setName,
160  r
161  )
162  );
163  }
164 
165  topoSet& currentSet = currentSetPtr();
166 
167  Info<< "Set:" << currentSet.name()
168  << " Size:" << currentSet.size()
169  << " Action:" << actionName
170  << endl;
171 
172  if ((r == IOobject::MUST_READ) && (action != topoSetSource::LIST))
173  {
174  // currentSet has been read so can make copy.
175  backup(mesh, setName, currentSet, setName + "_old");
176  }
177 
178  if (action == topoSetSource::CLEAR)
179  {
180  // Already handled above by not reading
181  }
182  else if (action == topoSetSource::INVERT)
183  {
184  currentSet.invert(currentSet.maxSize(mesh));
185  }
186  else if (action == topoSetSource::LIST)
187  {
188  currentSet.writeDebug(Info, mesh, 100);
189  Info<< endl;
190  }
191  else if (action == topoSetSource::SUBSET)
192  {
193  // Apply topoSetSources to it to handle new/add/delete
194  forAll(topoSetSources, topoSetSourceI)
195  {
196  // Backup current set.
197  topoSet oldSet(mesh, currentSet.name() + "_old2", currentSet);
198 
199  currentSet.clear();
200 
201  topoSetSources[topoSetSourceI].applyToSet
202  (
204  currentSet
205  );
206 
207  // Combine new value of currentSet with old one.
208  currentSet.subset(oldSet);
209  }
210  }
211  else
212  {
213  // Apply topoSetSources to it to handle new/add/delete
214  forAll(topoSetSources, topoSetSourceI)
215  {
216  topoSetSources[topoSetSourceI].applyToSet(action, currentSet);
217  }
218  }
219 
220 
221  if (action != topoSetSource::LIST)
222  {
223  // Set has changed.
224 
225  // Sync across coupled patches.
226  currentSet.sync(mesh);
227 
228  Info<< "Writing " << currentSet.name()
229  << " (size " << currentSet.size() << ") to "
230  << currentSet.instance()/currentSet.local()
231  /currentSet.name()
232  << endl << endl;
233 
234  currentSet.write();
235  }
236 
237  Info << nl << "End" << endl;
238 
239  return 0;
240 }
241 
242 
243 // ************************ vim: set sw=4 sts=4 et: ************************ //