Skip to content
Snippets Groups Projects
Commit 49c021fe authored by Wuttke, Joachim's avatar Wuttke, Joachim Committed by Wuttke, Joachim
Browse files

Proper error msg from Python/Std/Check

parent 25f1feb8
No related branches found
No related tags found
No related merge requests found
......@@ -33,6 +33,9 @@ std::vector<std::vector<double>> FT2DArray(const std::vector<std::vector<double>
//! Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
double DataUtils::relativeDataDifference(const OutputData<double>& dat,
const OutputData<double>& ref) {
std::cerr << "DEBUG rDD" << std::endl;
std::cerr << "DEBUG rDD ref rank=" << ref.rank() << ", size=" << ref.getAllocatedSize() << std::endl;
std::cerr << "DEBUG rDD dat rank=" << dat.rank() << ", size=" << dat.getAllocatedSize() << std::endl;
if (!dat.hasSameDimensions(ref))
throw std::runtime_error("OutputData dimension differs from reference");
......@@ -48,15 +51,17 @@ double DataUtils::relativeDataDifference(const OutputData<double>& dat,
//! Returns true is relative difference is below threshold; prints informative output
bool DataUtils::checkRelativeDifference(const OutputData<double>& dat,
const OutputData<double>& ref, const double threshold) {
std::cerr << "DEBUG cRD" << std::endl;
const double diff = relativeDataDifference(dat, ref);
std::cerr << "DEBUG cRD -> " << diff << std::endl;
if (diff > threshold) {
std::cerr << "FAILED: relative deviation of dat from ref is " << diff
<< ", above given threshold " << threshold << "\n";
<< ", above given threshold " << threshold << std::endl;
return false;
}
if (diff)
std::cerr << "- OK: relative deviation of dat from ref is " << diff
<< ", within given threshold " << threshold << "\n";
<< ", within given threshold " << threshold << std::endl;
else
std::cout << "- OK: dat = ref\n";
return true;
......
......@@ -40,11 +40,10 @@ OutputData<double>* IntensityDataIOFactory::readOutputData(const std::string& fi
return readOutputData(
file_name, [](std::istream& s) { return OutputDataReadWriteINT().readOutputData(s); });
#ifdef BORNAGAIN_TIFF_SUPPORT
else if (DataFormatUtils::isTiffFile(file_name))
if (DataFormatUtils::isTiffFile(file_name))
return readOutputData(
file_name, [](std::istream& s) { return OutputDataReadWriteTiff().readOutputData(s); });
#endif // BORNAGAIN_TIFF_SUPPORT
#endif
// Try to read ASCII by default. Binary maps to ASCII.
// If the file is not actually a matrix of numbers,
// the error will be thrown during the reading.
......@@ -74,7 +73,7 @@ void IntensityDataIOFactory::writeOutputData(const OutputData<double>& data,
writeOutputData(file_name, [&](std::ostream& s) {
OutputDataReadWriteTiff().writeOutputData(data, s);
});
#endif // BORNAGAIN_TIFF_SUPPORT
#endif
else
writeOutputData(file_name, [&](std::ostream& s) {
OutputDataReadWriteNumpyTXT().writeOutputData(data, s);
......
......@@ -35,9 +35,10 @@ std::unique_ptr<OutputData<double>> domainData(const std::string& test_name,
// Generate Python script
const std::string pyscript_filename =
FileSystemUtils::jointPath(BATesting::TestOutDir_PyStd(), test_name + ".py");
std::ofstream pythonFile(pyscript_filename);
pythonFile << ExportToPython::generateSimulationCode(direct_simulation);
pythonFile.close();
std::ofstream f(pyscript_filename);
f << ExportToPython::generateSimulationCode(direct_simulation);
f.close();
std::cout << "- wrote Python script " << pyscript_filename << std::endl;
// Run Python script
const std::string py_command = pyscript_filename + " " + output_path;
......@@ -51,14 +52,16 @@ std::unique_ptr<OutputData<double>> domainData(const std::string& test_name,
+ BABuild::pythonExecutable() + "\" -B " + py_command;
#endif
std::cout << "- system call: " << sys_command << std::endl;
int ret = std::system(sys_command.c_str());
if (ret != 0) {
std::stringstream msg;
msg << "System call returned non-zero value " << ret;
throw std::runtime_error(msg.str());
}
int err = std::system(sys_command.c_str());
std::cout << "- system call returned " << err << std::endl;
if (err)
throw std::runtime_error("Exported Python script did not execute properly");
return std::unique_ptr<OutputData<double>>(IntensityDataIOFactory::readOutputData(output_path));
auto ret = std::unique_ptr<OutputData<double>>(
IntensityDataIOFactory::readOutputData(output_path));
if (!ret)
throw std::runtime_error("Could not read back simulation output from file " + output_path);
return ret;
}
} // namespace
......@@ -70,7 +73,9 @@ bool checkSimulation(const std::string& name, const ISimulation& direct_simulati
std::cout << "PyStd test: checkSimulation(" << name << ")" << std::endl;
const std::unique_ptr<OutputData<double>> domain_data = domainData(name, direct_simulation);
std::cout << "- got domain data" << std::endl;
const std::unique_ptr<OutputData<double>> ref_data = direct_simulation.result().data();
std::cout << "- ran simulation" << std::endl;
return DataUtils::checkRelativeDifference(*domain_data, *ref_data, limit);
}
......@@ -204,9 +204,7 @@ def plot_simulation_result(result, **kwargs):
:param units: units for plot axes
:param noshow: don't plot to interactive device
"""
ns = "NOSHOW" in os.environ
noshow = kwargs.pop('noshow', ns)
print(f'DEBUG ns={ns}, noshow={noshow}, kwargs={kwargs}')
noshow = kwargs.pop('noshow', "NOSHOW" in os.environ)
if len(result.array().shape) == 1: # 1D data, specular simulation assumed
plot_specular_simulation_result(result, **kwargs)
......@@ -215,6 +213,8 @@ def plot_simulation_result(result, **kwargs):
plt.tight_layout()
if not (noshow):
plt.show()
else:
print("plot_simulation_result: noshow")
def run_and_plot(simulation, **kwargs):
simulation.runSimulation()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment