Flow123d  jenkins-Flow123d-linux-release-multijob-198
old_bcd.cc
Go to the documentation of this file.
1 /*
2  * old_bcd.cc
3  *
4  * Created on: Jan 28, 2013
5  * Author: jb
6  */
7 
8 
9 #include "flow/old_bcd.hh"
11 #include "mesh/region.hh"
12 
13 #include "system/tokenizer.hh"
14 #include "boost/lexical_cast.hpp"
15 
17 : flow_type_factory(
18  std::make_shared<FieldFactory<3, FieldValue<3>::Enum> >(&(this->flow_type)) ),
19  flow_pressure_factory(
20  std::make_shared<FieldFactory<3, FieldValue<3>::Scalar> >(&(this->flow_pressure)) ),
21  flow_flux_factory(
22  std::make_shared<FieldFactory<3, FieldValue<3>::Scalar> >(&(this->flow_flux)) ),
23  flow_sigma_factory(
24  std::make_shared<FieldFactory<3, FieldValue<3>::Scalar> >(&(this->flow_sigma)) ),
25  trans_conc_factory(
26  std::make_shared<FieldFactory<3, FieldValue<3>::Vector> >(&(this->trans_conc)) ),
27  mesh_(NULL)
28 {}
29 
31  static OldBcdInput *obcd = new OldBcdInput;
32  return obcd;
33 }
34 
35 #define DIRICHLET 1
36 #define NEUMANN 2
37 #define NEWTON 3
38 
39 void OldBcdInput::read_flow(const Mesh &mesh, const FilePath &flow_bcd)
40 {
41  using namespace boost;
42 
43  vector< unsigned int *> old_to_new_side_numbering;
44 
45  // in the file the sides are numbered according to opposite nodes as they appear in the MSH file
46  unsigned int sides_0 [1] = {0};
47  old_to_new_side_numbering.push_back( sides_0 );
48  unsigned int sides_1 [2] = {0,1};
49  old_to_new_side_numbering.push_back( sides_1 );
50  unsigned int sides_2 [3] = {0,1,2}; //{0,2,1};
51  old_to_new_side_numbering.push_back( sides_2 );
52  unsigned int sides_3 [4] = {0,1,2,3}; //{3,2,1,0};
53  old_to_new_side_numbering.push_back( sides_3 );
54 
55  // check that all fields has same mesh, reuse it for reader
56  mesh_=&mesh;
57  ASSERT(mesh_ , "Null mesh pointer.\n");
58 
59  /*
60  * - read one flow file, fill fields, make ID list
61  * - read second file, check IDs agains ID list, fill fields
62  */
63 
64  flow_type = std::make_shared< FieldEnum >(1);
65  flow_type->set_mesh(mesh_, true);
66  flow_pressure = std::make_shared< FieldScalar >(1);
67  flow_pressure->set_mesh(mesh_, true);
68  flow_flux = std::make_shared< FieldScalar >(1);
69  flow_flux->set_mesh(mesh_, true);
70  flow_sigma = std::make_shared< FieldScalar >(1);
71  flow_sigma->set_mesh(mesh_, true);
72 
73  Tokenizer tok(flow_bcd);
74  try {
75  double scalar, flux, sigma;
76  unsigned int id;
77 
78  xprintf(MsgLog, "Reading old BCD file for flow: %s ...", tok.f_name().c_str());
79  tok.skip_to("$BoundaryConditions");
80  tok.next_line(false);
81  unsigned int n_boundaries = lexical_cast<unsigned int>(*tok); ++tok;
82 
83  for(unsigned int i_bcd=0; i_bcd < n_boundaries; i_bcd++) {
84  tok.next_line();
85 
86  id = lexical_cast<unsigned int>(*tok); ++tok;
87 
88  unsigned int type = lexical_cast<unsigned int>(*tok); ++tok;
89 
90  switch( type ) {
91  case DIRICHLET:
92  scalar = lexical_cast<double>(*tok); ++tok;
93  flux = 0.0;
94  sigma = 0.0;
95  break;
96  case NEUMANN:
97  flux = lexical_cast<double>(*tok); ++tok;
98  sigma = 0.0;
99  scalar = 0.0;
100  break;
101  case NEWTON:
102  scalar = lexical_cast<double>(*tok); ++tok;
103  sigma = lexical_cast<double>(*tok); ++tok;
104  flux = 0.0;
105  break;
106  default :
107  xprintf(UsrErr,"Unknown type of boundary condition - cond # %d, type %c\n", id, type );
108  break;
109  }
110 
111  unsigned int where = lexical_cast<unsigned int>(*tok); ++tok;
112 
113  unsigned int eid, sid, bc_ele_idx, our_sid;
114  Element * ele;
115  Boundary * bcd;
116 
117  switch( where ) {
118  case 2: // SIDE_EL - BC given by element and its local side number
119  eid = lexical_cast<unsigned int>(*tok); ++tok;
120  sid = lexical_cast<unsigned int>(*tok); ++tok;
121 
122  // find and set the side
123  // const cast can be removed when get rid of FullIterators and whole sys_vector stuff
124  // and have correct constantness for mesh classes
125  ele = const_cast<Element *>(mesh_->element.find_id( eid ));
126  if( sid < 0 || sid >= ele->n_sides() )
127  xprintf(UsrErr,"Boundary %d has incorrect reference to side %d\n", id, sid );
128  our_sid=old_to_new_side_numbering[ele->dim()][sid];
129  bcd = ele->side(our_sid) -> cond();
130  if (bcd) {
131  bc_ele_idx = mesh_->bc_elements.index( ele->side(our_sid) -> cond()->element() );
132  id_2_bcd_[id]= bc_ele_idx;
133  if ( ! some_bc_region_.is_valid() ) some_bc_region_ = ele->side(our_sid) -> cond()->element()->region();
134 
135  flow_type->set_data_row( bc_ele_idx, type);
136  flow_pressure->set_data_row(bc_ele_idx, scalar);
137  flow_flux->set_data_row( bc_ele_idx, flux);
138  flow_sigma->set_data_row( bc_ele_idx, sigma);
139  } else {
140  xprintf(Warn, "IGNORING boundary condition %d for non-boundary side %d of element ID: %d\n", id, sid, eid);
141  }
142  break;
143  case 3: // SIDE_E - BC given only by element, apply to all its sides
144 
145  xprintf(UsrErr, "Element only BCD are not supported.\n");
146  break;
147  default:
148  xprintf(UsrErr,"Unknown entity for boundary condition - cond # %d, ent. %c\n", id, where );
149  break;
150  }
151  unsigned int n_tags = lexical_cast<unsigned int>(*tok); ++tok;
152  while (n_tags>0) ++tok, --n_tags; // skip remaining tags
153 
154  }
155  xprintf(MsgLog, "DONE\n");
156  } catch (bad_lexical_cast &) {
157  xprintf(UsrErr, "Wrong format of number, %s.\n", tok.position_msg().c_str());
158  } // flow bcd reader
159 }
160 
161 
162 
163 void OldBcdInput::read_transport(unsigned int n_substances, const FilePath &transport_bcd)
164 {
165  using namespace boost;
166 
167  ASSERT(mesh_ , "Null mesh pointer.\n");
168  trans_conc = std::make_shared< FieldVector >( n_substances );
169  trans_conc->set_mesh(mesh_, true);
170 
171  FieldValue<3>::Vector::return_type ele_value(n_substances);
172 
173  Tokenizer tok(transport_bcd);
174  try {
175  unsigned int bcd_id, boundary_id, bc_ele_idx;
176 
177  xprintf(MsgLog, "Reading old BCD file for transport: %s ...", tok.f_name().c_str());
178  if (tok.skip_to("$Transport_BCDFormat")) tok.next_line(false);
179  tok.skip_to("$Transport_BCD");
180  tok.next_line(false);
181  unsigned int n_bcd = lexical_cast<unsigned int>(*tok); ++tok;
182  for (unsigned int i_bcd = 0; i_bcd < n_bcd; i_bcd++) {
183  tok.next_line();
184  bcd_id = lexical_cast<unsigned int>(*tok); ++tok;
185  boundary_id = lexical_cast<unsigned int>(*tok); ++tok;
186 
188  if (it == id_2_bcd_.end())
189  xprintf(UsrErr,"Wrong boundary index %d for bcd id %d in transport bcd file!", boundary_id, bcd_id);
190  bc_ele_idx = it->second;
191 
192  for (unsigned int sbi = 0; sbi < n_substances; sbi++) {
193  ele_value[sbi] = lexical_cast<double>(*tok); ++tok;
194  }
195 
196  trans_conc->set_data_row(bc_ele_idx, ele_value);
197 
198  }
199 
200  xprintf(MsgLog, "DONE\n");
201 
202  } catch (bad_lexical_cast &) {
203  xprintf(UsrErr, "Wrong format of number, %s.\n", tok.position_msg().c_str());
204  } // flow bcd reader
205 }
shared_ptr< FieldScalar > flow_sigma
Definition: old_bcd.hh:69
const Mesh * mesh_
Definition: old_bcd.hh:145
#define DIRICHLET
Definition: old_bcd.cc:35
shared_ptr< FieldEnum > flow_type
Definition: old_bcd.hh:66
Definition: mesh.h:109
#define NEUMANN
Definition: old_bcd.cc:36
internal::ReturnType< NRows, NCols, ET >::return_type return_type
FullIter find_id(const int id)
Definition: sys_vector.hh:444
Definition: system.hh:72
Old BC setting system for backward compatibility.
Definition: old_bcd.hh:47
unsigned int dim() const
#define ASSERT(...)
Definition: global_defs.h:121
Definition: system.hh:72
shared_ptr< FieldVector > trans_conc
Definition: old_bcd.hh:70
shared_ptr< FieldScalar > flow_flux
Definition: old_bcd.hh:68
unsigned int n_sides() const
#define xprintf(...)
Definition: system.hh:100
#define NEWTON
Definition: old_bcd.cc:37
OldBcdInput()
Definition: old_bcd.cc:16
unsigned int index(const T *pointer) const
Definition: sys_vector.hh:383
ElementVector bc_elements
Definition: mesh.h:213
SideIter side(const unsigned int loc_index)
shared_ptr< FieldScalar > flow_pressure
Definition: old_bcd.hh:67
map< unsigned int, unsigned int > id_2_bcd_
Maps ID to index of corresponding BC element.
Definition: old_bcd.hh:140
Dedicated class for storing path to input and output files.
Definition: file_path.hh:32
void read_flow(const Mesh &mesh, const FilePath &flow_bcd)
Definition: old_bcd.cc:39
Definition: system.hh:72
Region some_bc_region_
Definition: old_bcd.hh:146
static OldBcdInput * instance()
Definition: old_bcd.cc:30
ElementFullIter element() const
Definition: side_impl.hh:41
void read_transport(unsigned int n_substances, const FilePath &transport_bcd)
Definition: old_bcd.cc:163
bool is_valid() const
Returns false if the region has undefined/invalid value.
Definition: region.hh:67
ElementVector element
Vector of elements of the mesh.
Definition: mesh.h:205