Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derived types containing allocatable character arrays #170

Open
Borrdom opened this issue Nov 3, 2022 · 0 comments
Open

Derived types containing allocatable character arrays #170

Borrdom opened this issue Nov 3, 2022 · 0 comments

Comments

@Borrdom
Copy link

Borrdom commented Nov 3, 2022

Hey,

i wanted to share my observations when using derived types that contain allocatable character arrays.

something like this:


type :: chartype
CHARACTER(7), ALLOCATABLE :: names(:)
end type chartype


I attached a full example as a zip file. With source .f90 files, a setup.py for compiling/wrapping as well as a test.py file.

Say chartype%names is allocated in fortran to become an array of dimension 2 storing the words "default" twice. Then, within python, chartype.names can be accessed as a numpy array of size (7,2), where the 7 additional elements result from the character being split into each individual letter (as discussed in a previous issue #138 ).

The expected result:
print(chartype.names) =>[['d','e','f'','a','u','l','t'],['d','e','f'','a','u','l','t']]
The result i got:
print(chartype.names) => [[100,101,102,97,117,108,116],[100,101,102,97,117,108,116]]
The observation:
These numbers are in fact decimal numbers directly corresponding their individual ASCII character.
So 'd'=>100 'e'=>100 and so on.

This conversion works the other way around, so I constructed a workaround for the wrapper on the python side.
Say from the python side we have the array ["william","michael"]. We can convert this into an array chartype.names= [[119,105,108,108,105,97,109],[109,105,99,104,97,101,108]] which can then be correctly interpreted in fortran.

charexample.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant