Skip to content

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
tincotema committed Aug 9, 2021
1 parent 00e650f commit 38e255a
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 30 deletions.
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions mpw/manager.py → mcpw/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import argparse
import os
from importlib import import_module
from mcstas_rapper import run_mcstas, run_compiler, run_instrument,
valid_config, valid_mcconfig,
psave, pload, load_python_instr_file
from mcpw.mcstas_wrapper import run_mcstas, run_compiler, run_instrument,\
valid_config, valid_mcconfig,\
psave, pload,\
check_for_detector_output, get_result_path_from_input

#for exeption handling
Expand Down Expand Up @@ -59,7 +59,9 @@ def error(self, message):


# importing var and mcvar from local_var.py and reseda.py files
sys.path.append(os.getcwd())
from local_var import variables

var = variables()
valid_config(var)

Expand Down
39 changes: 32 additions & 7 deletions mpw/mcstas_rapper.py → mcpw/mcstas_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def which(program):
if sp.run(["which", program], stdout=sp.DEVNULL, stderr=sp.DEVNULL).returncode == 1:
sys.exit(f" '{program}' not found or is not an executable")

def execute(command, errormsg, successmsg):
print(f"runing: {command}")
def execute(command, errormsg, successmsg, print_command=True):
if print_command:
print(f"runing: {command}")
run_return = sp.run(command, shell=True, text=True, stdout=sp.PIPE, stderr=sp.PIPE)
# check if the process was successfully
if run_return.returncode != 0:
Expand Down Expand Up @@ -86,7 +87,7 @@ def run_compiler(var,mcvar, cflags=""):
if var.mpi == 0:
run_string = f"nice gcc "
else:
run_string = f"ncie mpicc -DUSE_MPI "
run_string = f"nice mpicc -DUSE_MPI "
run_string = run_string + f"-o {var.p_local/instr_out_file} {var.p_local/instr_c_file} -lm -g -O2 -std=c99 {cflags}"
# exectue the run_string and capture the output
execute(run_string, "An error occurred while running the C Compiler", "C compiler done")
Expand All @@ -111,15 +112,18 @@ def run_instrument(var,mcvar):
else:
params = params + f"{var_name}={var_value} "
# scan or no scan
if scan_var is not []:
if scan_var:
#scan
#creating main result directory
os.mkdir(var.p_local/var.sim_res/mcvar.dn)
#scanning all points
print(f"running: {run_string} {scan_var[0]}={scan_var[1].mc} ")
for i in range (scan_var[1].N):
print(f"step: {scan_var[0]}={scan_var[1].absolute_value(i)}")
i_params = params + f"{scan_var[0]}={scan_var[1].absolute_value(i)} "
final_run_string = run_string + f"-d {str(var.p_local/var.sim_res/mcvar.dn/str(i))} {i_params} "
execute(final_run_string, errormsg, successmsg)

execute(final_run_string, errormsg, successmsg, print_command=False)
else:
final_run_string = run_string + f"-d {str(var.p_local/var.sim_res/mcvar.dn)} {params} "
execute(final_run_string, errormsg, successmsg)
Expand Down Expand Up @@ -151,7 +155,7 @@ def is_scan(mcvar):
return False

def check_for_detector_output(var, mcvar):
if is_scan:
if is_scan(mcvar):
for i in range(mcvar.scan.N):
if not os.path.isdir(var.p_local/var.sim_res/mcvar.dn/str(i)):
print(f"the mcstas output dir {mcvar.dn}/{i} dose not exist.\nexiting")
Expand All @@ -161,5 +165,26 @@ def check_for_detector_output(var, mcvar):
print(f"the mcstas output dir {mcvar.dn} dose not exist.\n exiting")
exit()
else:
print("allfine")
#print("all fine")
return

def get_result_path_from_input(var, mcvar, msg, args):# logic for retreiveng the correct name for the result foulder
if args.func == 'analyse':
if args.result_dir:
return args.result_dir, msg
else:
d = var.p_local/var.sim_res
return sorted(d.iterdir(), key=os.path.getmtime, reverse=True)[0].name, msg
if not args.result_dir:
name = mcvar.dn
else:
name = args.result_dir
if os.path.isdir(var.p_local/var.sim_res/name):
counter = 0
new_name = name + "_" + str(counter)
while os.path.isdir(var.p_local/var.sim_res/new_name):
counter = counter + 1
new_name = name + "_" + str(counter)
msg = f'####################\n new result directory is {new_name}\n####################\n'
return new_name, msg
return name, msg
2 changes: 1 addition & 1 deletion mpw/setup.py → mcpw/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess as sp #needed to run mcstas
import argparse
import os
from setup_tools import create_local_var, create_python_file
from mcpw.setup_tools import create_local_var, create_python_file



Expand Down
18 changes: 9 additions & 9 deletions mpw/setup_tools.py → mcpw/setup_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create_local_var(args):
class_variables_lines.append(f' self.mcstas = "{args.mcstas}"')
class_variables_lines.append(f' self.componentdir = "{args.component_dir}"')
class_variables_lines.append(f" #mcstas variables")
class_variables_lines.append(f' self.mpi = 2')
class_variables_lines.append(f' self.mpi = 0')
class_variables_lines.append(f" #additional c compiler flags")
class_variables_lines.append(f' self.cflags = ""')

Expand Down Expand Up @@ -119,7 +119,7 @@ def create_python_file(args):
class_mcvariables_lines.append(' self.dn = "default"')
class_mcvariables_lines.append(" self.n = 1000000")
class_mcvariables_lines.append(f' self.instr_file = "{instr_file}" #the name of the instrument file, must be located in p_server/p_local, all custom components used by the instrument must be located in the same diretory')
class_mcvariables_lines.append(" self.scan = Scan(-0.1,0.1,'A') # (begining, ending, Unit)")
class_mcvariables_lines.append(" self.scan = Scan(-0.1,0.1,'A', 3) # (begining, ending, Unit, number of steps)")
class_mcvariables_lines.append(" #variables defined in the DEFINE INSTRUMENT section of the mcstas instrument")
for line in var_lines:
if line.__contains__("="):
Expand All @@ -129,7 +129,7 @@ def create_python_file(args):

header_lines = []
header_lines.append(f'# import section')
header_lines.append(f'from mpw.mcstas_wrapper import Scan')
header_lines.append(f'from mcpw.mcstas_wrapper import Scan')
header_lines.append(f'')
header_lines.append(f'#this file must allways contain:')
header_lines.append(f'#class mcvariables() a class containing all parameters for the mcstas instrument file')
Expand All @@ -141,10 +141,10 @@ def create_python_file(args):
main_lines.append(f'# your custom function #')
main_lines.append(f'###########################')
main_lines.append(f'')
main_lines.append(f'def custom_function1(var,mcvar)')
main_lines.append(f'def custom_function1(var,mcvar,msg):')
main_lines.append(f' print("hallo world 1")')
main_lines.append(f'')
main_lines.append(f'def custom_function2(var,mcvar)')
main_lines.append(f'def custom_function2(var,mcvar,msg):')
main_lines.append(f' print("hallo world 2")')
main_lines.append(f'')
main_lines.append(f'')
Expand All @@ -153,12 +153,12 @@ def create_python_file(args):
main_lines.append(f'##########################################################')
main_lines.append(f'')
main_lines.append(f'# adding functions to post mcstas function group')
main_lines.append(f'def post_mcrun_funktions(var,mcvar)')
main_lines.append(f' custom_function1(var,mcvar)')
main_lines.append(f'def post_mcrun_funktions(var,mcvar,msg):')
main_lines.append(f' custom_function1(var,mcvar,msg)')
main_lines.append(f'')
main_lines.append(f'# adding functions to analyse function group')
main_lines.append(f'def post_mcrun_funktions(var,mcvar)')
main_lines.append(f' custom_function2(var,mcvar)')
main_lines.append(f'def analyse(var,mcvar,msg):')
main_lines.append(f' custom_function2(var,mcvar,msg)')
main_lines.append(f'')
main_lines.append(f'# end of documentation')

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sys
subprocess
#subprocess
argparse
os
pathlib
Expand Down
16 changes: 7 additions & 9 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[metadata]
name = mcstas-python-wrapper
version = 0.0.1
name = mcpw
version = 0.0.10
author = Tincotema
author_email = [email protected]
description = A lightweight mcstas wrapper
description = A lightweight mcstas python wrapper
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/tincotema/mcstas-python-wrapper
Expand All @@ -12,10 +12,8 @@ project_urls =
license = MIT
python_requires = >=3.6
classifiers =
Enivroment :: Console
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Operating System :: UNIX
keywords =
mcstas
wrapper
Expand All @@ -24,12 +22,12 @@ keywords =

[files]
packages =
mpw
mcpw

[options.entry_points]
console_scripts =
mpw_setup = mpw.setup:main
mpw_manager = mpw.manager:main
mcpw_setup = mcpw.setup:main
mcpw_manager = mcpw.manager:main

[bdist_wheel]
universal = 0

0 comments on commit 38e255a

Please sign in to comment.