Program written in Fortran to python? - fortran

There's a program called GranFilm calculating the polarizability of a sphere. I'd like to extract this value from the program. As you may guess, I have no experience with Fortran. I learned how to successfully import Fortran function but just from a single .f90 file... In the program, there's a dozen of file. I tried to list them all like indicated here: F2py example to no avail. I'm not sure what should be included in the command.
The polarizability is calculated in optics_mod.f90. This file uses Global_definitions.f90 & Interaction_mod.f90.
What I tried:
f2py -m test -h Test.py global_def.f90 optics_mod.f90 integral_mod.f90
What I get:
(base) C:\Users\Intel\Desktop\Sphere>call "C:\Anaconda3\Scripts\\..\python.exe" "C:\Anaconda3\Scripts\\f2py.py" -m test -h test.py global_def.f90 optics_mod.f90 integral_mod.f90
Reading fortran codes...
Reading file 'global_def.f90' (format:free)
Reading file 'os.h' (format:fix)
Reading file 'optics_mod.f90' (format:free)
Reading file 'integral_mod.f90' (format:free)
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "type" with "type_bn".
rmbadname1: Replacing "type" with "type_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
rmbadname1: Replacing "int" with "int_bn".
Post-processing...
Block: test
Block: global_definitions
In: :test:global_def.f90:global_definitions
get_parameters: got "invalid syntax (<string>, line 1)" on '(0.wp,1.'
Block: parameters
Block: surface_constitutive
Block: invariants
Block: mumubar
Block: integrals
Block: polynomials
Block: arth
Block: arth_sp
Block: arth_dp
Block: locate
Block: optics_mod
In: :test:optics_mod.f90:optics_mod
get_parameters: got "invalid syntax (<string>, line 1)" on '(0.wp,1.'
and then I get the same "invalid syntax" for many extra lines but it still saves to a signature file. I wonder what's the cause of that problem?
What I'd like to calculate isn't trivial so the reuse of code is pretty advantageous for my grad project. Any help is greatly appreciated!

Related

PyYAML shows "ScannerError: mapping values are not allowed here" in my unittest

I am trying to test a number of Python 2.7 classes using unittest.
Here is the exception:
ScannerError: mapping values are not allowed here
in "<unicode string>", line 3, column 32:
... file1_with_path: '../../testdata/concat1.csv'
Here is the example the error message relates to:
class TestConcatTransform(unittest.TestCase):
def setUp(self):
filename1 = os.path.dirname(os.path.realpath(__file__)) + '/../../testdata/concat1.pkl'
self.df1 = pd.read_pickle(filename1)
filename2 = os.path.dirname(os.path.realpath(__file__)) + '/../../testdata/concat2.pkl'
self.df2 = pd.read_pickle(filename2)
self.yamlconfig = u'''
--- !ConcatTransform
file1_with_path: '../../testdata/concat1.csv'
file2_with_path: '../../testdata/concat2.csv'
skip_header_lines: [0]
duplicates: ['%allcolumns']
outtype: 'dataframe'
client: 'testdata'
addcolumn: []
'''
self.testconcat = yaml.load(self.yamlconfig)
What is the the problem?
Something not clear to me is that the directory structure I have is:
app
app/etl
app/tests
The ConcatTransform is in app/etl/concattransform.py and TestConcatTransform is in app/tests. I import ConcatTransform into the TestConcatTransform unittest with this import:
from app.etl import concattransform
How does PyYAML associate that class with the one defined in yamlconfig?
A YAML document can start with a document start marker ---, but that has to be at the beginning of a line, and yours is indented eight positions on the second line of the input. That causes the --- to be interpreted as the beginning of a multi-line plain (i.e. non-quoted) scalar, and within such a scalar you cannot have a : (colon + space). You can only have : in quoted scalars. And if your document does not have a mapping or sequence at the root level, as yours doesn't, the whole document can only consists of a single scalar.
If you want to keep your sources nicely indented like you have now, I recommend you use dedent from textwrap.
The following runs without error:
import ruamel.yaml
from textwrap import dedent
yaml_config = dedent(u'''\
--- !ConcatTransform
file1_with_path: '../../testdata/concat1.csv'
file2_with_path: '../../testdata/concat2.csv'
skip_header_lines: [0]
duplicates: ['%allcolumns']
outtype: 'dataframe'
client: 'testdata'
addcolumn: []
''')
yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_config)
You should get into the habit to put the backslash (\) at the end of your first triple-quotes, so your YAML document. If you do that, your error would have actually indicated line 2 because the document doesn't start with an empty line anymore.
During loading the YAML parser encouncters the tag !ConcatTransform. A constructor for an object is probably registered with the PyYAML loader, associating that tag with the using PyYAML's add_constructor, during the import.
Unfortunately they registered their constructor with the default, non-safe, loader, which is not necessary, they could have registered with the SafeLoader, and thereby not force users to risk problems with non-controlled input.

Type error when passing Ocaml a file from the command line but not in repl

My code gives an error when run from a file but runs fine when pasted into the Ocaml repl. I have saved the following as test.ml:
module StringSet = Set.Make(String)
let words = StringSet.add StringSet.empty "something";;
When run from bash with "ocaml test.ml" I get:
File "test.ml", line 3, characters 26-41:
Error: This expression has type StringSet.t = Set.Make(String).t
but an expression was expected of type StringSet.elt = string
When pasted into the Ocaml repl I get:
# module StringSet = Set.Make(String)
let words = StringSet.add StringSet.empty "something";;
module StringSet :
sig
(* ... much more output ... *)
end
val words : StringSet.t = <abstr>
#
Everything seems to work fine from the repl.
My Ocaml version is reported by the repl as: OCaml version 4.02.1.
Does anyone know why the error is produced when running "ocaml test.ml"?
The StringSet.add function takes the element (the string) as the first parameter and the set (StringSet.empty) as the second parameter. You have them in the opposite order.
When I try your code I get the same error in both cases. When I invert the parameter order I don't get an error in either case.
I'm using OCaml 4.06.0, but I would really doubt that the paramter order has changed.

paste r commands inside rmarkdown file

I want my function to place a piece of markdown in a readme.rmd file. However I would like to include some rcode that that will be executed when a new version of the readme file is rendered.
This is what I want in the readme.rmd:
[![Last-changedate](https://img.shields.io/badge/last%20change-r gsub("-", "--", Sys.Date())-yellowgreen.svg)](/commits/master)"
Which at knitr time will turn into a nicely formated shield with the date.
However to paste this in the document I have to escape some of the characters:
paste0("`r ", "gsub(\"-\", \"--\", Sys.Date())", "`")
But this results into
[![Last-changedate](https://img.shields.io/badge/last%20change-`r gsub(\"-\", \"--\", Sys.Date())`-yellowgreen.svg)](/commits/master)"
And this cannot be rendered by rmarkdown error: unexpected input: gsub(\ ^....
With the suggestion of Chinsoon12:
" would using single quote works? i.e. use paste0("r ", "gsub('-', '--', Sys.Date())", "") "
My problem is solved!
I now paste with double and single quotes.
paste0("https://img.shields.io/badge/last%20change-",
"`r ", "gsub('-', '--', Sys.Date())", "`",
"-yellowgreen.svg")

Print type of variable in python3.5 on django 1.8

I am having a hard time of determining the type of my variable since I am used on python2 and have just migrated to python3
from django.http import HttpResponse
def myview(request):
x = "Name"
print (x)
print type(x)
return HttpResponse("Example output")
This code will throw an error because of print type(x). However if you changed that syntax line to type(x). The type does not return an output on the runserver of django.
print in Python 3 is no longer a statement, but a function. You need to call it using parentheses:
print(type(x))

how import statement executes in python?

I read about about import statement in pydocs. It says it executes in two steps.
(1)find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the import statement occurs). The first form (without from) repeats these steps for each identifier in the list. The form with from performs step (1) once, and then performs step (2) repeatedly.
I understood some bits of it, but its still not clear to me completely.I am mainly confused about initialization step and at last it says about repeating some step.The only thing which i understood is that if we use say for example:
import sys
in this case if we use functions of this module in our script we need call them using sys.fun_name(). As the functions weren't made available locally using this importstatement.
But when we use
from sys import argv
We can simply use argv function as it makes it available local for out srcipt.
Can someone please explain me its working and also let me know my understanding is correct or not.
Even i tried to import one of the my script into another script and it gave some strange result which i know have something to do with first step of import statement,(initiallization)
##### ex17.py #####
def print_two(*args):
arg1, arg2 = args
print "arg1: %r, arg2: %r" %(arg1, arg2)
def print_two_again(arg1, arg2):
print "arg1: %r, arg2: %r" %(arg1, arg2)
def print_one(arg1):
print "arg1: %r" %arg1
def print_none():
print "I got nothing."
print_two("Gaurav","Pareek")
print_two_again("Gaurav","Pareek")
print_one("First!")
print_none()
####### ex18.py ######
import ex17
ex17.print_none()
The output which i am getting when executing ex18.py is as below
arg1: 'Gaurav', arg2: 'Pareek'
arg1: 'Gaurav', arg2: 'Pareek'
arg1: 'First!'
I got nothing.
I got nothing.
why is it like this. It should only print I got nothing once.
It prints "I got nothing." twice because the function print_none is being invoked twice. Once when loading the ex17 module (since it's imported in ex18) and once when it's called in the ex18 module. If you don't want the function calls in ex17 to execute but only the function defs to be loaded, then you may write them as follows
## in ex17.py
if __name__ == '__main__':
print_two("Gaurav","Pareek")
print_two_again("Gaurav","Pareek")
print_one("First!")
print_none()
Now this code will only be executed if it's run as a script ie. $ python ex17.py but not when it's imported into some other module. More about __main__ here
About the excerpt from the docs, it simply says how the two import forms differ. Step 1 is responsible for finding and initializing the module and step 2 for adding the names to the local namespace. So in case of,
import sys
both step 1 and 2 will be executed once. But in case of,
from sys import argv, stdout
step 1 will be executed just once, but step 2 will be executed twice as it needs to add both argv and stdout to the local namespace.