16 #include "calculation_continuity.h"
17 #include "mesh_reader_h2d_xml.h"
19 #include "space_hdiv.h"
20 #include "space_hcurl.h"
28 CalculationContinuityException::CalculationContinuityException() : Exception()
32 CalculationContinuityException::CalculationContinuityException(exceptionEntityType
type,
const char * reason) : Exception()
34 this->init(type, reason);
37 void CalculationContinuityException::init(exceptionEntityType
type,
const char * reason)
39 char * msg =
new char[34 + strlen(reason)];
40 char * typeMsg =
new char[15];
44 sprintf(typeMsg,
"meshes");
47 sprintf(typeMsg,
"spaces");
50 sprintf(typeMsg,
"solutions");
53 sprintf(typeMsg,
"time steps");
56 sprintf(typeMsg,
"error info");
59 sprintf(typeMsg,
"general");
63 sprintf(msg,
"Exception in CalculationContinuity (%s): \"%s\"", typeMsg, reason);
68 IOCalculationContinuityException::IOCalculationContinuityException(exceptionEntityType type, inputOutput inputOutput,
const char * filename) : CalculationContinuityException()
70 char * msg =
new char[34 + strlen(filename)];
71 char * typeMsg =
new char[7];
75 sprintf(typeMsg,
"input");
78 sprintf(typeMsg,
"output");
81 sprintf(msg,
"I/O exception: %s, filename: \"%s\"", typeMsg, filename);
82 this->init(type, msg);
85 IOCalculationContinuityException::IOCalculationContinuityException(exceptionEntityType type, inputOutput inputOutput,
const char * filename,
const char * reason) : CalculationContinuityException()
87 char * msg =
new char[34 + strlen(filename)];
88 char * typeMsg =
new char[7];
92 sprintf(typeMsg,
"input");
95 sprintf(typeMsg,
"output");
98 sprintf(msg,
"I/O exception: %s, filename: \"%s\", reason: %s", typeMsg, filename, reason);
99 this->init(type, msg);
102 template<
typename Scalar>
103 CalculationContinuity<Scalar>::CalculationContinuity(IdentificationMethod identification_method) : last_record(NULL), record_available(false), identification_method(identification_method), num(0)
106 unsigned int last_number;
107 std::stringstream ss;
108 switch(identification_method)
111 ss <<
"timeAndNumber.h2d";
114 ss <<
"onlyTime.h2d";
117 ss <<
"onlyNumber.h2d";
121 std::ifstream ifile(ss.str().c_str());
126 switch(identification_method)
129 ifile >> this->num >> last_time >> last_number;
130 record_available =
true;
133 ifile >> this->num >> last_time;
134 record_available =
true;
137 ifile >> this->num >> last_number;
138 record_available =
true;
144 CalculationContinuity<Scalar>::Record* record;
145 switch(identification_method)
148 record =
new CalculationContinuity<Scalar>::Record(last_time, last_number);
149 this->records.insert(std::pair<std::pair<double, unsigned int>, CalculationContinuity<Scalar>::Record*>(std::pair<double, unsigned int>(last_time, last_number), record));
150 this->last_record = record;
153 record =
new CalculationContinuity<Scalar>::Record(last_time);
154 this->time_records.insert(std::pair<
double, CalculationContinuity<Scalar>::Record*>(last_time, record));
155 this->last_record = record;
158 record =
new CalculationContinuity<Scalar>::Record(last_number);
159 this->numbered_records.insert(std::pair<
unsigned int, CalculationContinuity<Scalar>::Record*>(last_number, record));
160 this->last_record = record;
166 template<
typename Scalar>
169 std::ofstream ofile(
"timeAndNumber.h2d", std::ios_base::app);
172 ofile << ++this->num <<
' ' << time <<
' ' << number << std::endl;
186 if(time_step_n_minus_one > 0.0)
187 record->save_time_step_length_n_minus_one(time_step_n_minus_one);
192 this->last_record = record;
195 template<
typename Scalar>
198 std::ofstream ofile(
"timeAndNumber.h2d", std::ios_base::app);
201 ofile << ++this->num <<
' ' << time <<
' ' << number << std::endl;
205 throw IOCalculationContinuityException(CalculationContinuityException::general, IOCalculationContinuityException::output,
"timeAndNumber.h2d");
207 CalculationContinuity<Scalar>::Record* record =
new CalculationContinuity<Scalar>::Record(time, number);
208 record->save_meshes(meshes);
209 if(spaces != Hermes::vector<Space<Scalar>*>())
210 record->save_spaces(spaces);
211 if(slns != Hermes::vector<Solution<Scalar>*>())
212 record->save_solutions(slns);
214 record->save_time_step_length(time_step);
215 if(time_step_n_minus_one > 0.0)
216 record->save_time_step_length_n_minus_one(time_step_n_minus_one);
218 record->save_error(error);
220 this->records.insert(std::pair<std::pair<double, unsigned int>, CalculationContinuity<Scalar>::Record*>(std::pair<double, unsigned int>(time, number), record));
221 this->last_record = record;
224 template<
typename Scalar>
227 std::ofstream ofile(
"onlyTime.h2d", std::ios_base::app);
230 ofile << ++this->num <<
' ' << time << std::endl;
244 if(time_step_n_minus_one > 0.0)
245 record->save_time_step_length_n_minus_one(time_step_n_minus_one);
250 this->last_record = record;
253 template<
typename Scalar>
256 std::ofstream ofile(
"onlyTime.h2d", std::ios_base::app);
259 ofile << ++this->num <<
' ' << time << std::endl;
263 throw IOCalculationContinuityException(CalculationContinuityException::general, IOCalculationContinuityException::output,
"onlyTime.h2d");
264 CalculationContinuity<Scalar>::Record* record =
new CalculationContinuity<Scalar>::Record(time);
265 record->save_meshes(meshes);
266 if(spaces != Hermes::vector<Space<Scalar>*>())
267 record->save_spaces(spaces);
268 if(slns != Hermes::vector<Solution<Scalar>*>())
269 record->save_solutions(slns);
271 record->save_time_step_length(time_step);
272 if(time_step_n_minus_one > 0.0)
273 record->save_time_step_length_n_minus_one(time_step_n_minus_one);
275 record->save_error(error);
276 this->time_records.insert(std::pair<
double, CalculationContinuity<Scalar>::Record*>(time, record));
277 this->last_record = record;
280 template<
typename Scalar>
283 std::ofstream ofile(
"onlyNumber.h2d", std::ios_base::app);
286 ofile << ++this->num <<
' ' << number << std::endl;
300 if(time_step_n_minus_one > 0.0)
301 record->save_time_step_length_n_minus_one(time_step_n_minus_one);
306 this->last_record = record;
309 template<
typename Scalar>
312 std::ofstream ofile(
"onlyNumber.h2d", std::ios_base::app);
315 ofile << ++this->num <<
' ' << number << std::endl;
319 throw IOCalculationContinuityException(CalculationContinuityException::general, IOCalculationContinuityException::output,
"onlyNumber.h2d");
321 CalculationContinuity<Scalar>::Record* record =
new CalculationContinuity<Scalar>::Record(number);
322 record->save_meshes(meshes);
323 if(spaces != Hermes::vector<Space<Scalar>*>())
324 record->save_spaces(spaces);
325 if(slns != Hermes::vector<Solution<Scalar>*>())
326 record->save_solutions(slns);
328 record->save_time_step_length(time_step);
329 if(time_step_n_minus_one > 0.0)
330 record->save_time_step_length_n_minus_one(time_step_n_minus_one);
332 record->save_error(error);
333 this->numbered_records.insert(std::pair<
unsigned int, CalculationContinuity<Scalar>::Record*>(number, record));
334 this->last_record = record;
337 template<
typename Scalar>
342 template<
typename Scalar>
347 template<
typename Scalar>
352 template<
typename Scalar>
355 return this->record_available;
358 template<
typename Scalar>
361 if(this->last_record != NULL)
362 return this->last_record;
367 template<
typename Scalar>
373 template<
typename Scalar>
377 for(
unsigned int i = 0; i < meshes.size(); i++)
379 std::stringstream filename;
380 filename << CalculationContinuity<Scalar>::mesh_file_name << i <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
383 reader.
save(filename.str().c_str(), meshes[i]);
391 template<
typename Scalar>
395 std::stringstream filename;
396 filename << CalculationContinuity<Scalar>::mesh_file_name << 0 <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
399 reader.
save(filename.str().c_str(), mesh);
407 template<
typename Scalar>
410 for(
unsigned int i = 0; i < spaces.size(); i++)
412 std::stringstream filename;
413 filename << CalculationContinuity<Scalar>::space_file_name << i <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
416 spaces[i]->save(filename.str().c_str());
425 template<
typename Scalar>
428 std::stringstream filename;
429 filename << CalculationContinuity<Scalar>::space_file_name << 0 <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
432 space->
save(filename.str().c_str());
440 template<
typename Scalar>
443 for(
unsigned int i = 0; i < solutions.size(); i++)
445 std::stringstream filename;
446 filename << CalculationContinuity<Scalar>::solution_file_name << i <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
450 solutions[i]->save(filename.str().c_str());
452 catch(Hermes::Exceptions::SolutionSaveFailureException& e)
454 throw IOCalculationContinuityException(CalculationContinuityException::solutions, IOCalculationContinuityException::output, filename.str().c_str(), e.what());
458 template<
typename Scalar>
461 std::stringstream filename;
462 filename << CalculationContinuity<Scalar>::solution_file_name << 0 <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
465 solution->
save(filename.str().c_str());
467 catch(Hermes::Exceptions::SolutionSaveFailureException& e)
469 throw IOCalculationContinuityException(CalculationContinuityException::solutions, IOCalculationContinuityException::output, filename.str().c_str(), e.what());
473 template<
typename Scalar>
476 std::stringstream filename;
477 filename << CalculationContinuity<Scalar>::time_step_file_name <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
480 std::ofstream out(filename.str().c_str());
481 out << time_step_length_to_save;
486 throw IOCalculationContinuityException(CalculationContinuityException::time_steps, IOCalculationContinuityException::output, filename.str().c_str(), e.what());
490 template<
typename Scalar>
493 std::stringstream filename;
494 filename << CalculationContinuity<Scalar>::time_stepNMinusOne_file_name <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
497 std::ofstream out(filename.str().c_str());
498 out << time_step_length_to_save;
503 throw IOCalculationContinuityException(CalculationContinuityException::time_steps, IOCalculationContinuityException::output, filename.str().c_str(), e.what());
507 template<
typename Scalar>
510 std::stringstream filename;
511 filename << CalculationContinuity<Scalar>::error_file_name <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
514 std::ofstream out(filename.str().c_str());
524 template<
typename Scalar>
528 for(
unsigned int i = 0; i < meshes.size(); i++)
530 std::stringstream filename;
531 filename << CalculationContinuity<Scalar>::mesh_file_name << i <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
534 reader.
load(filename.str().c_str(), meshes[i]);
536 catch(Hermes::Exceptions::MeshLoadFailureException& e)
542 template<
typename Scalar>
546 std::stringstream filename;
547 filename << CalculationContinuity<Scalar>::mesh_file_name << 0 <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
551 reader.
load(filename.str().c_str(), mesh);
553 catch(Hermes::Exceptions::MeshLoadFailureException& e)
559 template<
typename Scalar>
562 Hermes::vector<Space<Scalar>*> spaces;
564 if(shapesets == Hermes::vector<Shapeset*>())
565 for(
unsigned int i = 0; i < meshes.size(); i++)
566 shapesets.push_back(NULL);
568 for(
unsigned int i = 0; i < meshes.size(); i++)
570 std::stringstream filename;
571 filename << CalculationContinuity<Scalar>::space_file_name << i <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
575 spaces.push_back(
Space<Scalar>::load(filename.str().c_str(), meshes[i],
false, essential_bcs[i], shapesets[i]));
577 catch(Hermes::Exceptions::SpaceLoadFailureException& e)
588 template<
typename Scalar>
591 Hermes::vector<Space<Scalar>*> spaces;
593 if(shapesets == Hermes::vector<Shapeset*>())
594 for(
unsigned int i = 0; i < meshes.size(); i++)
595 shapesets.push_back(NULL);
597 for(
unsigned int i = 0; i < meshes.size(); i++)
599 std::stringstream filename;
600 filename << CalculationContinuity<Scalar>::space_file_name << i <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
604 spaces.push_back(
Space<Scalar>::load(filename.str().c_str(), meshes[i],
false, NULL, shapesets[i]));
606 catch(Hermes::Exceptions::SpaceLoadFailureException& e)
617 template<
typename Scalar>
620 std::stringstream filename;
621 filename << CalculationContinuity<Scalar>::space_file_name << 0 <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
627 catch(Hermes::Exceptions::SpaceLoadFailureException& e)
637 template<
typename Scalar>
640 if(solutions.size() != spaces.size())
641 throw Exceptions::LengthException(1, 2, solutions.size(), spaces.size());
642 for(
unsigned int i = 0; i < solutions.size(); i++)
644 std::stringstream filename;
645 filename << CalculationContinuity<Scalar>::solution_file_name << i <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
648 solutions[i]->load(filename.str().c_str(), spaces[i]);
649 solutions[i]->space_type = spaces[i]->get_type();
651 catch(Hermes::Exceptions::SolutionLoadFailureException& e)
653 throw IOCalculationContinuityException(CalculationContinuityException::solutions, IOCalculationContinuityException::input, filename.str().c_str(), e.what());
657 throw IOCalculationContinuityException(CalculationContinuityException::solutions, IOCalculationContinuityException::input, filename.str().c_str(), e.what());
661 template<
typename Scalar>
664 std::stringstream filename;
665 filename << CalculationContinuity<Scalar>::solution_file_name << 0 <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
668 solution->
load(filename.str().c_str(), space);
669 solution->space_type = space->
get_type();
671 catch(Hermes::Exceptions::SolutionLoadFailureException& e)
673 throw IOCalculationContinuityException(CalculationContinuityException::solutions, IOCalculationContinuityException::input, filename.str().c_str(), e.what());
677 throw IOCalculationContinuityException(CalculationContinuityException::solutions, IOCalculationContinuityException::input, filename.str().c_str(), e.what());
681 template<
typename Scalar>
684 std::stringstream filename;
685 filename << CalculationContinuity<Scalar>::time_step_file_name <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
688 std::ifstream in(filename.str().c_str());
689 in >> time_step_length;
694 throw IOCalculationContinuityException(CalculationContinuityException::time_steps, IOCalculationContinuityException::input, filename.str().c_str(), e.what());
698 template<
typename Scalar>
701 std::stringstream filename;
702 filename << CalculationContinuity<Scalar>::time_stepNMinusOne_file_name <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
705 std::ifstream in(filename.str().c_str());
706 in >> time_step_length;
711 throw IOCalculationContinuityException(CalculationContinuityException::time_steps, IOCalculationContinuityException::input, filename.str().c_str(), e.what());
715 template<
typename Scalar>
718 std::stringstream filename;
719 filename << CalculationContinuity<Scalar>::error_file_name <<
'_' << (
std::string)
"t = " << this->time << (
std::string)
"n = " << this->number << (
std::string)
".h2d";
722 std::ifstream in(filename.str().c_str());
732 template<
typename Scalar>
738 template<
typename Scalar>
744 template<
typename Scalar>
747 template<
typename Scalar>
750 template<
typename Scalar>
753 template<
typename Scalar>
755 template<
typename Scalar>
758 template<
typename Scalar>
761 template<
typename Scalar>
764 mesh_file_name = mesh_file_nameToSet;
766 template<
typename Scalar>
769 space_file_name = space_file_nameToSet;
771 template<
typename Scalar>
772 void CalculationContinuity<Scalar>::set_solution_file_name(
std::string solution_file_nameToSet)
774 solution_file_name = solution_file_nameToSet;
776 template<
typename Scalar>
777 void CalculationContinuity<Scalar>::set_time_step_file_name(
std::string time_step_file_nameToSet)
779 time_step_file_name = time_step_file_nameToSet;
781 template<
typename Scalar>
782 void CalculationContinuity<Scalar>::set_error_file_name(
std::string error_file_nameToSet)
784 error_file_name = error_file_nameToSet;
787 template class HERMES_API CalculationContinuity<double>;
788 template class HERMES_API CalculationContinuity<std::complex<double> >;