File indexing completed on 2026-04-09 07:48:58
0001
0002 #include <vector>
0003 #include <array>
0004 #include <cstdlib>
0005 #include <csignal>
0006
0007 #include "ssys.h"
0008 #include "spath.h"
0009
0010 #include "SSim.hh"
0011 #include "NP.hh"
0012
0013 #include "CSGFoundry.h"
0014 #include "CSGQuery.h"
0015 #include "CSGDraw.h"
0016
0017 #ifdef DEBUG_RECORD
0018 #include "CSGRecord.h"
0019 #endif
0020
0021 #include "CSGSimtraceRerun.h"
0022 #include "SLOG.hh"
0023
0024 const plog::Severity CSGSimtraceRerun::LEVEL = SLOG::EnvLevel("CSGSimtraceRerun", "DEBUG") ;
0025
0026
0027 CSGSimtraceRerun::CSGSimtraceRerun()
0028 :
0029 sim(SSim::Create()),
0030 fd(CSGFoundry::Load()),
0031 vv(fd ? fd->loadAux("Values/values.npy") : nullptr ),
0032 SELECTION(getenv("SELECTION")),
0033 selection(ssys::getenv_vec<int>("SELECTION",nullptr,',')),
0034 with_selection(selection && selection->size() > 0 ),
0035 fold(spath::Resolve("$T_FOLD")),
0036 path0(spath::Join(fold, "simtrace.npy")),
0037 path1(spath::Join(fold, with_selection ? "simtrace_selection.npy" : "simtrace_rerun.npy" )),
0038 simtrace0(NP::LoadIfExists(path0)),
0039 simtrace1(with_selection ? NP::Make<float>(selection->size(),2,4,4) : NP::MakeLike(simtrace0)),
0040 qq0(simtrace0 ? (const quad4*)simtrace0->bytes() : nullptr),
0041 qq1(simtrace1 ? (quad4*)simtrace1->bytes() : nullptr),
0042 q(fd ? new CSGQuery(fd) : nullptr),
0043 d(q ? new CSGDraw(q,'Z') : nullptr)
0044 {
0045 init();
0046 }
0047
0048 void CSGSimtraceRerun::init()
0049 {
0050 LOG_IF(fatal, fd == nullptr) << " NO GEOMETRY " ;
0051 assert( fd );
0052
0053
0054
0055 LOG(LEVEL) << ( d ? d->desc() : "-" ) ;
0056 LOG(LEVEL) << " fd.cfbase " << fd->cfbase ;
0057 LOG(LEVEL) << " vv " << ( vv ? vv->sstr() : "-" ) ;
0058 LOG(LEVEL) << "vv.lpath [" << ( vv->lpath.empty() ? "" : vv->lpath ) << "]" << std::endl << ( vv ? vv->descValues() : "-" ) ;
0059
0060 code_count.fill(0u);
0061 }
0062
0063 std::string CSGSimtraceRerun::desc() const
0064 {
0065 std::stringstream ss ;
0066 ss << "CSGSimtraceRerun::desc" << std::endl
0067 << " fd " << ( fd ? "Y" : "N" ) << std::endl
0068 << " fd.geom " << (( fd && fd->geom ) ? fd->geom : "-" ) << std::endl
0069 << " " << CSGQuery::Label() << std::endl
0070 << " path0 " << ( path0 ? path0 : "-" ) << std::endl
0071 << " path1 " << ( path1 ? path1 : "-" ) << std::endl
0072 << " simtrace0 " << ( simtrace0 ? simtrace0->sstr() : "-" ) << std::endl
0073 << " simtrace1 " << ( simtrace1 ? simtrace1->sstr() : "-" ) << std::endl
0074 << " SELECTION " << ( SELECTION ? SELECTION : "-" ) << std::endl
0075 << " selection " << ( selection ? "Y" : "N" )
0076 << " selection.size " << ( selection ? selection->size() : -1 ) << std::endl
0077 << " with_selection " << with_selection << std::endl
0078 ;
0079
0080 for(unsigned i=0 ; i < code_count.size() ; i++) ss << " code_count[" << i << "] " << code_count[i] << std::endl ;
0081 std::string s = ss.str();
0082 return s ;
0083 }
0084
0085 std::string CSGSimtraceRerun::Desc(const quad4& isect1, const quad4& isect0)
0086 {
0087 bool valid_isect0 = isect0.q0.f.w > isect0.q1.f.w ;
0088 bool valid_isect1 = isect1.q0.f.w > isect1.q1.f.w ;
0089
0090 std::stringstream ss ;
0091 ss << CSGQuery::Desc( isect0, "isect0", &valid_isect0 ) << std::endl ;
0092 ss << CSGQuery::Desc( isect1, "isect1", &valid_isect1 ) << std::endl ;
0093 std::string s = ss.str();
0094 return s ;
0095 }
0096
0097 unsigned CSGSimtraceRerun::intersect_again(quad4& isect1, const quad4& isect0 )
0098 {
0099 bool valid_isect = q->intersect_again(isect1, isect0);
0100 bool valid_isect0 = isect0.q0.f.w > isect0.q1.f.w ;
0101 bool valid_isect1 = isect1.q0.f.w > isect1.q1.f.w ;
0102 unsigned code = ( unsigned(valid_isect0) << 1 ) | unsigned(valid_isect1) ;
0103 assert( code < 4 );
0104 code_count[code] += 1 ;
0105 code_count[4] += 1 ;
0106
0107 bool valid_isect_expect = valid_isect == valid_isect1 ;
0108 assert( valid_isect_expect );
0109 if(!valid_isect_expect) std::raise(SIGINT);
0110
0111 return code ;
0112 }
0113
0114 void CSGSimtraceRerun::intersect_again(unsigned idx, bool dump)
0115 {
0116 const quad4& isect0 = qq0[idx] ;
0117 quad4& isect1 = qq1[idx] ;
0118 unsigned code = intersect_again(isect1, isect0);
0119
0120 if( dump || code == 1 || code == 2 )
0121 {
0122 std::cout
0123 << "CSGSimtraceRerun::intersect_again"
0124 << " idx " << std::setw(7) << idx
0125 << " code " << code
0126 << std::endl
0127 << Desc(isect1, isect0)
0128 << std::endl
0129 ;
0130 }
0131 }
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142 void CSGSimtraceRerun::intersect_again_selection(unsigned i, bool dump)
0143 {
0144 unsigned idx = selection->at(i) ;
0145 const quad4& isect0_ = qq0[idx] ;
0146
0147 quad4& isect0 = qq1[i*2+0] ;
0148 quad4& isect1 = qq1[i*2+1] ;
0149 isect0 = isect0_ ;
0150
0151 unsigned code = intersect_again(isect1, isect0);
0152
0153 if( dump || code == 1 || code == 3 )
0154 {
0155 std::cout << " i " << std::setw(3) << i << " idx " << std::setw(7) << idx << " code " << code << std::endl ;
0156 std::cout << Desc(isect1, isect0) << std::endl ;
0157 }
0158 }
0159
0160
0161 void CSGSimtraceRerun::intersect_again()
0162 {
0163 unsigned n = with_selection ?
0164 ( selection ? selection->size() : 0u )
0165 :
0166 (simtrace0 ? simtrace0->shape[0] : 0u )
0167 ;
0168
0169 for(unsigned i=0 ; i < n ; i++)
0170 {
0171 if( with_selection )
0172 {
0173 intersect_again_selection(i,true);
0174 }
0175 else
0176 {
0177
0178 bool dump = false ;
0179 intersect_again(i, dump);
0180 }
0181 }
0182 }
0183
0184 void CSGSimtraceRerun::save() const
0185 {
0186 LOG_IF(error, simtrace1 == nullptr) << " simtrace1 null : cannot save " ;
0187 if(simtrace1 == nullptr) return ;
0188
0189 LOG(info) << " path1 " << path1 ;
0190 if(with_selection) simtrace1->set_meta<std::string>("SELECTION", SELECTION) ;
0191 simtrace1->save(path1);
0192 }
0193
0194 void CSGSimtraceRerun::report() const
0195 {
0196 LOG(info) << "t.desc " << desc() ;
0197 #ifdef DEBUG_RECORD
0198 LOG(info) << "with : DEBUG_RECORD " ;
0199 CSGRecord::Dump("CSGSimtraceRerun::report");
0200
0201 LOG(info) << " save CSGRecord.npy to fold " << fold ;
0202 CSGRecord::Save(fold);
0203 #else
0204 std::cout << "not with DEBUG_RECORD : recompile with DEBUG_RECORD for full detailed recording " << std::endl ;
0205 #endif
0206
0207 #ifdef DEBUG
0208 std::cout << "with : DEBUG " << std::endl ;
0209 #else
0210 std::cout << "not with : DEBUG : recompile with DEBUG for full detailed recording " << std::endl ;
0211 #endif
0212
0213 }
0214
0215