CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

Why is this an invalid static_cast of scalarField rA?

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   August 11, 2017, 15:22
Default Why is this an invalid static_cast of scalarField rA?
  #1
Senior Member
 
Klaus
Join Date: Mar 2009
Posts: 118
Rep Power: 10
klausb is on a distinguished road
Why is this operation invalid and how can I fix it?

scalar rA_low = static_cast<float>(rA);

rA is a scalarField in PCG.C, and I need a float version of it which I call rA_low.

I get the following error message:

myMPPCG.C: In member function ‘virtual Foam::solverPerformance Foam::myMPPCG::solve(Foam::scalarField&, const scalarField&, Foam::direction) const’:

myMPPCG.C:115:42: error: invalid static_cast from type ‘Foam::scalarField {aka Foam::Field<double>}’ to type ‘float’
scalar rA_low = static_cast<float>(rA);

How can I create float versions of scalarFields which are of double type?

Klaus
klausb is offline   Reply With Quote

Old   August 11, 2017, 16:02
Default
  #2
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,705
Rep Power: 29
alexeym will become famous soon enoughalexeym will become famous soon enough
Send a message via Skype™ to alexeym
Hi,

scalarField is a field of scalars (or a list of scalars with certain additional operations defined). How you would like to convert a list into single value? If you know, that your field is uniform, then you can use just first element of the list. If your list is non-uniform, propose your way of converting arbitrary list of floats into single float.
alexeym is offline   Reply With Quote

Old   August 11, 2017, 16:51
Default I am looking for a typecast double > float of all values
  #3
Senior Member
 
Klaus
Join Date: Mar 2009
Posts: 118
Rep Power: 10
klausb is on a distinguished road
Hello,

there's a misunderstanding. My question is about typecasting of the scalar field values.

I need another scalarField with typecast rA(double type values) to rA_low (float type values).

I tried the following to but that doesn't work either:

label rAsize = rA.size();
const scalarField rA_low(rAsize);
forAll(rA_low, i)
{
scalar s = static_cast<float>(rA[i]);
rA_low[i] = s;
}

Klaus
klausb is offline   Reply With Quote

Old   August 11, 2017, 17:19
Default
  #4
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,705
Rep Power: 29
alexeym will become famous soon enoughalexeym will become famous soon enough
Send a message via Skype™ to alexeym
I think it would be better to call it mis-explanation, cause this:

Code:
scalar rA_low = static_cast<float>(rA);
is an attempt to convert a field rA into a single float.

OK. It seems, your build is "DP", i.e. scalar is, let's call it alias of, double.

So

Code:
const scalarField rA_low(rAsize);
is a list of doubles. With

Code:
scalar s =  static_cast<float>(rA[i]);
you do static cast to float and then assign it to double.

Why no use Field<float> if you want field of floats? Or you can use floatScalar if you want to stay in OpenFOAM types.

Also it would be nice to decipher "that doesn't work either". Does not compile? Does not work as expected? Works from time to time?
alexeym is offline   Reply With Quote

Old   August 11, 2017, 18:23
Default How to convert and transfer the content of rA?
  #5
Senior Member
 
Klaus
Join Date: Mar 2009
Posts: 118
Rep Power: 10
klausb is on a distinguished road
Thank you for the hints.

Yes the build is DP.

And now I created an empty scalarField rA_low which can take values of type <float>:

label rAsize = rA.size();
floatScalar rA_low(rAsize);

The question is, how to typecast and load the data from rA into rA_low?
klausb is offline   Reply With Quote

Old   August 12, 2017, 03:47
Default
  #6
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,705
Rep Power: 29
alexeym will become famous soon enoughalexeym will become famous soon enough
Send a message via Skype™ to alexeym
It seems we are still do not understand each other.

What is wrong with, for example:

Code:
Field<float> rA_low(rA.size());
forAll(rA, i) { rA_low[i] = static_cast<float>(rA[i]); }
?

You are constantly trying to convert FIELD (i.e. list of numbers) into SCALAR (single number).
alexeym is offline   Reply With Quote

Old   August 12, 2017, 07:08
Default some steps back...
  #7
Senior Member
 
Klaus
Join Date: Mar 2009
Posts: 118
Rep Power: 10
klausb is on a distinguished road
There's rA, a List which contains multiple entries in format <double>.

I need the same List with entries in format <float>, this one I call it rA_low.

The question is how to do such a "typecast conversion" with the List rA?
klausb is offline   Reply With Quote

Old   August 12, 2017, 07:28
Default
  #8
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,705
Rep Power: 29
alexeym will become famous soon enoughalexeym will become famous soon enough
Send a message via Skype™ to alexeym
So I repeat my question, what is wrong with type conversion in cycle?
alexeym is offline   Reply With Quote

Old   August 12, 2017, 16:19
Default
  #9
Senior Member
 
Klaus
Join Date: Mar 2009
Posts: 118
Rep Power: 10
klausb is on a distinguished road
An update, this seems to work:

Code:
List<float> rA_low(nCells);
for (int cell=0; cell<nCells; cell++)
{  
     rA_low[cell] = static_cast<float>(rA[cell]);
}
klausb is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
parallel simulations - error message: "OPT_ITERATIONS: invalid option name" v8areu SU2 5 July 23, 2015 02:57
GroovyBC the dynamic cousin of funkySetFields that lives on the suburb of the mesh gschaider OpenFOAM 300 October 29, 2014 19:00
Problems building Paraview 3.12 on Gentoo Linux pajot OpenFOAM Installation 11 April 11, 2013 08:09
OpenFOAM install on Ubuntu Natty 11.04 bkubicek OpenFOAM 13 May 26, 2011 05:48
Phase locked average in run time panara OpenFOAM 2 February 20, 2008 15:37


All times are GMT -4. The time now is 04:24.