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

Line absorption rate estimator #595

Merged
merged 9 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
5 changes: 4 additions & 1 deletion tardis/montecarlo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _initialize_estimator_arrays(self, no_of_shells, tau_sobolev_shape):
self.j_estimator = np.zeros(no_of_shells, dtype=np.float64)
self.nu_bar_estimator = np.zeros(no_of_shells, dtype=np.float64)
self.j_blue_estimator = np.zeros(tau_sobolev_shape)
self.Edotlu_estimator = np.zeros(tau_sobolev_shape)


def _initialize_geometry_arrays(self, structure):
Expand Down Expand Up @@ -134,6 +135,8 @@ def run(self, model, no_of_packets, no_of_virtual_packets=0, nthreads=1):
# but python expects (no_of_lines, no_of_shells)
self.j_blue_estimator = self.j_blue_estimator.flatten().reshape(
self.j_blue_estimator.shape, order='F')
self.Edotlu_estimator = self.Edotlu_estimator.flatten().reshape(
self.Edotlu_estimator.shape, order='F')

def legacy_return(self):
return (self.output_nu, self.output_energy,
Expand Down Expand Up @@ -248,4 +251,4 @@ def calculate_f_lambda(self, wavelength):

def save_spectra(self, fname):
self.spectrum.to_ascii(fname)
self.spectrum_virtual.to_ascii('virtual_' + fname)
self.spectrum_virtual.to_ascii('virtual_' + fname)
4 changes: 4 additions & 0 deletions tardis/montecarlo/montecarlo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ cdef extern from "src/cmontecarlo.h":
double *continuum_list_nu
int_type_t line_lists_tau_sobolevs_nd
double *line_lists_j_blues
double *line_lists_Edotlu
int_type_t line_lists_j_blues_nd
int_type_t no_of_lines
int_type_t no_of_edges
Expand Down Expand Up @@ -156,6 +157,9 @@ cdef initialize_storage_model(model, runner, storage_model_t *storage):
storage.line_lists_j_blues = <double*> PyArray_DATA(
runner.j_blue_estimator)

storage.line_lists_Edotlu = <double*> PyArray_DATA(
runner.Edotlu_estimator)

storage.line_interaction_id = runner.get_line_interaction_id(
model.tardis_config.plasma.line_interaction_type)

Expand Down
14 changes: 14 additions & 0 deletions tardis/montecarlo/src/cmontecarlo.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ increment_j_blue_estimator (const rpacket_t * packet, storage_model_t * storage,
}
}

void
increment_Edotlu_estimator (const rpacket_t * packet, storage_model_t * storage, int64_t line_idx)
{
if (storage->line_lists_Edotlu != NULL)
{
#ifdef WITHOPENMP
#pragma omp atomic
#endif
storage->line_lists_Edotlu[line_idx] += rpacket_get_energy (packet);
Copy link
Contributor

@unoebauer unoebauer Jun 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not yet the final estimator, right?

Copy link
Contributor Author

@Tobychev Tobychev Jun 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all that we said is needed in the montecarlo code. There is a factor $1 - e^{\tau_{lu}}$ and some normalization needed after the run to get the complete estimator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no - the factor 1 - exp(-tau_s) is not global. Instead it is different for each line interaction. As the subscribed lu suggests, it is a quantity which depends on the line transition. In fact, this is the Sobolev line optical depth. It will be different in each shell and for each line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify: the factor (1 - exp(-tau_s)) describes the interaction probability, i.e. the probability of a packet which comes into resonance with the line to interact with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it depends on the transition, I just meant to say that the tau for each line is added to the estimator(s, the code above is also for each transition and each shell) once all monte carlo steps have finished.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tobychev - hmm, I guess you could do that. But I think it should be easier to add the factor already during the increment. This way you avoid looping over all entries at the end again.

}
}


int64_t
montecarlo_one_packet (storage_model_t * storage, rpacket_t * packet,
int64_t virtual_mode, rk_state *mt_state)
Expand Down Expand Up @@ -613,6 +626,7 @@ montecarlo_line_scatter (rpacket_t * packet, storage_model_t * storage,
if (rpacket_get_virtual_packet (packet) == 0)
{
increment_j_blue_estimator (packet, storage, distance, line2d_idx);
increment_Edotlu_estimator (packet, storage, line2d_idx);
}
double tau_line =
storage->line_lists_tau_sobolevs[line2d_idx];
Expand Down
8 changes: 8 additions & 0 deletions tardis/montecarlo/src/cmontecarlo.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#define LOG_VPACKETS 0
#endif

#define RESONANCE 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be an enum?

#define DOWNBRANCH 1
#define MACROATOM 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather work with enums and not use preprocessor statements.


typedef void (*montecarlo_event_handler_t) (rpacket_t *packet,
storage_model_t *storage,
double distance, rk_state *mt_state);
Expand Down Expand Up @@ -67,6 +71,10 @@ void increment_j_blue_estimator (const rpacket_t * packet,
storage_model_t * storage,
double d_line, int64_t j_blue_idx);

void increment_Edotlu_estimator (const rpacket_t * packet,
storage_model_t * storage,
int64_t j_blue_idx);

int64_t montecarlo_one_packet (storage_model_t * storage, rpacket_t * packet,
int64_t virtual_mode, rk_state *mt_state);

Expand Down
1 change: 1 addition & 0 deletions tardis/montecarlo/src/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct StorageModel
double *line_lists_tau_sobolevs;
int64_t line_lists_tau_sobolevs_nd;
double *line_lists_j_blues;
double *line_lists_Edotlu;
int64_t line_lists_j_blues_nd;
int64_t no_of_lines;
int64_t no_of_edges;
Expand Down
1 change: 1 addition & 0 deletions tardis/montecarlo/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class StorageModel(Structure):
('line_lists_tau_sobolevs_nd', c_int64),
('line_lists_j_blues', POINTER(c_double)),
('line_lists_j_blues_nd', c_int64),
('line_lists_Edotlu', POINTER(c_double)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here's your bug.
The order matters for the definition of the storage_model struct fields. You defined them as

   double *line_lists_j_blues;
  double *line_lists_Edotlu;
   int64_t line_lists_j_blues_nd;

('no_of_lines', c_int64),
('no_of_edges', c_int64),
('line_interaction_id', c_int64),
Expand Down
1 change: 1 addition & 0 deletions tardis/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def legacy_run_simulation(self, model):
model.ws = next_w
model.t_inner = next_t_inner
model.j_blue_estimators = self.runner.j_blue_estimator
model.Edotlu_estimators = self.runner.Edotlu_estimator

model.calculate_j_blues(init_detailed_j_blues=False)
model.update_plasmas(initialize_nlte=False)
Expand Down