Alamo
Debug.cpp
Go to the documentation of this file.
1#include "Util/Debug.H"
2#include "IO/ParmParse.H"
3#include "Util/MPI.H"
4#include "IO/FileNameParse.H"
5#include "Util/Util.H"
6
7#include <filesystem>
8
9
10// This code is not included in coverage count since it is for debugging purposes only
11// LCOV_EXCL_START
12
13namespace Util
14{
15namespace Debug
16{
17
18bool Compare_on = false;
19std::map<std::string,int> Compare_map;
21
23 std::string file, std::string func, int line,
24 const amrex::MultiFab &a_mf, std::string desc,
25 amrex::Box domain, int /*ngrow*/)
26{
28 // Switch to enableor disable debug comparisons.
29 pp.query_default("util.debug.compare",Compare_on,false);
30 if (!Compare_on) return;
31
33
34 Set::Scalar tolerance = 1E-10;
35
36 std::ostringstream messagestream;
37 std::string filesan = file;
38 Util::String::ReplaceAll(filesan,"/","_");
39 messagestream << filesan << "_" << func << "_" << line << desc;
40 std::string key = messagestream.str();
41
42 int cntr = 0;
43 if (Compare_map.find(key) == Compare_map.end())
44 Compare_map[key] = 0;
45 else
46 Compare_map[key] ++;
47 cntr = Compare_map[key];
48
49 messagestream << "_" << cntr;
50
51 std::string name = messagestream.str();
52 std::vector<std::string> allnames;
53 Util::MPI::Allgather(name,allnames);
54 for (auto & othername : allnames)
55 {
56 if (name != othername)
57 Util::ParallelAbort(file,func,line, "MPI Paths have diverged: I am trying to read ",
58 name, " but some other process is trying to read ",
59 othername);
60 }
61
62 amrex::BoxArray ba = a_mf.boxArray();
63 amrex::DistributionMapping dm = a_mf.DistributionMap();
64 int nghost = 2;
65 int ncomp = a_mf.nComp();
66 if (domain != amrex::Box::TheUnitBox())
67 {
68 for (auto &b : ba.boxList()) b = b.grow(2) & domain;
69 nghost = 0;
70 }
71 amrex::MultiFab mf(ba,dm,ncomp,nghost);
72 amrex::MultiFab::Copy(mf,a_mf,0,0,ncomp,0);
73 if (amrex::ParallelDescriptor::NProcs() == 1)
74 {
75 Util::ParallelMessage(file,func,line,"[",Compare_cnt,"] ","Storing ",name);
76 Util::ParallelMessage(file,func,line,"[",Compare_cnt,"] ",name, " - ncomp ",ncomp);
77 std::filesystem::create_directory("checks");
78 amrex::VisMF::Write(mf, "checks/" + name);
79 }
80 else
81 {
82 Util::ParallelMessage(file,func,line,"[",Compare_cnt,"] ","Comparing ",name);
83 {
84 amrex::MultiFab mftmp;
85 amrex::VisMF::Read(mftmp,"checks/" + name);
86 if (ba != mftmp.boxArray())
87 {
88 Util::ParallelMessage(file,func,line,"[",Compare_cnt,"] ","our boxarray ",ba);
89 Util::ParallelMessage(file,func,line,"[",Compare_cnt,"] ","saved boxarray ",mftmp.boxArray());
90 Util::Warning(file,func,line,"[",Compare_cnt,"] ","different box arrays !! !! !!");
91 }
92 }
93
94 amrex::MultiFab mforig(ba,mf.distributionMap,ncomp,nghost);
95 amrex::VisMF::Read(mforig,"checks/" + name);
96
97 amrex::MultiFab mfdiff(ba, mf.distributionMap, ncomp, nghost);
98 amrex::MultiFab::Copy(mfdiff, mf,0,0,ncomp,nghost);
99 amrex::MultiFab::Subtract(mfdiff,mforig,0,0,ncomp,nghost);
100 mfdiff.abs(0,ncomp,nghost);
101
102 for (int n = 0 ; n < ncomp; n++)
103 {
104 Set::Scalar max = mfdiff.max(n,nghost);
105 auto argmax = mfdiff.maxIndex(n,nghost);
106 if (max > tolerance)
107 {
108 amrex::ParallelDescriptor::Barrier();
109 Util::ParallelMessage(file,func,line,"ours (n=",n,")");
110 amrex::ParallelDescriptor::Barrier();
111 Util::Debug::Probe(file,func,line,mf,argmax[0],argmax[1],0,n,3);
112
113 amrex::ParallelDescriptor::Barrier();
114 Util::ParallelMessage(file,func,line,"original (n=",n,")");
115 amrex::ParallelDescriptor::Barrier();
116 Util::Debug::Probe(file,func,line,mforig,argmax[0],argmax[1],0,n,3);
117
118 amrex::ParallelDescriptor::Barrier();
119 Util::ParallelMessage(file,func,line,"diff (n=",n,")");
120 amrex::ParallelDescriptor::Barrier();
121 Util::Debug::Probe(file,func,line,mfdiff,argmax[0],argmax[1],0,n,3);
122
123 messagestream << "_diff";
124 amrex::VisMF::Write(mfdiff,messagestream.str());
125 Util::Warning(file,func,line,"[",Compare_cnt,"] ",name);
126 Util::ParallelAbort(file,func,line,"[",Compare_cnt,"] ",max," at ", argmax);
127 }
128 }
129 }
130 //if (Compare_cnt == 169) Util::ParallelAbort(INFO,"exiting");
131}
132}
133}
134// LCOV_EXCL_STOP
int query_default(std::string name, T &value, T defaultvalue)
Definition ParmParse.H:293
amrex::Real Scalar
Definition Base.H:18
std::map< std::string, int > Compare_map
Definition Debug.cpp:19
int Compare_cnt
Definition Debug.cpp:20
void Compare(std::string file, std::string func, int line, const amrex::MultiFab &a_mf, std::string desc, amrex::Box domain, int)
Definition Debug.cpp:22
AMREX_FORCE_INLINE void Probe(std::string file, std::string func, int line, amrex::FabArray< amrex::BaseFab< T > > &a_mf, int i, int j, int k, int n)
Definition Debug.H:16
bool Compare_on
Definition Debug.cpp:18
int Allgather(std::vector< T > &a_data)
Definition MPI.H:18
AMREX_FORCE_INLINE int ReplaceAll(std::string &str, const std::string before, const std::string after)
Definition String.H:23
A collection of utility routines.
Definition Set.cpp:33
void ParallelAbort(std::string file, std::string func, int line, Args const &... args)
Definition Util.H:107
void ParallelMessage(std::string file, std::string func, int line, Args const &... args)
Definition Util.H:185
void Warning(std::string file, std::string func, int line, Args const &... args)
Definition Util.H:202