40 const bool wantPointData,
41 const label expectedSize
45 <<
"Looking for global" << (wantPointData ?
" point" :
"")
46 <<
" field name:" <<
name;
59 if (good && expectedSize >= 0)
81 const label expectedSize,
87 if (hasVariable(
name) && variable(
name).isType<Type>())
91 else if (isGlobalVariable<Type>(
name))
99 const auto& var = tvar.
cref();
113 if (!var.isUniform())
116 <<
"Variable " <<
name
117 <<
" is nonuniform and does not fit the size "
118 << expectedSize <<
". Using average" <<
endl;
127 <<
"Variable (" <<
name <<
") not found." <<
nl
143 Info<<
"fvExprDriver::foundField. Name: " <<
name
144 <<
" Type: " << Type::typeName
145 <<
" registry:" << searchRegistry()
146 <<
" disk:" << searchFiles() <<
endl;
150 for (
int checki = 0; checki < 2; ++checki)
160 else if (searchRegistry())
162 ioptr = this->
mesh().cfindIOobject(name);
164 if (!ioptr)
continue;
166 const Type* fldPtr =
dynamic_cast<const Type*
>(ioptr);
174 Info<<
"Found registered:";
178 Info<<
"Found context object:";
190 Info<<
"Registered:";
194 Info<<
"Context object:";
198 << Type::typeName <<
nl;
204 if (searchFiles() && getTypeOfField(
name) == Type::typeName)
231 Info<<
"fvExprDriver::isField <" << name << '>
' << endl;
237 ? this->foundField<PointField<Type>>(name)
240 this->foundField<VolumeField<Type>>(name)
241 || this->foundField<SurfaceField<Type>>(name)
247template<class GeomField, class Mesh>
248Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
256 typedef typename GeomField::value_type Type;
258 tmp<GeomField> tfield;
262 Info<< "fvExprDriver::getOrReadField <" << name
263 << "> Type: " << GeomField::typeName << endl;
270 refPtr<expressions::exprResult> tvar;
272 if (hasVariable(name) && variable(name).isType<Type>())
274 tvar.cref(variable(name));
276 else if (isGlobalVariable<Type>(name))
278 tvar.cref(lookupGlobal(name));
283 const auto& var = tvar.cref();
284 const Type deflt(var.getValue<Type>());
288 Info<< "Getting " << name << " from variables. Default: "
294 Info<< "Creating field " << name << " of type "
295 << GeomField::typeName << nl;
298 tfield = GeomField::New
302 dimensioned<Type>(deflt),
303 // Patch is zeroGradient (volFields) or calculated (other)
304 defaultBoundaryType(GeomField::null())
306 auto& fld = tfield.ref();
310 Info<< "New field: " << name << " ownedByRegistry"
311 << fld.ownedByRegistry() << endl;
314 const Field<Type>& vals = var.cref<Type>();
318 Pout<< "sizes: " << vals.size() << ' ' << fld.size() << endl;
321 if (returnReduce((vals.size() == fld.size()), andOp<bool>()))
323 fld.primitiveFieldRef() = vals;
327 const Type avg = gAverage(vals);
333 MinMax<Type> range = gMinMax(vals);
335 if (range.mag() > SMALL)
338 << "The min/max ranges differ " << range
339 << " - using average " << avg << nl;
343 fld.primitiveFieldRef() = avg;
352 // Find context or registered field
353 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
355 const GeomField* origFldPtr = nullptr;
357 for (int checki = 0; !origFldPtr && checki < 2; ++checki)
359 // Check 0: object context (first)
360 // Check 1: regular objectRegistry
364 origFldPtr = exprDriver::cfindContextObject<GeomField>(name);
366 else if (searchRegistry())
369 meshRef.thisDb().template cfindObject<GeomField>(name);
375 // Found from context or registry
379 Info<< "Retrieve context/registered:" << name << nl;
382 const GeomField& origFld = *origFldPtr;
384 // Make a deep copy of the data to return. Avoids shadowing
385 // the original object, but most importantly the backend
386 // parser (eg, lemon) will be working with combining via plain
387 // pointers. We thus lose any of the tmp<> shallow copy semantics
388 // anyhow. Additionally, need to disable dimension checking here or
391 tfield = GeomField::New(name + "_exprDriverCopy", origFld);
397 Info<< "Getting oldTime of " << name << " has "
398 << origFld.nOldTimes() << endl;
401 if (!origFld.nOldTimes() && this->prevIterIsOldTime())
405 Info<< "No oldTime, using previous iteration" << endl;
407 tfield.ref().oldTime() = origFld.prevIter();
411 else if (searchFiles() && getTypeOfField(name) == GeomField::typeName)
415 Info<< "Reading " << name << " from disc" << endl;
420 this->readAndRegister<GeomField>(name, meshRef)
422 // oldTime automatically read
428 Info<< "field: valid()=" << Switch::name(tfield.valid()) << endl;
433 GeomField& fld = tfield.ref();
437 Info<< "Valid " << name << " found. Removing dimensions" << nl;
440 fld.dimensions().clear();
446 Info<< "Removing dimensions of oldTime of " << name
447 << " has " << fld.nOldTimes() << nl;
450 // Switch dimension checking off
451 const bool oldDimChecking = dimensionSet::checking(false);
453 // go through ALL old times
454 GeomField* fp = &(fld);
456 while (fp->nOldTimes())
458 fp = &(fp->oldTime());
459 fp->dimensions().clear();
462 // Restore old value of dimension checking
463 dimensionSet::checking(oldDimChecking);
469 << "Could not find field " << name
470 << " in registry or on file-system" << nl
479Foam::autoPtr<T> Foam::expressions::fvExprDriver::getTopoSet
486 // Avoid possible name clashes
487 const word regName = name + "RegisteredNameFor" + T::typeName;
491 Info<< "Looking for " << T::typeName << " named " << name
492 << " or registered as " << regName << " with mesh "
493 << "Caching:" << cacheSets()
494 << " Found:" << (mesh.foundObject<T>(name))
495 << " Found registered:" << mesh.foundObject<T>(regName)
500 origin = SetOrigin::INVALID;
508 !mesh.thisDb().foundObject<T>(regName)
509 && !mesh.thisDb().foundObject<T>(name)
515 Info<< "Constructing new " << T::typeName << ' ' << name << nl;
519 Pout<< mesh.thisDb().names();
523 origin = SetOrigin::FILE;
524 setPtr.reset(new T(mesh, name, IOobject::MUST_READ));
530 Info<< "Registering a copy of " << name << " with mesh" << nl;
533 autoPtr<T> toCache(new T(mesh, regName, *setPtr));
534 toCache->store(toCache);
539 const T* ptr = mesh.thisDb().template cfindObject<T>(name);
545 Info<< "Getting existing " << name << endl;
548 origin = SetOrigin::MEMORY;
549 setPtr.reset(new T(mesh, name, *ptr));
555 Info<< "Getting existing " << regName << endl;
558 origin = SetOrigin::CACHE;
559 setPtr.reset(new T(mesh, name, mesh.lookupObject<T>(regName)));
569bool Foam::expressions::fvExprDriver::updateSet
576 const label oldSize = setPtr->size();
578 bool updated = false;
579 const auto& mesh = dynamicCast<const polyMesh>(setPtr->db());
583 Info<< "UpdateSet: " << setPtr->name() << " Id: " << name
584 << " Origin: " << int(origin) << endl;
589 case SetOrigin::FILE:
594 mesh.time().timeName(),
595 polyMesh::meshSubDir/"sets",
601 if (header.typeHeaderOk<T>())
605 Pout<< "Rereading from "
606 << header.localFilePath(T::typeName) << endl;
608 setPtr.reset(new T(header));
615 case SetOrigin::MEMORY:
616 case SetOrigin::CACHE:
618 if (origin == SetOrigin::NEW)
621 << "State NEW shouldn't exist
"
627 const T* ptr = mesh.thisDb().template cfindObject<T>(name);
633 Info<< "Found
" << name
634 << " and rereading it
" << endl;
637 setPtr.reset(new T(mesh, name, *ptr));
642 << name << " Not
found" << endl
643 << "In registry:
" << mesh.thisDb().names() << endl
653 << T::typeName << ' ' << name << " is invalid
" << endl
662 Info<< "Origin
" << int(origin) << " not implemented
" << endl;
670 Pout<< name << " old size
" << oldSize << " new:
"
671 << setPtr->size() << endl;
678// ************************************************************************* //
Generic templated field type.
const word & headerClassName() const noexcept
Return name of the class name read from header.
void size(const label n)
Older name for setAddressableSize.
const exprResult & result() const noexcept
Const access to expression result.
const regIOobject * cfindContextIOobject(const word &name) const
Find named context field, if it exists.
A polymorphic field/result from evaluating an expression.
const word & valueType() const noexcept
Basic type for the field or single value.
label size() const
The field or object size.
bool isType() const
True if valueType corresponds to the given Type.
bool isPointData(const bool wantPointData=true) const
bool foundField(const word &name) const
Test if specified field can be found in memory or disk.
tmp< Field< Type > > getVariable(const word &name, const label expectSize, const bool mandatory=true) const
Retrieve local/global variable as a tmp field.
const exprResult & lookupGlobal(const word &name) const
Return the global variable if available or a null result.
bool isField(const word &name, const bool wantPointData=false, const label expectSize=-1) const
Test for the existence of a mesh field.
bool isGlobalVariable(const word &name, const bool wantPointData=false, const label expectedSize=-1) const
Test existence of a global variable.
A class for managing references or pointers (no reference counting)
bool valid() const noexcept
Identical to good(), or bool operator.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
A class for managing temporary objects.
A class for handling words, derived from Foam::string.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
#define DebugInfo
Report an information message using Foam::Info.
#define WarningInFunction
Report a warning using Foam::Warning.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Type gAverage(const FieldField< Field, Type > &f)
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh > > &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce (copy) and return value.
errorManipArg< error, int > exit(error &err, const int errNo=1)
constexpr char nl
The newline '\n' character (0x0a)