FreeFem / FreeFem-sources

FreeFEM source code
https://freefem.org/
Other
776 stars 191 forks source link

Bug with the name of a key of string[string] object inside macro #171

Closed abaikin closed 3 years ago

abaikin commented 3 years ago

If I create an array, pass it to macro and try to create an element of string[string] object with the key equal to array var name ( for example, "array"), then I get the following error

s1[array] error operator <5MyMapI6StringS0_E>, List of choices ( : <5MyMapI6StringS0_E>, )

If I pass not array but simple type like real, it works well.

FreeFem++ is version 4.8, gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04). The code is

` real[int] array; macro macr(array){

string[string] s1;
s1["array"] = "sds";

}

//EOM macr(array);

`

prj- commented 3 years ago

What are you trying to do? If you change your macro definition to something like

macro macr(arrayParameterName){

It does work OK.

abaikin commented 3 years ago

For convenience, I want to make the name of the keys of the map equal to the object names I pass to a macro.

Yes, your suggestion does work. But it is very weird that the naming of the variable (arrays only) influences the keys naming for string[string] map. Is it possible to fix it in FF?

frederichecht commented 3 years ago

Dear Abaikin,

I really do not understand what you want exactly. Can you explain with example? Best, F. Hecht

Le 15 avr. 2021 à 08:17, abaikin @.***> a écrit :

Yes, it does work. But it is very weird that the naming of the variable (arrays only) influences the keys naming for string[string] map. Is it possible to fix it in FF?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/FreeFem/FreeFem-sources/issues/171#issuecomment-820146633, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLPNDBUZLPI375NBWPKYA3TI2AHNANCNFSM426XP44A.

abaikin commented 3 years ago

Dear Dr. Hecht,

I have an input h5-file. I create the string[string] simInputDatasetsNames to store the names of the datasets paths.

For example, simInputDatasetsNames["volumeRate"] = "SCHEDULE/VOLUME_RATE_TABLE/VOLUME_RATE";

In my code I create variable real[int] volumeRate

For convenience, the name of the key "volumeRate" is the same as the name of the variable volumeRate. And I create a macro where I read all the parameters from h5-file to the freefem variables (I write my own h5-reader).

The simplified code is

real[int] volumeRate;
macro readSimInputHDF5(volumeRate){
string[string] simInputDatasetsNames;
simInputDatasetsNames["volumeRate"] = "SCHEDULE/VOLUME_RATE_TABLE/VOLUME_RATE";

// My user-defined type and function    
hdf5Reader reader1;
ReadRealArray1DHDF5Dataset(reader1, simInputDatasetsNames["volumeRate"], volumeRate);
} // EOM

// call
readSimInputHDF5(volumeRate)

The problem is that volumeRate variable name in ff language is in conflict with the string "volumeRate" unexpectedly.

It works, if I take not array type. The real, for example,

real volumeRate;

Why it does not work for arrays?

frederichecht commented 3 years ago

Dear abaikin,

you can use Stringification(a) to wrap a nam in string. I think this you can solve your problem

Best F. Hecht.

Le 15 avr. 2021 à 11:17, abaikin @.***> a écrit :

Dear Dr. Hecht,

I have an input h5-file. I create the string[string] simInputDatasetsNames to store the names of the datasets paths.

For example, simInputDatasetsNames["volumeRate"] = "SCHEDULE/VOLUME_RATE_TABLE/VOLUME_RATE";

In my code I create variable real[int] volumeRate

For convenience, the name of the key "volumeRate" is the same as the name of the variable volumeRate. And I create a macro where I read all the parameters from h5-file to the freefem variables (I write my own h5-reader).

The simplified code is

real[int] volumeRate; macro readSimInputHDF5(volumeRate){ string[string] simInputDatasetsNames; simInputDatasetsNames["volumeRate"] = "SCHEDULE/VOLUME_RATE_TABLE/VOLUME_RATE";

// My user-defined type and function
hdf5Reader reader1; ReadRealArray1DHDF5Dataset(reader1, simInputDatasetsNames["volumeRate"], volumeRate); } // EOM

// call readSimInputHDF5(volumeRate) The problem is that volumeRate variable name in ff language is in conflict with the string "volumeRate" unexpectedly.

It works, if I take not array type. The real, for example,

real volumeRate;

Why it does not work for arrays?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/FreeFem/FreeFem-sources/issues/171#issuecomment-820270254, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLPNDGD4LT6SIX47PQHEG3TI2VLVANCNFSM426XP44A.

abaikin commented 3 years ago

Dear Dr. Hecht, Thank you, yes it solves my particular problem.

I tried the more general workaround and it works

ReadRealArray1DHDF5Dataset(reader1, simInputDatasetsNames[Stringification("volumeRate")], volumeRate);

frederichecht commented 3 years ago

you do not need " in Stringification

ReadRealArray1DHDF5Dataset(reader1, simInputDatasetsNames[Stringification(volumeRate)], volumeRate);

Le 15 avr. 2021 à 14:02, abaikin @.***> a écrit :

Dear Dr. Hecht, Thank you, yes it solves my particular problem.

I tried the more general workaround and it works

ReadRealArray1DHDF5Dataset(reader1, simInputDatasetsNames[Stringification("volumeRate")], volumeRate);

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/FreeFem/FreeFem-sources/issues/171#issuecomment-820369097, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLPNDF76HWEBXCXX2NH4BDTI3IVTANCNFSM426XP44A.