Importing data from file

Good evening, I was wondering if there was a way to import parameters from file in FreeFem++, for example in the way JSON does. I am aware of the existence of the getARGV() method but I was hoping for something more practical that didn’t require the insertion of the parameters through command line, as in my case they would be too many and it would definitely require too much time.

Thanks in advance :slight_smile:

Hello,
if you have the data file “datafile.dat” as

1. -3. 2.5
4. 6.
2

You can read it with

real a,b,c,d,e;
int nn;
{
 ifstream readparams("datafile.dat");
 readparams >> a >> b >> c;// read the first line
 readparams >> d >> e;// read second line
 readparams >> nn;// read the third line
}
cout << "a=" << a << " b=" << b << " c=" << c << " d=" << d << " e=" << e << " nn=" << nn << endl;

You get the output
a=1 b=-3 c=2.5 d=4 e=6 nn=2
I hope this answers your question.

Thank you for your answer Francois. This almost solves my problem…the only thing I wasn’t able to do yet is import functions (for example a forcing term) from file. I even tried workarounds such as importing it as a string and then tried to convert it to func variable type but without success. Do you know how this can be done?

Thank you :slight_smile:

Do you mean importing a function by a data file (this can be done very similarly to the case of parameters, with a loop overall), or by a file giving a formula like “f(a)=1+a^2”. I have never tried to do this last case.
What you can do is to have a separate file “myfunction.edp”, and in your main code you can put include "myfunction.edp"