Hi all,
I am working on updating an old solver. Namely, I am updating Pascal Beckstein's eddyCurrentFoam, which you can learn more about here. The TL;DR is that is solves for the eddy current created by an alternating magnetic field, however that is not very relevant. This code is quite old, and was written for foam-extend 4.1. I wanted to update it to 5.0, as, as far as I know, 4.1 must be complied with gcc 7, which is not readily available anymore, as well as other reasons. Before I dive into the technical element, let me preface that I am not that experienced with coding in C++, and even less knowledgeable about the foam-extend framework. So please forgive any mistakes, and tell me what you think.
While updating, I was confronted with the following error. In short, (I believe that) the list being referenced to in line 411 is a pointer to the list, rather than the list itself.
In file included from /home/ubuntu/foam/foam-extend-5.0/src/foam/lnInclude/List.H:43,
from /home/ubuntu/foam/foam-extend-5.0/src/foam/lnInclude/HashTable.C:30,
from /home/ubuntu/foam/foam-extend-5.0/src/foam/lnInclude/HashTable.H:557,
from eddyCurrentApp/eddyCurrentAppInclude.H:36,
from eddyCurrentApp/eddyCurrentApp.H:35,
from eddyCurrentFoam.C:35:
/home/ubuntu/foam/foam-extend-5.0/src/foam/lnInclude/FieldField.C: In instantiation of ‘void Foam::FieldField<Field, Type>::operator=(const Foam::tmp<Foam::FieldField<Field, Type> >&) [with Field = Foam::Field; Type = double]’:
include/AVLoop_AEqn.H:150:81: required from here
/home/ubuntu/foam/foam-extend-5.0/src/foam/lnInclude/UList.H:411:36: error: request for member ‘size’ in ‘(Foam::FieldField<Foam::Field, double>*)this’, which is of pointer type ‘Foam::FieldField<Foam::Field, double>*’ (maybe you meant to use ‘->’ ?)
411 | for (Foam::label i=0; i<(list).size(); i++)
| ~~~~~~~^~~~
/home/ubuntu/foam/foam-extend-5.0/src/foam/lnInclude/FieldField.C:321:5: note: in expansion of macro ‘forAll’
321 | forAll (this, i)
| ^~~~~~
I looked to the source, FieldField.C, (in 5.0) and I saw the (somewhat funny) section below, around line 300.
template<template<class> class Field, class Type>
void FieldField<Field, Type>::operator=(const tmp<FieldField>& tf)
{
if (this == &(tf()))
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
}
// This is dodgy stuff, don't try this at home.
// It hurt me. HJ, 2/May/2022 lol
// FieldField* fieldPtr = tf.ptr();
// PtrList<Field<Type> >::transfer(*fieldPtr);
// delete fieldPtr;
// Boundary field is transferred, avoiding assignment operators.
// This disables virtual functions, which breaks the code.
// Use normal assignment instead. HJ, 3/May/2022
const FieldField<Field, Type>& f = tf();
forAll (this, i)
{
this->operator[](i) = f[i];
}
tf.clear();
}
This replaced the following dodgy code from 4.1.
template<template<class> class Field, class Type>
void FieldField<Field, Type>::operator=(const tmp<FieldField>& tf)
{
if (this == &(tf()))
{
FatalErrorIn
(
"FieldField<Field, Type>::operator=(const tmp<FieldField>&)"
) << "attempted assignment to self"
<< abort(FatalError);
}
// This is dodgy stuff, don't try this at home.
FieldField* fieldPtr = tf.ptr();
PtrList<Field<Type> >::transfer(*fieldPtr);
delete fieldPtr;
}
As mentioned, when compiling 5.0, I get a pointer problem. However, if I change the end of the 5.0 section to the following, then the program complies. Note the addition of the * saying that the input should be a pointer.
forAll (*this, i)
{
this->operator[](i) = f[i];
}
tf.clear();
}
For what it is worth, I also tried to change line 411 in UList.H to take a pointer, as the error message suggests, however that creates other errors, as I suppose that the function is used elsewhere (correctly)
So, to me, it seems that the problem was simply a missing * , however I do not have the foresight to see if this will have other changes, or if it is the best way to solve my problem.
I am curious to hear what other people have to say about this problem and solution. I assigned this to HJ because they changed the lines that I referenced here.
Best,
Erik Nijkamp
Fixed in nextRelease, fc9d091d21dedbdfb2d5d63b4221b67b9ef493f6