diff --git a/src/OpenFOAM/fields/Fields/Field/Field.C b/src/OpenFOAM/fields/Fields/Field/Field.C index fe237c8283..fad7a81541 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.C +++ b/src/OpenFOAM/fields/Fields/Field/Field.C @@ -179,20 +179,32 @@ Foam::Field::Field template Foam::Field::Field(const entry& e, const label len) { - assign(e, len); + Field::assign(e, len); } template Foam::Field::Field ( - const word& keyword, + const word& key, const dictionary& dict, const label len, - enum keyType::option matchOpt + IOobjectOption::readOption readOpt ) { - assign(keyword, dict, len, matchOpt); + if (!Field::assign(key, dict, len, readOpt)) + { + if (IOobjectOption::isReadOptional(readOpt)) + { + // Lazy read: init with zero value + if (len > 0) this->resize(len, Zero); + } + else + { + // No read: set length only + if (len > 0) this->resize(len); + } + } } @@ -213,7 +225,7 @@ void Foam::Field::assign(const entry& e, const label len) // Resize to expected length (or -1 : retain current length) if (len >= 0) { - this->resize(len); + this->resize_nocopy(len); } operator=(pTraits(is)); } @@ -239,7 +251,7 @@ void Foam::Field::assign(const entry& e, const label len) else { FatalIOErrorInFunction(is) - << "size " << lenRead + << "Size " << lenRead << " is not equal to the expected length " << len << exit(FatalIOError); } @@ -257,18 +269,39 @@ void Foam::Field::assign(const entry& e, const label len) template -void Foam::Field::assign +bool Foam::Field::assign ( - const word& keyword, + const word& key, const dictionary& dict, const label len, - enum keyType::option matchOpt + IOobjectOption::readOption readOpt ) { - if (len) + if (!len) { - assign(dict.lookupEntry(keyword, matchOpt), len); + return true; } + else if (readOpt != IOobjectOption::NO_READ) + { + const entry* eptr = dict.findEntry(key, keyType::LITERAL); + + if (eptr) + { + Field::assign(*eptr, len); + return true; + } + + // Missing (mandatory or optional) + if (IOobjectOption::isReadRequired(readOpt)) + { + FatalIOErrorInFunction(dict) + << "Required entry '" << key << "' missing in dictionary " + << dict.relativeName() << nl + << exit(FatalIOError); + } + } + + return false; } diff --git a/src/OpenFOAM/fields/Fields/Field/Field.H b/src/OpenFOAM/fields/Fields/Field/Field.H index aa91c3de16..f398928d1a 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.H +++ b/src/OpenFOAM/fields/Fields/Field/Field.H @@ -52,6 +52,7 @@ SourceFiles #include "keyType.H" #include "scalarList.H" #include "VectorSpace.H" +#include "IOobjectOption.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -69,7 +70,6 @@ template class SubField; template Ostream& operator<<(Ostream&, const Field&); template Ostream& operator<<(Ostream&, const tmp>&); - /*---------------------------------------------------------------------------*\ Class FieldBase Declaration \*---------------------------------------------------------------------------*/ @@ -264,17 +264,22 @@ public: //- Construct from Istream inline Field(Istream& is); - //- Construct from a dictionary (primitive) entry + //- Construct from a dictionary (primitive) entry. Field(const entry& e, const label len); - //- Construct from lookup of a dictionary (primitive) entry. - // Uses REGEX lookup for legacy compatibility. + //- Lookup of a primitive dictionary entry by (literal) name + //- and assign its contents to this. The behaviour largely as + //- described in assign(): + // - For MUST_READ and key not found: FatalIOError. + // - For LAZY_READ and key not found: initialise field with Zero. + // - For NO_READ and key not found: simply size the field. + // . Field ( - const word& keyword, - const dictionary& dict, - const label len, - enum keyType::option matchOpt = keyType::REGEX + const word& key, //!< Lookup key. Uses LITERAL (not REGEX) + const dictionary& dict, //!< dictionary + const label len, //!< length + IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ ); //- Clone @@ -297,17 +302,31 @@ public: // Member Functions - //- Assign from a dictionary (primitive) entry + //- Assign from a primitive dictionary entry with the following + //- behaviour: + // - If len == 0 : a no-op + // - If len < 0 : no size checking + // - If len > 0 : resize \em uniform entries to this size + // before assigning. Use as length check for \em nonuniform + // entries. A mismatch is possibly Fatal, except when + // allowConstructFromLargerSize is true (eg, for column mesh). + // . void assign(const entry& e, const label len); - //- Assign from lookup of a dictionary (primitive) entry - // Uses REGEX lookup for legacy compatibility. - void assign + //- Lookup a primitive dictionary entry by (literal) name + //- and assign its contents to this (behaviour as described above). + // - If len == 0 : a no-op and return True. + // - For NO_READ : a no-op and return False. + // - For LAZY_READ and key not found : a no-op and return False. + // - For MUST_READ and key not found : FatalIOError + // . + // \returns True if an assignment occurred or for zero-length. + bool assign ( - const word& keyword, - const dictionary& dict, - const label len, - enum keyType::option matchOpt = keyType::REGEX + const word& key, //!< Lookup key. Uses LITERAL (not REGEX) + const dictionary& dict, //!< dictionary + const label len, //!< expected length + IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ ); //- 1 to 1 map from the given field diff --git a/src/OpenFOAM/fields/Fields/Field/FieldBase.H b/src/OpenFOAM/fields/Fields/Field/FieldBase.H deleted file mode 100644 index 7cec1b4541..0000000000 --- a/src/OpenFOAM/fields/Fields/Field/FieldBase.H +++ /dev/null @@ -1 +0,0 @@ -#warning File removed - left for old dependency check only