CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   converting double to volScalarField (https://www.cfd-online.com/Forums/openfoam/121896-converting-double-volscalarfield.html)

zcarral August 7, 2013 09:39

converting double to volScalarField
 
I need to convert a “double” variable “P” to a “volScalarField” variable “source”.

This is part of my code:


57 volScalarField source=P.value();
58 volScalarField x = mesh.C().component(vector::X);
59 forAll(source,i)
60 {
61 if (x[i] < 0.1) source[i]=P.value();
62 else source[i] = 0;
63 }


When I compile, line 61 is Ok. Nevertheless, I have the following error in line 57:



oFoam.C:57: error: conversion from ‘double’ to non-scalar type ‘Foam::volScalarField’ requested




I tried to change line 57 to “volScalarField source=P;” but I have the same error.

adhiraj August 7, 2013 11:55

I am not sure you can convert a double to a volScalarField. The latter is more than just an array of doubles.
If you want to assign a value to all elements of "source", you are better off using a "forAll" loop.
Line 61 does not flag an error because the operator[] on a volScalarField returns a scalar, and a scalar can be cast to double.

zcarral August 8, 2013 02:50

I need to define the variable "source". I also tried this text in line 57:


57 volScalarField source;


But I have the following error:


oFoam.C:57: error: no matching function for call to ‘Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>::GeometricField()’

romant August 8, 2013 03:47

goal?
 
What is your goal?

One way to do this is to create a field with only ones inside and the multiply the field by the value from your double P.

Bernhard August 8, 2013 05:20

Also look into funkySetFields

zcarral August 8, 2013 05:27

Dear Romant, I agree that your idea is the solution. But I want to know if there is a more elegant form to convert a double to a volScalarField.

Dear Bernhard. FunkySetFields is a possibility. It has the inconvenience that it need to be installed.

Joanne November 8, 2017 10:54

Has anyone ever found a solution to this?

I want to define a field parameter that varies with time but using a double (runTime.value()) to calculate a volScalarField is proving to be problematic.

Any advice?


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