Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/io/configuration/components/montecarlo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ The ``no_of_packets`` parameter can take a float number for input convenience an
used in each :term:`Monte Carlo` loop. The parameters ``last_no_of_packets`` and ``no_of_virtual_packets`` influence the last run
of the :term:`Monte Carlo` loop (which calculates the final spectrum!) when the radiation field should have converged. ``last_no_of_packets`` is normally higher than
``no_of_packets`` to create a less noisy output spectrum. ``no_of_virtual_packets`` can also be set to greater than 0 (a useful number is 3) to
use the Virtual Packet formalism. Increasing this number drastically increases computational costs (and memory requirements if they are logged).
use the Virtual Packet formalism. Virtual packets are generated after the last run by the spectrum solver, which post-processes the full
histories of the last run's real packets. When ``no_of_virtual_packets`` is greater than 0, TARDIS therefore records the full
real-packet histories of the last run in ``transport_state.tracker_full_df``, as with ``tracking.track_rpacket``, so memory use in that
run grows with ``last_no_of_packets`` and the number of interactions per packet. Increasing ``no_of_virtual_packets`` drastically increases computational costs (and memory requirements if they are logged).
Virtual packets are not currently supported by the Type IIP workflow, which ignores ``no_of_virtual_packets``.
The ``iterations`` parameter describes the maximum number of :term:`Monte Carlo` loops executed in a simulation before it ends. Convergence criteria can be used to make the simulation stop
sooner when the convergence threshold has been reached (see :doc:`../../../physics_walkthrough/update_and_conv/update_and_conv`).

Expand Down
99 changes: 49 additions & 50 deletions docs/io/output/vpacket_logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,58 @@ simulation's :ref:`virtual packets <virtual_packets>`.

The parent-interaction fields are populated from the real-packet histories
used by virtual-packet spectrum post-processing. Virtual packets spawned at the
inner boundary retain the ``NO_INTERACTION`` defaults.

After running the simulation, the following information can be retrieved:

.. note::
The following tables list the attributes of the simulation in which the information would be stored. For
example, if you call your TARDIS simulation ``sim``, you would access the virtual packet frequencies by running
``sim.transport.virt_packet_nus``.
inner boundary have ``last_interaction_type`` ``NO_INTERACTION``, ``before_nu``
0, ``radius`` equal to the inner boundary radius, and -1 for ``shell_id`` and
the line ids.

After running the simulation, the logged virtual packets are available from the
spectrum solver as a DataFrame with one row per escaping virtual packet. For a
TARDIS simulation ``sim``, run ``sim.spectrum_solver.vpacket_tracker_to_df()``;
workflows provide the same method on ``workflow.spectrum_solver``.
The interaction columns use the same names and types as the real-packet
``transport_state.tracker_last_interaction_df``. Each row
describes the real-packet event that spawned the virtual packet.

.. list-table::
:header-rows: 1
* - Attribute of Simulation

* - Column
- Type
- Description
* - ``virt_packet_logging``
- Boolean
- Shows if virtual packet logging is turned on
* - ``transport.virt_packet_nus``
- Numpy array
- Virtual packet frequencies
* - ``transport.virt_packet_energies``
- Numpy array
- Virtual packet energies
* - ``transport.virt_packet_initial_mus``
- Numpy array
- Propagation directions that virtual packets are launched at
* - ``transport.virt_packet_initial_rs``
- Numpy array
- Radii that virtual packets are launched at
* - ``transport.virt_packet_last_interaction_type``
- Numpy array
- | Type of interaction that caused the virtual packets to be spawned
| (enum, see :doc:`../../how_to/output/how_to_physical_quantities`)
* - ``transport.virt_packet_last_interaction_in_nu``
- Numpy array
- Frequencies of the r-packets which spawned the virtual packet
* - ``transport.virt_packet_last_interaction_in_r``
- Numpy array
- Radii of the r-packets which spawned the virtual packet
* - ``transport.virt_packet_last_line_interaction_in_id``
- Numpy array
- | If the last interaction was a line interaction, the
| line_interaction_in_id for that interaction
| (see :doc:`../../how_to/output/how_to_physical_quantities`)
* - ``transport.virt_packet_last_line_interaction_out_id``
- Numpy array
- | If the last interaction was a line interaction, the
| line_interaction_out_id for that interaction
| (see :doc:`../../how_to/output/how_to_physical_quantities`)
* - ``transport.virt_packet_last_line_interaction_shell_id``
- Numpy array
- | If the last interaction was a line interaction, the
| line_interaction_shell_id for that interaction
| (see :doc:`../../how_to/output/how_to_physical_quantities`)
* - ``last_interaction_type``
- Categorical
- | Type of the real-packet event that spawned the virtual packet;
| ``NO_INTERACTION`` for virtual packets spawned at the inner boundary
* - ``radius``
- Float
- Radius of the spawning real-packet event [cm]
* - ``shell_id``
- Integer
- Shell of the spawning real-packet event
* - ``before_nu``
- Float
- Frequency of the real packet before the spawning event [Hz]
* - ``line_absorb_id``
- Integer
- | Absorbing line of the spawning event, or -1 if it was not a line
| interaction (see :doc:`../../how_to/output/how_to_physical_quantities`)
* - ``line_emit_id``
- Integer
- | Emitting line of the spawning event, or -1 if it was not a line
| interaction (see :doc:`../../how_to/output/how_to_physical_quantities`)
* - ``nus``
- Float
- Escaping virtual packet frequency [Hz]
* - ``energies``
- Float
- Escaping virtual packet energy [erg]
* - ``initial_mus``
- Float
- Propagation direction the virtual packet was launched at
* - ``initial_rs``
- Float
- Radius the virtual packet was launched at [cm]

When a simulation is saved to HDF, this DataFrame is stored at
``/simulation/spectrum_solver/vpacket_tracker_df``, with
``last_interaction_type`` stored as ``InteractionType`` integer codes.
13 changes: 11 additions & 2 deletions docs/physics_walkthrough/spectrum/virtualpackets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ Virtual Packet Procedure
In the virtual packet scheme, a new population of Monte Carlo packets is
introduced. TARDIS first records the launch and physical-interaction states of
the real packets. After real-packet transport is complete, these histories are
replayed against the same frozen geometry and opacity state. At every recorded
launch or physical interaction, a pre-defined number of virtual packets,
passed to the ``SpectrumSolver`` and replayed against the same frozen geometry
and opacity state. At every recorded launch or physical interaction, a
pre-defined number of virtual packets,
:math:`N_v`, are generated. The propagation of these "virtual packets" is followed in a
similar fashion to the real ones with the important distinction that their
trajectory is never changed. However, the optical depth the virtual packet
Expand All @@ -69,6 +70,14 @@ sampling separate from real-packet transport. Consequently, changing
:math:`N_v` changes the sampling noise of the virtual-packet spectrum but not
the real-packet trajectories, estimators, or spectrum.

The histories are read from the full real-packet tracking DataFrame
(``transport_state.tracker_full_df``), so the virtual spectrum can also be
recomputed later from a saved copy of that DataFrame with
``SpectrumSolver.solve_virtual_packet_spectrum``. This also needs the run's
transport state (geometry and opacity), Monte Carlo configuration and
spectrum settings. When saving the DataFrame to HDF, use ``format="table"``,
which supports its categorical columns.

.. note::

TARDIS is a time-independent radiative transfer scheme. Thus, :math:`\Delta
Expand Down
38 changes: 4 additions & 34 deletions docs/tutorials/run_montecarlo_transport.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"source": [
"# Running Monte Carlo Transport Independently\n",
"\n",
"This tutorial demonstrates how to run the Monte Carlo transport loop directly using `Simulation.from_config` without running full TARDIS iterations. This approach gives you direct control over the Monte Carlo transport process."
"This tutorial demonstrates how to run the real-packet Monte Carlo transport loop directly using `Simulation.from_config` without running full TARDIS iterations. This approach gives you direct control over the Monte Carlo transport process. Virtual-packet spectra are computed separately by `SpectrumSolver` after transport completes."
]
},
{
Expand Down Expand Up @@ -46,15 +46,11 @@
" packet_propagation,\n",
")\n",
"from tardis.transport.montecarlo.modes.montecarlo_transport import (\n",
" calculate_virtual_packet_spectrum,\n",
" montecarlo_transport,\n",
")\n",
"from tardis.transport.montecarlo.packet_source.black_body import (\n",
" BlackBodySimpleSource,\n",
")\n",
"from tardis.transport.montecarlo.packets.virtual_packet import (\n",
" trace_vpacket_volley,\n",
")\n",
"from tardis.transport.montecarlo.packets.trackers import (\n",
" generate_tracker_full_list,\n",
" tracker_full_df2tracker_last_interaction_df,\n",
Expand Down Expand Up @@ -95,7 +91,6 @@
"\n",
"CONFIG_FILE_NAME = \"tardis_example.yml\"\n",
"NUMBER_OF_PACKETS = 10000\n",
"NUMBER_OF_VPACKETS = 0 # Set to 0 to disable virtual packets\n",
"ITERATION_NUMBER = 1\n",
"SHOW_PROGRESS_BARS = True\n",
"TOTAL_ITERATIONS = 1\n",
Expand Down Expand Up @@ -157,7 +152,6 @@
"opacity_state = sim.opacity_state\n",
"montecarlo_configuration = sim.transport.montecarlo_configuration\n",
"time_explosion = sim.simulation_state.time_explosion.to(u.s).value\n",
"spectrum_frequency_grid = sim.transport.spectrum_frequency_grid.to(u.Hz).value\n",
"\n",
"# Create our own independent packet source instead of reusing sim.transport.packet_source\n",
"packet_source = BlackBodySimpleSource(\n",
Expand Down Expand Up @@ -211,7 +205,7 @@
"packet_collection = packet_source.create_packets(NUMBER_OF_PACKETS, seed_offset)\n",
"\n",
"# Setup packet tracking\n",
"if ENABLE_RPACKET_TRACKING or NUMBER_OF_VPACKETS > 0:\n",
"if ENABLE_RPACKET_TRACKING:\n",
" rpacket_trackers = generate_tracker_full_list(\n",
" NUMBER_OF_PACKETS,\n",
" montecarlo_configuration.INITIAL_TRACKING_ARRAY_LENGTH,\n",
Expand All @@ -236,18 +230,6 @@
" SHOW_PROGRESS_BARS,\n",
" packet_propagation,\n",
")\n",
"if NUMBER_OF_VPACKETS > 0:\n",
" v_packets_energy_hist, vpacket_tracker = calculate_virtual_packet_spectrum(\n",
" packet_collection,\n",
" geometry_state_numba,\n",
" time_explosion,\n",
" opacity_state_numba,\n",
" montecarlo_configuration,\n",
" spectrum_frequency_grid,\n",
" rpacket_trackers,\n",
" NUMBER_OF_VPACKETS,\n",
" trace_vpacket_volley,\n",
" )\n",
"\n",
"print(\"Monte Carlo transport completed successfully!\")"
]
Expand All @@ -261,7 +243,7 @@
"source": [
"%%timeit\n",
"timed_packet_collection = packet_source.create_packets(NUMBER_OF_PACKETS, seed_offset)\n",
"if ENABLE_RPACKET_TRACKING or NUMBER_OF_VPACKETS > 0:\n",
"if ENABLE_RPACKET_TRACKING:\n",
" timed_rpacket_trackers = generate_tracker_full_list(\n",
" NUMBER_OF_PACKETS,\n",
" montecarlo_configuration.INITIAL_TRACKING_ARRAY_LENGTH,\n",
Expand All @@ -279,19 +261,7 @@
" timed_rpacket_trackers,\n",
" SHOW_PROGRESS_BARS,\n",
" packet_propagation,\n",
")\n",
"if NUMBER_OF_VPACKETS > 0:\n",
" calculate_virtual_packet_spectrum(\n",
" timed_packet_collection,\n",
" geometry_state_numba,\n",
" time_explosion,\n",
" opacity_state_numba,\n",
" montecarlo_configuration,\n",
" spectrum_frequency_grid,\n",
" timed_rpacket_trackers,\n",
" NUMBER_OF_VPACKETS,\n",
" trace_vpacket_volley,\n",
" )"
")"
]
},
{
Expand Down
5 changes: 4 additions & 1 deletion tardis/io/configuration/schemas/montecarlo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ properties:
type: number
multipleOf: 1.0
default: 0
description: Setting the number of virtual packets for the last iteration.
description: Number of virtual packets spawned per recorded real-packet
launch or interaction in the last iteration. Virtual packets are
generated by post-processing the last iteration's full real-packet
histories.
virtual_spectrum_spawn_range:
type: object
default: {}
Expand Down
3 changes: 0 additions & 3 deletions tardis/io/hdf_writer_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@ def full_hdf_properties(self) -> List[str]:
List[str]
List of all HDF property names.
"""
if hasattr(self, "virt_logging") and self.virt_logging:
self.hdf_properties.extend(self.vpacket_hdf_properties)

return self.optional_hdf_properties + self.hdf_properties

@staticmethod
Expand Down
Loading
Loading