41 return Selection(
"Balance_output_format",
"Format of output file for balance.")
49 return Record(
"Balance",
"Balance of a conservative quantity, boundary fluxes and sources.")
51 .
declare_key(
"add_output_times",
Bool(),
Default(
"true"),
"Add all output times of the balanced equation to the balance output times set. " 52 "Note that this is not the time set of the output stream.")
56 "If true, then balance is calculated at each computational time step, which can slow down the program.")
85 : file_prefix_(file_prefix),
89 allocation_done_(false),
91 output_line_counter_(0),
92 output_yaml_header_(false)
174 for (
auto name : names)
181 unsigned int max_dofs_per_boundary)
217 for(
unsigned int si=0; si<elm->
n_sides(); si++)
262 for (
unsigned int c=0; c<n_quant; ++c)
264 chkerr(MatCreateAIJ(PETSC_COMM_WORLD,
269 (
rank_==0)?n_bulk_regs_per_dof:0,
271 (rank_==0)?0:n_bulk_regs_per_dof,
275 chkerr(MatCreateSeqAIJ(PETSC_COMM_SELF,
282 chkerr(MatCreateSeqAIJ(PETSC_COMM_SELF,
289 chkerr(MatCreateAIJ(PETSC_COMM_WORLD,
300 chkerr(VecCreateMPI(PETSC_COMM_WORLD,
305 chkerr(VecCreateMPI(PETSC_COMM_WORLD,
316 std::string default_file_name;
423 unsigned int region_idx,
430 PetscInt reg_array[1] = { (int)region_idx };
443 unsigned int boundary_idx,
450 PetscInt elem_array[1] = { int(
be_offset_+boundary_idx) };
461 unsigned int region_idx,
469 PetscInt reg_array[1] = { (int)region_idx };
472 loc_dof_indices.size(),
473 &(loc_dof_indices[0]),
480 loc_dof_indices.size(),
481 &(loc_dof_indices[0]),
489 unsigned int region_idx,
500 unsigned int boundary_idx,
531 double temp_source = 0;
532 int lsize, n_cols_mat, n_cols_rhs;
534 const double *vals_mat, *vals_rhs, *sol_array;
535 chkerr(VecGetLocalSize(solution, &lsize));
536 chkerr(VecGetArrayRead(solution, &sol_array));
542 for (
int i=0; i<lsize; ++i){
548 for (
int j=0; j<n_cols_mat; ++j)
549 temp_source += vals_mat[j]*sol_array[i] + vals_rhs[j];
554 chkerr(VecRestoreArrayRead(solution, &sol_array));
561 chkerr(VecCreateMPI(PETSC_COMM_WORLD,
569 chkerr(VecSum(temp, &sum_fluxes));
570 chkerr(VecDestroy(&temp));
589 chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD,
597 chkerr(VecZeroEntries(bulk_vec));
602 chkerr(VecDestroy(&bulk_vec));
618 int lsize, n_cols_mat, n_cols_rhs;
620 const double *vals_mat, *vals_rhs, *sol_array;
621 chkerr(VecGetLocalSize(solution, &lsize));
622 chkerr(VecGetArrayRead(solution, &sol_array));
627 for (
int i=0; i<lsize; ++i)
635 for (
int j=0; j<n_cols_mat; ++j)
639 double f = vals_mat[j]*sol_array[i] + vals_rhs[j];
644 chkerr(VecRestoreArrayRead(solution, &sol_array));
648 chkerr(VecCreateMPI(PETSC_COMM_WORLD,
656 chkerr(VecScale(temp, -1));
661 const double *flux_array;
663 chkerr(VecGetArrayRead(temp, &flux_array));
664 chkerr(VecGetLocalSize(temp, &lsize));
665 for (
int e=0; e<lsize; ++e)
667 if (flux_array[e] < 0)
670 fluxes_in_[quantity_idx][be_regions_[e]] += flux_array[e];
672 chkerr(VecRestoreArrayRead(temp, &flux_array));
673 chkerr(VecDestroy(&temp));
690 const int buf_size = n_quant*2*n_blk_reg + n_quant*2*n_bdr_reg + n_quant;
691 double sendbuffer[buf_size], recvbuffer[buf_size];
692 for (
unsigned int qi=0; qi<n_quant; qi++)
694 for (
unsigned int ri=0; ri<n_blk_reg; ri++)
696 sendbuffer[qi*2*n_blk_reg + + ri] =
sources_in_[qi][ri];
697 sendbuffer[qi*2*n_blk_reg + n_blk_reg + ri] =
sources_out_[qi][ri];
699 for (
unsigned int ri=0; ri<n_bdr_reg; ri++)
701 sendbuffer[n_quant*2*n_blk_reg + qi*2*n_bdr_reg + + ri] =
fluxes_in_[qi][ri];
702 sendbuffer[n_quant*2*n_blk_reg + qi*2*n_bdr_reg + n_bdr_reg + ri] =
fluxes_out_[qi][ri];
717 for (
unsigned int qi=0; qi<n_quant; qi++)
719 for (
unsigned int ri=0; ri<n_blk_reg; ri++)
721 sources_in_[qi][ri] = recvbuffer[qi*2*n_blk_reg + + ri];
722 sources_out_[qi][ri] = recvbuffer[qi*2*n_blk_reg + n_blk_reg + ri];
724 for (
unsigned int ri=0; ri<n_bdr_reg; ri++)
726 fluxes_in_[qi][ri] = recvbuffer[n_quant*2*n_blk_reg + qi*2*n_bdr_reg + + ri];
727 fluxes_out_[qi][ri] = recvbuffer[n_quant*2*n_blk_reg + qi*2*n_bdr_reg + n_bdr_reg + ri];
751 for( RegionSet::const_iterator reg = b_set.begin(); reg != b_set.end(); ++reg)
753 for (
unsigned int qi=0; qi<n_quant; qi++)
763 for( RegionSet::const_iterator reg = bulk_set.begin(); reg != bulk_set.end(); ++reg)
765 for (
unsigned int qi=0; qi<n_quant; qi++)
781 for (
unsigned int qi=0; qi<n_quant; qi++)
786 for (
unsigned int qi=0; qi<n_quant; qi++)
826 if (
rank_ != 0)
return;
833 unsigned int wl = 2*(w-5)+7;
834 string bc_head_format =
"# %-*s%-*s%-*s%-*s%-*s%-*s\n",
835 bc_format =
"%*s%-*d%-*s%-*s%-*g%-*g%-*g\n",
836 bc_total_format =
"# %-*s%-*s%-*g%-*g%-*g\n";
838 output_ <<
"# " << setw((w*c+wl-14)/2) << setfill(
'-') <<
"--" 840 << setw((w*c+wl-14)/2) << setfill(
'-') <<
"" << endl
844 output_ <<
"# Mass flux through boundary [M/T]:\n# " 845 << setiosflags(ios::left) << setfill(
' ')
846 << setw(w) <<
"[boundary_id]" 847 << setw(wl) <<
"[label]" 848 << setw(w) <<
"[substance]" 849 << setw(w) <<
"[total flux]" 850 << setw(w) <<
"[outward flux]" 851 << setw(w) <<
"[inward flux]" 855 output_ <<
"# " << setw(w*c+wl) << setfill(
'-') <<
"" << setfill(
' ') << endl;
859 for( RegionSet::const_iterator reg = b_set.begin(); reg != b_set.end(); ++reg) {
860 for (
unsigned int qi=0; qi<n_quant; qi++) {
862 << setw(w) << (int)reg->id()
863 << setw(wl) << reg->label().c_str()
867 << setw(w) <<
fluxes_in_[qi][reg->boundary_idx()]
873 output_ <<
"# " << setw(w*c+wl) << setfill(
'-') <<
"" << setfill(
' ') << endl;
876 for (
unsigned int qi=0; qi<n_quant; qi++)
877 output_ <<
"# " << setiosflags(ios::left)
878 << setw(w+wl) <<
"Total mass flux of substance [M/T]" 888 string src_head_format =
"# %-*s%-*s%-*s%-*s%-*s\n",
889 src_format =
"%*s%-*d%-*s%-*s%-*g%-*g\n",
890 src_total_format =
"# %-*s%-*s%-*g%-*g\n";
891 output_ <<
"# Mass [M] and sources [M/T] on regions:\n" 892 <<
"# " << setiosflags(ios::left)
893 << setw(w) <<
"[region_id]" 894 << setw(wl) <<
"[label]" 895 << setw(w) <<
"[substance]" 896 << setw(w) <<
"[total_mass]" 897 << setw(w) <<
"[total_source]" 901 output_ <<
"# " << setw(w*c+wl) << setfill(
'-') <<
"" << setfill(
' ') << endl;
905 for( RegionSet::const_iterator reg = bulk_set.begin(); reg != bulk_set.end(); ++reg)
907 for (
unsigned int qi=0; qi<n_quant; qi++)
910 << setw(w) << (int)reg->id()
911 << setw(wl) << reg->label().c_str()
913 << setw(w) <<
masses_[qi][reg->bulk_idx()]
920 output_ <<
"# " << setw(w*c+wl) << setfill(
'-') <<
"" << setfill(
' ') << endl;
923 for (
unsigned int qi=0; qi<n_quant; qi++)
924 output_ <<
"# " << setiosflags(ios::left) << setw(w+wl) <<
"Total mass [M] and sources [M/T]" 933 output_ <<
"\n\n# Cumulative mass balance on time interval [" 935 << setiosflags(ios::left) << time <<
"]\n" 936 <<
"# Initial mass [M] + sources integrated over time [M] - flux integrated over time [M] = current mass [M]\n" 937 <<
"# " << setiosflags(ios::left)
938 << setw(w) <<
"[substance]" 939 << setw(w) <<
"[A=init. mass]" 940 << setw(w) <<
"[B=source]" 941 << setw(w) <<
"[C=flux]" 942 << setw(w) <<
"[A+B-C]" 943 << setw(w) <<
"[D=curr. mass]" 944 << setw(w) <<
"[A+B-C-D=err.]" 945 << setw(w) <<
"[rel. error]" 948 for (
unsigned int qi=0; qi<n_quant; qi++)
951 output_ <<
" " << setiosflags(ios::left)
970 std::stringstream ss;
971 for (
unsigned int i=0; i<cnt; i++) ss <<
format_csv_val(0, delimiter);
976 void Balance::output_csv(
double time,
char delimiter,
const std::string& comment_string,
unsigned int repeat)
979 if (
rank_ != 0)
return;
988 for( RegionSet::const_iterator reg = bulk_set.begin(); reg != bulk_set.end(); ++reg)
990 for (
unsigned int qi=0; qi<n_quant; qi++)
1010 for( RegionSet::const_iterator reg = b_set.begin(); reg != b_set.end(); ++reg)
1012 for (
unsigned int qi=0; qi<n_quant; qi++) {
1029 for (
unsigned int qi=0; qi<n_quant; qi++)
1059 std::stringstream ss;
1060 if (delimiter ==
' ') {
1066 output_ << comment_string << ss.str()
1086 std::stringstream ss;
1087 std::replace( val.begin(), val.end(),
'\"',
'\'');
1089 if (!initial) ss << delimiter;
1090 if (delimiter ==
' ') {
1091 std::stringstream sval;
1092 sval <<
"\"" << val <<
"\"";
1095 ss <<
"\"" << val <<
"\"";
1103 std::stringstream ss;
1105 if (!initial) ss << delimiter;
1106 if (delimiter ==
' ') {
1124 output_yaml_ <<
"column_names: [ flux, flux_in, flux_out, mass, source, source_in, source_out, flux_increment, " 1125 <<
"source_increment, flux_cumulative, source_cumulative, error ]" << endl;
1134 for( RegionSet::const_iterator reg = bulk_set.begin(); reg != bulk_set.end(); ++reg)
1136 for (
unsigned int qi=0; qi<n_quant; qi++)
1139 output_yaml_ << setw(4) <<
"" <<
"region: " << reg->label() << endl;
1141 output_yaml_ << setw(4) <<
"" <<
"data: " <<
"[ 0, 0, 0, " <<
masses_[qi][reg->bulk_idx()] <<
", " 1144 <<
", 0, 0, 0, 0, 0 ]" << endl;
1150 for( RegionSet::const_iterator reg = b_set.begin(); reg != b_set.end(); ++reg)
1152 for (
unsigned int qi=0; qi<n_quant; qi++) {
1154 output_yaml_ << setw(4) <<
"" <<
"region: " << reg->label() << endl;
1159 <<
", 0, 0, 0, 0, 0, 0, 0, 0, 0 ]" << endl;
1165 for (
unsigned int qi=0; qi<n_quant; qi++)
1169 output_yaml_ << setw(4) <<
"" <<
"region: ALL" << endl;
1177 << error <<
" ]" << endl;
UnitSI units_
Units of conserved quantities.
static const Input::Type::Record & get_input_type()
Main balance input record type.
std::vector< double > integrated_sources_
unsigned int add_quantity(const string &name)
std::vector< double > sum_fluxes_out_
void calculate_mass(unsigned int quantity_idx, const Vec &solution, vector< double > &output_array)
RegionSet get_region_set(const std::string &set_name) const
TimeMark::Type output_mark_type_
TimeMark type for output of particular equation.
void allocate(unsigned int n_loc_dofs, unsigned int max_dofs_per_boundary)
TimeMark::Type balance_output_type_
TimeMark type for balance output of particular equation.
unsigned int * boundary_idx_
std::string format_text() const
void finish_source_assembly(unsigned int quantity_idx)
This method must be called after assembling the matrix and vectors for computing source.
unsigned int bulk_size() const
void add_mass_vec_value(unsigned int quantity_idx, unsigned int region_idx, double value)
bool initial_
true before calculating the mass at initial time, otherwise false
std::vector< std::vector< double > > sources_in_
void add_cumulative_source(unsigned int quantity_idx, double source)
std::vector< std::vector< double > > fluxes_in_
std::vector< double > sum_fluxes_in_
std::vector< std::vector< double > > fluxes_out_
const TimeGovernor * time_
Mat * region_source_rhs_
Matrices for calculation of signed source (n_dofs x n_bulk_regions).
Input::Record input_record_
Record for current balance.
ofstream output_
Handle for file for output in given OutputFormat of balance and total fluxes over individual regions ...
void read_from_input(Input::Array in_array, const TimeGovernor &tg)
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
void output_legacy(double time)
Perform output in old format (for compatibility)
SideIter side(const unsigned int loc_index)
void add_mass_matrix_values(unsigned int quantity_idx, unsigned int region_idx, const std::vector< LongIdx > &dof_indices, const std::vector< double > &values)
const RegionDB & region_db() const
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
const TimeStep & step(int index=-1) const
bool allocation_done_
true before allocating necessary internal structures (Petsc matrices etc.)
void calculate_cumulative(unsigned int quantity_idx, const Vec &solution)
Basic time management functionality for unsteady (and steady) solvers (class Equation).
void add_flux_matrix_values(unsigned int quantity_idx, unsigned int boundary_idx, const std::vector< LongIdx > &dof_indices, const std::vector< double > &values)
static TimeMarks & marks()
#define MPI_Reduce(sendbuf, recvbuf, count, datatype, op, root, comm)
void calculate_instant(unsigned int quantity_idx, const Vec &solution)
void add_flux_vec_value(unsigned int quantity_idx, unsigned int boundary_idx, double value)
Basic time management class.
std::vector< double > sum_fluxes_
virtual ElementAccessor< 3 > element_accessor(unsigned int idx) const
Create and return ElementAccessor to element of given idx.
bool balance_on_
If the balance is on. Balance is off in the case of no balance output time marks. ...
static constexpr bool value
std::vector< double > increment_sources_
Vec * be_flux_vec_
Vectors for calculation of flux (n_boundary_edges).
static void set_yaml_output()
Set global variable to output balance files into YAML format (in addition to the table format)...
static const Input::Type::Selection & get_format_selection_input_type()
Input selection for file format.
std::vector< double > integrated_fluxes_
unsigned int boundary_idx() const
Returns index of the region in the boundary set.
std::vector< Quantity > quantities_
Names of conserved quantities.
std::string csv_zero_vals(unsigned int cnt, char delimiter)
Return part of output represented by zero values. Count of zero values is given by cnt parameter...
std::vector< std::vector< double > > sources_out_
Mat * be_flux_matrix_
Matrices for calculation of flux (n_boundary_edges x n_dofs).
double last_time_
time of last calculated balance
void open_stream(Stream &stream) const
std::vector< std::vector< double > > fluxes_
void finish_mass_assembly(unsigned int quantity_idx)
This method must be called after assembling the matrix for computing mass.
TimeMark::Type equation_fixed_mark_type() const
void format_csv_output_header(char delimiter, const std::string &comment_string)
Print output header.
TimeMark::Type equation_mark_type() const
unsigned int n_sides() const
void init_from_input(const Input::Record &in_rec, TimeGovernor &tg)
Mat * region_mass_matrix_
Matrices for calculation of mass (n_dofs x n_bulk_regions).
std::vector< double > sum_sources_out_
unsigned int max_dofs_per_boundary_
bool cumulative_
if true then cumulative balance is computed
Mat * region_source_matrix_
Matrices for calculation of source (n_dofs x n_bulk_regions).
std::vector< double > increment_fluxes_
void chkerr_assert(unsigned int ierr)
bool is_current()
Returns true if the current time step is marked for the balance output.
Balance(const std::string &file_prefix, const Mesh *mesh)
void output_csv(double time, char delimiter, const std::string &comment_string, unsigned int repeat=0)
Perform output in csv format.
Distribution * get_el_ds() const
static bool do_yaml_output_
std::vector< unsigned int > add_quantities(const std::vector< string > &names)
Dedicated class for storing path to input and output files.
Support classes for parallel programing.
void output_yaml(double time)
Perform output in yaml format.
void output()
Perform output to file for given time instant.
std::vector< double > sum_sources_
std::string get_unit_string() const
void start_flux_assembly(unsigned int quantity_idx)
std::vector< double > initial_mass_
static const unsigned int output_column_width
Size of column in output (used if delimiter is space)
void add_source_values(unsigned int quantity_idx, unsigned int region_idx, const std::vector< LongIdx > &loc_dof_indices, const std::vector< double > &mat_values, const std::vector< double > &vec_values)
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
unsigned int boundary_size() const
void finish_flux_assembly(unsigned int quantity_idx)
This method must be called after assembling the matrix and vector for computing flux.
std::vector< unsigned int > be_regions_
Number of boundary region for each local boundary edge.
OutputFormat output_format_
Format of output file.
static const Input::Type::Array get_input_type()
void units(const UnitSI &unit)
Setter for units of conserved quantities.
int be_offset_
Offset for local part of vector of boundary edges.
std::vector< std::vector< double > > masses_
void start_mass_assembly(unsigned int quantity_idx)
std::vector< double > sum_masses_
Vec * region_mass_vec_
Vectors for calculation of mass (n_bulk_regions).
unsigned int output_line_counter_
hold count of line printed into output_
bool add_output_times_
Add output time marks to balance output time marks.
Class for representation SI units of Fields.
unsigned int n_loc_dofs_
Allocation parameters. Set by the allocate method used in the lazy_initialize.
LongIdx * get_el_4_loc() const
void undef(bool val=true)
void start_source_assembly(unsigned int quantity_idx)
FilePath balance_output_file_
File path for output_ stream.
std::string file_prefix_
Save prefix passed in in constructor.
bool output_yaml_header_
marks whether YAML output has printed header
std::string format_csv_val(std::string val, char delimiter, bool initial=false)
Format string value of csv output. Wrap string into quotes and if delimiter is space, align text to column.
std::vector< double > sum_sources_in_
unsigned int lsize(int proc) const
get local size