Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from pymontecarlo/emas2017
Emas2017
- Loading branch information
Showing
410 changed files
with
35,160 additions
and
19,574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[run] | ||
omit=pymontecarlo/_version.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pymontecarlo/testdata/settings.cfg | ||
*.bak | ||
sketch/*.png | ||
cover/ | ||
|
||
#### joe made this: https://goel.io/joe | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,62 @@ | ||
import os | ||
import time | ||
from math import radians | ||
import math | ||
|
||
# Options | ||
from pymontecarlo.input import * | ||
# Create options | ||
import pymontecarlo | ||
from pymontecarlo.options.beam import GaussianBeam | ||
from pymontecarlo.options.material import Material | ||
from pymontecarlo.options.sample import HorizontalLayerSample, SubstrateSample, VerticalLayerSample | ||
from pymontecarlo.options.detector import PhotonDetector | ||
from pymontecarlo.options.analysis import KRatioAnalysis | ||
from pymontecarlo.options.limit import ShowersLimit | ||
from pymontecarlo.options.options import Options | ||
|
||
ops = Options('simulation1') | ||
ops.beam.energy_eV = 5e3 | ||
program = pymontecarlo.settings.get_program('casino2') | ||
|
||
ops.geometry = MultiLayers(Material('Brass', {30: 0.37, 29: '?'})) | ||
ops.geometry.add_layer(pure(28), 500e-9) # 500 nm thick | ||
beam = GaussianBeam(15e3, 10e-9) | ||
|
||
elevation=(radians(35), radians(45)) # Take-off angle: 40 deg | ||
azimuth=(0.0, radians(360)) # Annular detector | ||
ops.detectors['intensity'] = PhotonIntensityDetector(elevation, azimuth) | ||
ops.detectors['spectrum'] = \ | ||
PhotonSpectrumDetector(elevation, azimuth, (0.0, ops.beam.energy_eV), 1000) | ||
mat1 = Material.pure(29) | ||
mat2 = Material.from_formula('SiO2') | ||
mat3 = Material('Stuff', {27: 0.5, 25: 0.2, 8: 0.3}, 4500.0) | ||
|
||
ops.limits.add(ShowersLimit(10000)) | ||
ops.models.add(ELASTIC_CROSS_SECTION.rutherford) | ||
sample = SubstrateSample(mat2) | ||
|
||
# Converter, Exporter, Importer -> Worker | ||
from pymontecarlo.runner.runner import Runner | ||
from pymontecarlo.program.nistmonte.runner.worker import Worker # Program specific | ||
#sample = HorizontalLayerSample(mat3) | ||
#sample.add_layer(mat2, 10e-9) | ||
#sample.add_layer(mat3, 25e-9) | ||
|
||
outputdir = '/tmp' # TO BE CHANGED | ||
runner = Runner(Worker, outputdir, nbprocesses=1) | ||
runner.put(ops) | ||
#sample = VerticalLayerSample(mat1, mat2) | ||
#sample.add_layer(mat3, 10e-9) | ||
|
||
runner.start() | ||
photon_detector = PhotonDetector('xray', math.radians(35.0)) | ||
analysis = KRatioAnalysis(photon_detector) | ||
|
||
while runner.is_alive(): | ||
counter, progress, status = runner.report() | ||
print counter, progress, status | ||
time.sleep(1) | ||
limit = ShowersLimit(1000) | ||
|
||
runner.stop() # Not really required, but to be saved | ||
options = Options(program, beam, sample, [analysis], [limit]) | ||
|
||
# Results | ||
from pymontecarlo.output.results import Results | ||
# Run simulation | ||
from pymontecarlo.runner.local import LocalSimulationRunner | ||
from pymontecarlo.project import Project | ||
|
||
project = Project() | ||
project.filepath = '/tmp/example.mcsim' | ||
|
||
with LocalSimulationRunner(project, max_workers=3) as runner: | ||
futures = runner.submit(options) | ||
print('{} simulations launched'.format(len(futures))) | ||
|
||
results = Results.load(os.path.join(outputdir, ops.name + '.h5')) | ||
while not runner.wait(1): | ||
print(runner.progress) | ||
|
||
print results['intensity'].intensity('Ni La') | ||
print('{} simulations succeeded'.format(runner.done_count)) | ||
print('{} simulations failed'.format(runner.failed_count)) | ||
for future in runner.failed_futures: | ||
print(future.exception()) | ||
|
||
# Results | ||
#project.recalculate() | ||
print('{} were simulated'.format(len(project.simulations))) | ||
for simulation in project.simulations: | ||
print(simulation.results) | ||
|
||
project.write() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,34 @@ | ||
#!/usr/bin/env python | ||
""" | ||
================================================================================ | ||
:mod:`pymontecarlo` -- Common interface to several Monte Carlo codes | ||
================================================================================ | ||
Common interface to several Monte Carlo codes | ||
""" | ||
|
||
__author__ = "Philippe T. Pinard" | ||
__email__ = "philippe.pinard@gmail.com" | ||
__copyright__ = "Copyright (c) 2011 Philippe T. Pinard" | ||
__license__ = "GPL v3" | ||
|
||
# This is required to create a namespace package. | ||
# A namespace package allows programs to be located in different directories or | ||
# eggs. | ||
|
||
__import__('pkg_resources').declare_namespace(__name__) | ||
|
||
from ._version import get_versions | ||
__version__ = get_versions()['version'] | ||
del get_versions | ||
|
||
# Standard library modules. | ||
import os | ||
import sys | ||
|
||
# Third party modules. | ||
|
||
# Local modules. | ||
|
||
# Globals and constants variables. | ||
|
||
#--- Units | ||
|
||
import pint | ||
|
||
unit_registry = pint.UnitRegistry() | ||
unit_registry.define('electron = mol') | ||
pint.set_application_registry(unit_registry) | ||
|
||
#--- Settings | ||
|
||
from pymontecarlo._settings import Settings | ||
|
||
try: | ||
settings = Settings.read() | ||
except: | ||
settings = Settings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
"""""" | ||
|
||
# Standard library modules. | ||
import os | ||
import argparse | ||
import multiprocessing | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
# Third party modules. | ||
import tabulate | ||
|
||
# Local modules. | ||
import pymontecarlo | ||
from pymontecarlo.util.cbook import find_by_type | ||
|
||
# Globals and constants variables. | ||
|
||
def _create_parser(): | ||
prog = 'pymontecarlo' | ||
description = 'Run, configure pymontecarlo' | ||
parser = argparse.ArgumentParser(prog=prog, description=description) | ||
|
||
parser.add_argument('-v', '--verbose', action='store_true', | ||
help='Run in debug mode') | ||
parser.add_argument('--programs', action='store_true', | ||
help='List available and configured programs') | ||
|
||
return parser | ||
|
||
def _create_commands(parser): | ||
subparsers = parser.add_subparsers(title='Commands', dest='command') | ||
_create_run_command(subparsers.add_parser('run')) | ||
_create_config_command(subparsers.add_parser('config')) | ||
|
||
def _create_run_command(parser): | ||
parser.description = 'Run simulation(s) and save results in a project.' | ||
|
||
nprocessors = multiprocessing.cpu_count() | ||
|
||
parser.add_argument('-o', required=False, metavar='FILE', | ||
help='Path to project') | ||
parser.add_argument('-s', action='store_true', | ||
help='Skip existing simulations in project') | ||
parser.add_argument('-n', type=int, default=nprocessors, | ||
help='Number of processors to use') | ||
|
||
def _create_config_command(parser): | ||
parser.description = 'Configure pymontecarlo and Monte Carlo programs.' | ||
|
||
# Programs | ||
subparsers_programs = parser.add_subparsers(title='Programs', dest='program') | ||
|
||
for clasz, program in pymontecarlo.settings.iter_programs(): | ||
configurator = clasz.create_configurator() | ||
identifier = clasz.getidentifier() | ||
|
||
parser_program = subparsers_programs.add_parser(identifier) | ||
|
||
group_activation = parser_program.add_mutually_exclusive_group(required=True) | ||
group_activation.add_argument('--activate', action='store_true', | ||
help='Activate program') | ||
group_activation.add_argument('--deactivate', action='store_false', | ||
help='Deactivate program') | ||
|
||
try: | ||
configurator.prepare_parser(parser_program, program) | ||
except: | ||
logger.exception('Prepare parser failed') | ||
subparsers_programs._name_parser_map.pop(identifier) | ||
|
||
def _parse(parser, ns): | ||
if ns.verbose: | ||
logger.setLevel(logging.DEBUG) | ||
|
||
if ns.programs: | ||
header = ['Program', 'Available', 'Configured', 'Details'] | ||
rows = [] | ||
for clasz, program in pymontecarlo.settings.iter_programs(): | ||
configurator = clasz.create_configurator() | ||
identifier = clasz.getidentifier() | ||
configured = program is not None | ||
|
||
details = [] | ||
if configured: | ||
dummy = argparse.ArgumentParser() | ||
configurator.prepare_parser(dummy, program) | ||
for action in dummy._actions: | ||
if action.default == argparse.SUPPRESS: | ||
continue | ||
details.append('{}: {}'.format(action.dest, action.default)) | ||
|
||
rows.append([identifier, True, configured, ', '.join(details)]) | ||
|
||
parser.exit(message=tabulate.tabulate(rows, header) + os.linesep) | ||
|
||
def _parse_commands(parser, ns): | ||
if ns.command == 'run': | ||
_parse_run_command(parser, ns) | ||
parser.parse_args(['run', '--help']) | ||
|
||
if ns.command == 'config': | ||
_parse_config_command(parser, ns) | ||
parser.parse_args(['config', '--help']) | ||
|
||
def _parse_run_command(parser, ns): | ||
pass | ||
|
||
def _parse_config_command(parser, ns): | ||
if ns.program: | ||
settings = pymontecarlo.settings | ||
|
||
program_class = None | ||
for clasz in settings.iter_available_programs(): | ||
if ns.program == clasz.getidentifier(): | ||
program_class = clasz | ||
break | ||
|
||
if program_class is None: | ||
parser.exit('Cannot find type of program') | ||
|
||
# Create program | ||
configurator = program_class.create_configurator() | ||
if ns.activate: | ||
program = configurator.create_program(ns, program_class) | ||
|
||
# Validate | ||
try: | ||
validator = program.create_validator() | ||
validator.validate_program(program, None) | ||
except Exception as ex: | ||
parser.error(str(ex)) | ||
|
||
# Remove existing program | ||
configured_programs = settings.programs | ||
for configured_program in find_by_type(configured_programs, program_class): | ||
settings.programs.remove(configured_program) | ||
|
||
# Add new program | ||
if ns.activate: | ||
settings.programs.append(program) | ||
|
||
# Save settings | ||
settings.write() | ||
parser.exit(message='Settings updated and saved' + os.linesep) | ||
|
||
def main(): | ||
parser = _create_parser() | ||
_create_commands(parser) | ||
|
||
ns = parser.parse_args() | ||
|
||
_parse(parser, ns) | ||
_parse_commands(parser, ns) | ||
parser.print_help() | ||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.