/* -*- C++ -*- */ /************************************************************************* * Copyright(c) 1995~2005 Masaharu Goto ([email protected]) * * For the licensing terms see the file COPYING * ************************************************************************/ /************************************************************************* * eular.c * * array pre-compiled class library is needed to run this demo program. *************************************************************************/ #include <array.h> const complex j=complex(0,1); const double PI=3.141592; main() { array x=array(-2*PI , 2*PI , 100 ); // start,stop,npoint plot << "Eular's Law" << x << exp(x*j) << "exp(x*j)" << endl ; }
static void fp_xpress(char *s, char *e, char *tag) { char *p, *q = s; int unary = *s; int x; if ((unary == '+') || (unary == '-') || (unary == '*')) q++; if ((p = contains(q, e, "+\0-\0")) || (p = contains(q, e, "/\0*\0"))) { q = p + 1; if (complex(q, e)) { fp_xpress(q, e, tag); switch (*p) { case '-': fpxpress_asmq(" $x_reserve "); fp_xpress(s, p, tag); fpxpress_asmq(" $x_retrieve_subtract "); break; case '+': if (complex_beyond(s, p, "+\0-\0*+\0*-\0")) { fpxpress_asmq(" $x_reserve "); fp_xpress(s, p, tag); fpxpress_asmq(" $x_retrieve_add "); break; } x = PLUS; while (q = next_operator(s, p, "+\0-\0", 0)) { trailing_fp_operation(x, s, q, tag); x = oper_ator(q, p - q); s = q + ufield[x]; } trailing_fp_operation(x, s, p, tag); break; case '*': if (complex_beyond(s, p, "*\0/\0*+\0*-\0")) { fpxpress_asmq(" $x_reserve "); fp_xpress(s, p, tag); fpxpress_asmq(" $x_retrieve_multiply "); break; } x = MULTIPLY; while (q = next_operator(s, p, "*\0/\0", 0)) { trailing_fp_operation(x, s, q, tag); x = oper_ator(q, p - q); s = q + ufield[x]; } trailing_fp_operation(x, s, p, tag); break; case '/': fpxpress_asmq(" $x_reserve "); fp_xpress(s, p, tag); fpxpress_asmq(" $x_retrieve_divide "); } } else { fp_xpress(s, p, tag); if (*q == '(') q++; switch(*p) { case '-': fpxpress_assemble(" $x_subtract ", q, e, tag); break; case '+': fpxpress_assemble(" $x_add ", q, e, tag); break; case '*': fpxpress_assemble(" $x_multiply ", q, e, tag); break; case '/': fpxpress_assemble(" $x_divide ", q, e, tag); } } return; } if (*s == '(') { fp_xpress(s + 1, e, tag); return; } unary = *s; if ((unary == '+') || (unary == '-')) { if (*(s + 1) == '(') { fp_xpress(s + 2, e, tag); if (unary == '-') fpxpress_asmq(" $x_reverse"); return; } if (number(s + 1, e) == 0) { if (unary == '+') fpxpress_assemble(" $x_load ", s + 1, e, tag); else fpxpress_assemble(" $x_load_negative ", s + 1, e, tag); return; } } fpxpress_assemble(" $x_load ", s, e, tag); }
double refrigerator::update_refrigerator_state(double dt0,TIMESTAMP t1) { OBJECT *hdr = OBJECTHDR(this); // accumulate the energy energy_used += refrigerator_power*dt0; if(cycle_time>0) { cycle_time -= dt0; } if(defrostDelayed>0) { defrostDelayed -= dt0; } if(defrostDelayed<=0){ if(DC_TIMED==defrost_criterion){ run_defrost = true; } else if(DC_COMPRESSOR_TIME==defrost_criterion && total_compressor_time>=compressor_defrost_time){ run_defrost = true; total_compressor_time = 0; check_defrost = false; } else if(no_of_defrost>0 && DC_DOOR_OPENINGS==defrost_criterion){ run_defrost = true; FF_Door_Openings = 0; check_defrost = false; } } else{ if(DC_TIMED==defrost_criterion){ check_defrost = true; } else if(DC_COMPRESSOR_TIME==defrost_criterion && total_compressor_time>=compressor_defrost_time){ check_defrost = true; } else if(no_of_defrost>0 && DC_DOOR_OPENINGS==defrost_criterion){ check_defrost = true; } } if(long_compressor_cycle_time>0) { long_compressor_cycle_time -= dt0; } if (dr_mode_double == 0) dr_mode = DM_UNKNOWN; else if (dr_mode_double == 1) dr_mode = DM_LOW; else if (dr_mode_double == 2) dr_mode = DM_NORMAL; else if (dr_mode_double == 3) dr_mode = DM_HIGH; else if (dr_mode_double == 4) dr_mode = DM_CRITICAL; else dr_mode = DM_UNKNOWN; if(door_return_time > 0){ door_return_time = door_return_time - dt0; } if(door_next_open_time > 0){ door_next_open_time = door_next_open_time - dt0; } if(door_return_time<=0 && true==door_open){ door_return_time = 0; door_open = false; } if(door_next_open_time<=0 && hourly_door_opening>0){ door_next_open_time = 0; door_open = true; FF_Door_Openings++; door_return_time += ceil(gl_random_uniform(0, door_open_time)); door_energy_calc = true; hourly_door_opening--; if(hourly_door_opening > 0){ door_next_open_time = int(gl_random_uniform(0,(3600-(t1-start_time)%3600))); // next_door_openings[hourly_door_opening-1] - ((t1-start_time)%3600); door_to_open = true; } else{ door_to_open = false; } check_DO = FF_Door_Openings%DO_Thershold; if(check_DO==0){ no_of_defrost++; } } if(((t1-start_time)%3600)==0 && start_time>0) { double temp_hourly_door_opening = (door_opening_criterion*daily_door_opening) - floor(door_opening_criterion*daily_door_opening); if(temp_hourly_door_opening >= 0.5){ hourly_door_opening = ceil(door_opening_criterion*daily_door_opening); } else{ hourly_door_opening = floor(door_opening_criterion*daily_door_opening); } hourly_door_opening = floor(gl_random_normal(hourly_door_opening, ((hourly_door_opening)/3))); //Clip the hourly door openings if(hourly_door_opening<0){ hourly_door_opening = 0; } else if(hourly_door_opening>2*hourly_door_opening){ hourly_door_opening = 2*hourly_door_opening; } if(hourly_door_opening > long_compressor_cycle_threshold*daily_door_opening) { long_compressor_cycle_due = true; long_compressor_cycle_time = int(gl_random_uniform(0,(3600))); } if(hourly_door_opening>0){ door_to_open = true; door_next_open_time = int(gl_random_uniform(0,(3600))); } else{ door_to_open = false; } } double dt1 = 0; switch(state){ case RS_DEFROST: if ((energy_used >= energy_needed || cycle_time <= 0)) { state = RS_COMPRESSSOR_ON_LONG; refrigerator_power = long_compressor_cycle_power; energy_needed = long_compressor_cycle_energy; energy_used = 0; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); } else { refrigerator_power = defrost_power; } break; case RS_COMPRESSSOR_ON_NORMAL: if(run_defrost==true) { state = RS_DEFROST; refrigerator_power = defrost_power; energy_needed = defrost_energy; energy_used = 0; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); run_defrost=false; no_of_defrost--; defrostDelayed = delay_defrost_time; } else if ((energy_used >= energy_needed || cycle_time <= 0)) { state = RS_COMPRESSSOR_OFF_NORMAL; refrigerator_power = compressor_off_normal_power; energy_needed = compressor_off_normal_energy; energy_used = 0; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); //new_running_state = true; } else { refrigerator_power = compressor_on_normal_power; total_compressor_time += dt0; if(door_energy_calc==true){ door_energy_calc = false; energy_needed += door_return_time*refrigerator_power; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); } } break; case RS_COMPRESSSOR_ON_LONG: if(run_defrost==true) { state = RS_DEFROST; refrigerator_power = defrost_power; energy_needed = defrost_energy; energy_used = 0; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); run_defrost=false; no_of_defrost--; defrostDelayed = delay_defrost_time; } else if ((energy_used >= energy_needed || cycle_time <= 0)) { state = RS_COMPRESSSOR_OFF_NORMAL; refrigerator_power = compressor_off_normal_power; energy_needed = compressor_off_normal_energy; energy_used = 0; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); //new_running_state = true; } else { refrigerator_power = long_compressor_cycle_power; if(door_energy_calc==true){ door_energy_calc = false; energy_needed += door_return_time*refrigerator_power; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); } } break; case RS_COMPRESSSOR_OFF_NORMAL: if(run_defrost==true) { state = RS_DEFROST; refrigerator_power = defrost_power; energy_needed = defrost_energy; energy_used = 0; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); run_defrost=false; no_of_defrost--; defrostDelayed = delay_defrost_time; } else if ((energy_used >= energy_needed || cycle_time <= 0)) { if(long_compressor_cycle_due==true && long_compressor_cycle_time<=0) { state = RS_COMPRESSSOR_ON_LONG; refrigerator_power = long_compressor_cycle_power; energy_needed = long_compressor_cycle_energy; long_compressor_cycle_due=false; energy_used = 0; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); } else{ state = RS_COMPRESSSOR_ON_NORMAL; refrigerator_power = compressor_on_normal_power; energy_needed = compressor_on_normal_energy; energy_used = 0; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); } } else { refrigerator_power = compressor_off_normal_power; if(door_energy_calc==true){ door_energy_calc = false; energy_needed -= door_return_time*compressor_off_normal_power; cycle_time = ceil((energy_needed - energy_used)/refrigerator_power); if(cycle_time<0) cycle_time = 0; } } break; } double posted_power = refrigerator_power; //*************************** if(door_open==true) { posted_power += door_opening_power; } //*************************** if(check_icemaking == true) { posted_power += icemaking_power; icemaker_running = true; return_time = 60-dt0; if(0==((t1-start_time)%60) || return_time<=0){ ice_making_no--; if(ice_making_no == 0) { check_icemaking = false; } return_time = 60; } } else { if(((t1-start_time)%60)==0) { ice_making = double(gl_random_uniform(0,1)); if(ice_making <=ice_making_probability) { check_icemaking = true; icemaker_running = true; // ice_making_no = gl_random_sampled(3,ice_making_time); ice_making_no = 1; posted_power += icemaking_power; return_time = 60; ice_making_no--; if(ice_making_no == 0) { check_icemaking = false; } } else { icemaker_running = false; return_time = 60; } } else { icemaker_running = false; } } load.power.SetPowerFactor(posted_power/1000, load.power_factor); load.admittance = complex(0,0,J); load.current = complex(0,0,J); if(true==door_open && true==door_to_open) { door_time = door_return_time<door_next_open_time?door_return_time:door_next_open_time; } else if(true==door_to_open) { door_time = door_next_open_time; } else if(true==door_open){ door_time = door_return_time; } else if(start_time>0) { door_time = (3600 - ((t1-start_time)%3600)); } if(0.0==return_time) { dt1 = cycle_time>door_time?door_time:cycle_time; } else { if(cycle_time>return_time) if(return_time>door_time) if(door_time>long_compressor_cycle_time) dt1 = long_compressor_cycle_time; else dt1 = door_time; else if(return_time>long_compressor_cycle_time) dt1 = long_compressor_cycle_time; else dt1 = return_time; else if(cycle_time>door_time) if(door_time>long_compressor_cycle_time) dt1 = long_compressor_cycle_time; else dt1 = door_time; else if(cycle_time>long_compressor_cycle_time) dt1 = long_compressor_cycle_time; else dt1 = cycle_time; } if(check_defrost == true){ if(dt1 > defrostDelayed){ dt1 = defrostDelayed; } } load.total = load.power + load.current + load.admittance; total_power = (load.power.Re() + (load.current.Re() + load.admittance.Re()*load.voltage_factor)*load.voltage_factor); last_dr_mode = dr_mode; if ((dt1 > 0) && (dt1 < 1)){ dt1 = 1; } return dt1; }
TIMESTAMP house::sync_panel(TIMESTAMP t0, TIMESTAMP t1) { TIMESTAMP sync_time = TS_NEVER; OBJECT *obj = OBJECTHDR(this); // clear accumulators for panel currents complex I[3]; I[X12] = I[X23] = I[X13] = complex(0,0); // clear heatgain accumulator double heatgain = 0; // gather load power and compute current for each circuit CIRCUIT *c; for (c=panel.circuits; c!=NULL; c=c->next) { // get circuit type int n = (int)c->type; if (n<0 || n>2) GL_THROW("%s:%d circuit %d has an invalid circuit type (%d)", obj->oclass->name, obj->id, c->id, (int)c->type); /* TROUBLESHOOT Invalid circuit types are an internal error for the house panel. Please report this error. The likely causes include an object that is not a house is being processed by the house model, or the panel was not correctly initialized. */ // if breaker is open and reclose time has arrived if (c->status==BRK_OPEN && t1>=c->reclose) { c->status = BRK_CLOSED; c->reclose = TS_NEVER; sync_time = t1; // must immediately reevaluate devices affected gl_debug("house:%d panel breaker %d closed", obj->id, c->id); } // if breaker is closed if (c->status==BRK_CLOSED) { // compute circuit current if ((c->pV)->Mag() == 0) { gl_debug("house:%d circuit %d (enduse %s) voltage is zero", obj->id, c->id, c->pLoad->name); break; } complex current = ~(c->pLoad->total*1000 / *(c->pV)); // check breaker if (c->max_amps>0 && current.Mag()>c->max_amps) { // probability of breaker failure increases over time if (c->tripsleft>0 && gl_random_bernoulli(1/(c->tripsleft--))==0) { // breaker opens c->status = BRK_OPEN; // average five minutes before reclosing, exponentially distributed c->reclose = t1 + (TIMESTAMP)(gl_random_exponential(1/300.0)*TS_SECOND); gl_debug("house:%d circuit breaker %d tripped - enduse %s overload at %.0f A", obj->id, c->id, c->pLoad->name, current.Mag()); } // breaker fails from too frequent operation else { c->status = BRK_FAULT; c->reclose = TS_NEVER; gl_debug("house:%d circuit breaker %d failed", obj->id, c->id); } // must immediately reevaluate everything sync_time = t1; } // add to panel current else { tload.power += c->pLoad->power; // reminder: |a| + |b| != |a+b| tload.current += c->pLoad->current; tload.admittance += c->pLoad->admittance; // should this be additive? I don't buy t.a = c->pL->a ... -MH tload.total += c->pLoad->total; tload.heatgain += c->pLoad->heatgain; tload.energy += c->pLoad->power * gl_tohours(t1-t0); I[n] += current; c->reclose = TS_NEVER; } } // sync time if (sync_time > c->reclose) sync_time = c->reclose; } // compute line currents and post to meter if (obj->parent != NULL) LOCK_OBJECT(obj->parent); pLine_I[0] = I[X13]; pLine_I[1] = I[X23]; pLine_I[2] = 0; *pLine12 = I[X12]; if (obj->parent != NULL) UNLOCK_OBJECT(obj->parent); return sync_time; }
bool analyzeTrafficBurst(signalVector &rxBurst, unsigned TSC, float detectThreshold, int samplesPerSymbol, complex *amplitude, float *TOA, unsigned maxTOA, bool requestChannel, signalVector **channelResponse, float *channelResponseOffset) { assert(TSC<8); assert(amplitude); assert(TOA); assert(gMidambles[TSC]); if (maxTOA < 3*samplesPerSymbol) maxTOA = 3*samplesPerSymbol; unsigned spanTOA = maxTOA; if (spanTOA < 5*samplesPerSymbol) spanTOA = 5*samplesPerSymbol; unsigned startIx = (66-spanTOA)*samplesPerSymbol; unsigned endIx = (66+16+spanTOA)*samplesPerSymbol; unsigned windowLen = endIx - startIx; unsigned corrLen = 2*maxTOA+1; unsigned expectedTOAPeak = (unsigned) round(gMidambles[TSC]->TOA + (gMidambles[TSC]->sequenceReversedConjugated->size()-1)/2); signalVector burstSegment(rxBurst.begin(),startIx,windowLen); static complex staticData[200]; signalVector correlatedBurst(staticData,0,corrLen); correlate(&burstSegment, gMidambles[TSC]->sequenceReversedConjugated, &correlatedBurst, CUSTOM,true, expectedTOAPeak-maxTOA,corrLen); float meanPower; *amplitude = peakDetect(correlatedBurst,TOA,&meanPower); float valleyPower = 0.0; //amplitude->norm2(); complex *peakPtr = correlatedBurst.begin() + (int) rint(*TOA); // check for bogus results if ((*TOA < 0.0) || (*TOA > correlatedBurst.size())) { *amplitude = 0.0; return false; } int numRms = 0; for (int i = 2*samplesPerSymbol; i <= 5*samplesPerSymbol;i++) { if (peakPtr - i >= correlatedBurst.begin()) { valleyPower += (peakPtr-i)->norm2(); numRms++; } if (peakPtr + i < correlatedBurst.end()) { valleyPower += (peakPtr+i)->norm2(); numRms++; } } if (numRms < 2) { // check for bogus results *amplitude = 0.0; return false; } float RMS = sqrtf(valleyPower/(float)numRms)+0.00001; float peakToMean = (amplitude->abs())/RMS; // NOTE: Because ideal TSC is 66 symbols into burst, // the ideal TSC has an +/- 180 degree phase shift, // due to the pi/4 frequency shift, that // needs to be accounted for. *amplitude = (*amplitude)/gMidambles[TSC]->gain; *TOA = (*TOA) - (maxTOA); LOG(DEBUG) << "TCH peakAmpl=" << amplitude->abs() << " RMS=" << RMS << " peakToMean=" << peakToMean << " TOA=" << *TOA; LOG(DEBUG) << "autocorr: " << correlatedBurst; if (requestChannel && (peakToMean > detectThreshold)) { float TOAoffset = maxTOA; //gMidambles[TSC]->TOA+(66*samplesPerSymbol-startIx); delayVector(correlatedBurst,-(*TOA)); // midamble only allows estimation of a 6-tap channel signalVector channelVector(6*samplesPerSymbol); float maxEnergy = -1.0; int maxI = -1; for (int i = 0; i < 7; i++) { if (TOAoffset+(i-5)*samplesPerSymbol + channelVector.size() > correlatedBurst.size()) continue; if (TOAoffset+(i-5)*samplesPerSymbol < 0) continue; correlatedBurst.segmentCopyTo(channelVector,(int) floor(TOAoffset+(i-5)*samplesPerSymbol),channelVector.size()); float energy = vectorNorm2(channelVector); if (energy > 0.95*maxEnergy) { maxI = i; maxEnergy = energy; } } *channelResponse = new signalVector(channelVector.size()); correlatedBurst.segmentCopyTo(**channelResponse,(int) floor(TOAoffset+(maxI-5)*samplesPerSymbol),(*channelResponse)->size()); scaleVector(**channelResponse,complex(1.0,0.0)/gMidambles[TSC]->gain); LOG(DEEPDEBUG) << "channelResponse: " << **channelResponse; if (channelResponseOffset) *channelResponseOffset = 5*samplesPerSymbol-maxI; } return (peakToMean > detectThreshold); }
/*! \deprecated use standard contructors for typecasting \sa cxsc::complex::complex(const cdotprecision &) */ friend inline complex _complex(const cdotprecision &a) throw() { return complex(a); }
scalar fn_init(double x, double y, scalar& dx, scalar& dy) { return complex(exp(-10*(x*x + y*y)), 0); }
/** exponentioal of a complex number \ingroup complex \param[in] z Complex number \return \f$ e^z \f$*/ complex exp(const complex& z) { return complex(gsl_complex_exp(z.as_gsl_type())); }
/** Logarithm of a complex number (base 10) \ingroup complex \param[in] z Complex number \return \f$ \log_{10} z \f$*/ complex log10(const complex& z) { return complex(gsl_complex_log10(z.as_gsl_type())); }
complex complex::conjugate() const { gsl_complex t = gsl_complex_conjugate(_complex); return complex(t); }
complex complex::inverse() const { gsl_complex t = gsl_complex_inverse(_complex); return complex(t); }
complex complex::operator/(const double& a) const { gsl_complex rl = gsl_complex_div_real(_complex,a); return complex(rl); }
complex complex::operator/(const complex& z1) const { gsl_complex rl = gsl_complex_div(_complex, z1._complex); return complex(rl); }
complex complex::operator-() const { gsl_complex t = gsl_complex_negative(_complex); return complex(&t); }
/** Hyperbolic cosecant \ingroup complex \param[in] z Complex number \return \f$ \mathrm{csch} z \f$*/ complex csch(const complex& z) { return complex(gsl_complex_csch(z.as_gsl_type())); }
/** Logarithm of a complex number (base b) \ingroup complex \param[in] z Complex number \param[in] b Complex number \return \f$ \log_b z \f$*/ complex log(const complex& z, const complex& b) { return complex(gsl_complex_log_b(z.as_gsl_type(),b.as_gsl_type())); }
/** Inverse hyperbolic cotangent \ingroup complex \param[in] z Complex number \return \f$ \mathrm{acoth}(z) \f$*/ complex arccoth(const complex& z) { return complex(gsl_complex_arccoth(z.as_gsl_type())); }
/** DiLogarithm of a complex number \ingroup complex \param[in] z Complex number \return \f$ Li_2(z) \f$*/ complex dilog(const complex& z) { gsl_sf_result re, im; gsl_sf_complex_dilog_xy_e(z.real(), z.imag(), &re, &im); return complex(re.val, im.val, false); }
void CAMdataHandler::allocateData(long size, int dType) { switch(dType) { case CAMType::typeInt : dataSize = size; dataPointer = new int[size]; break; case CAMType::typeLong : dataSize = size; dataPointer = new long[size]; break; case CAMType::typeFloat : dataSize = size; dataPointer = new float[size]; break; case CAMType::typeDouble : dataSize = size; dataPointer = new double[size]; break; #ifndef __NO_COMPLEX__ case CAMType::typeComplex : dataSize = size; dataPointer = new complex[size]; break; #endif } // // initialize with zero's // #ifndef __NO_BLAS__ int izero = 0; long lzero = 0; float fzero = 0.0; double dzero = 0.0; long strideX = 0; long strideY = 1; #endif switch(dType) { case CAMType::typeInt : register int* idataP; #ifdef __NO_BLAS__ for(idataP = (int*)dataPointer; idataP < (int*)dataPointer + size; idataP++) *(idataP) = 0; #else idataP = (int*)dataPointer; icopy_(&size,&izero,&strideX, idataP, &strideY); #endif break; case CAMType::typeLong : register long* ldataP; #ifdef __NO_BLAS__ for(ldataP = (long*)dataPointer; ldataP < (long*)dataPointer + size; ldataP++) *(ldataP) = 0; #else ldataP = (long*)dataPointer; lcopy_(&size,&lzero,&strideX, ldataP, &strideY); #endif break; case CAMType::typeFloat : register float* fdataP; #ifdef __NO_BLAS__ for(fdataP = (float*)dataPointer; fdataP < (float*)dataPointer + size; fdataP++) *(fdataP) = 0.0; #else fdataP = (float*)dataPointer; scopy_(&size,&fzero,&strideX, fdataP, &strideY); #endif break; case CAMType::typeDouble : register double* ddataP; #ifdef __NO_BLAS__ for(ddataP = (double*)dataPointer; ddataP < (double*)dataPointer + size; ddataP++) *(ddataP) = 0.0; #else ddataP = (double*)dataPointer; dcopy_(&size,&dzero,&strideX, ddataP, &strideY); #endif break; #ifndef __NO_COMPLEX__ case CAMType::typeComplex : complex* cdataP; for(cdataP = (complex*)dataPointer; cdataP < (complex*)dataPointer + size; cdataP++) *(cdataP) = complex(0.0,0.0); break; #endif } }
/** Square root of a complex number \ingroup complex \param[in] z Complex number \return \f$ \sqrt z \f$*/ complex sqrt(const complex& z) { return complex(gsl_complex_sqrt(z.as_gsl_type())); }
complex bc_values(int marker, double x, double y) { return complex(0.0, 0.0); }
/** Complex number to the z2 complex order \ingroup complex \param[in] z1 Complex number \param[in] z2 Complex number \return \f$ z_1^{z_2} \f$*/ complex pow(const complex& z1, const complex& z2) { return complex(gsl_complex_pow(z1.as_gsl_type(), z2.as_gsl_type())); }
//////////////////////////////////////////////////////////// // read shifted states // shifted states are needed to calculate epsilon when q=0 //////////////////////////////////////////////////////////// void States::readStateShifted(char *fromFile) //=================================================================================== {//begin routine //=================================================================================== // A little screen output for the fans //CkPrintf("Reading state file: %s for chare (%d %d %d), with binary option %d.\n",fromFile,ispin,ikpt,istate,ibinary_opt); if(ibinary_opt < 0 || ibinary_opt > 3){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Bad binary option : %d\n",ibinary_opt); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); }//endif //=================================================================================== // First read in the state and k-vectors : allows parsing of doublePack option #if !CMK_PROJECTIONS_USE_ZLIB if(ibinary_opt>1) { CkPrintf("Attempt to use ZLIB Failed! Please review compilation\n"); //CkPrintf("Macro cmk-projections-use-zlib is %d \n", CMK_PROJECTIONS_USE_ZLIB); CkExit(); } #endif int nx,ny,nz; int nktot = 0; if(ibinary_opt==0){ FILE *fp=fopen(fromFile,"r"); if (fp==NULL){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't open state file %s\n",fromFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); }//endif int numCoeffLoc; if(4!=fscanf(fp,"%d%d%d%d",&numCoeffLoc,&nx,&ny,&nz)){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't parse size line of file %s\n",fromFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); }//endif numCoeff = numCoeffLoc; stateCoeff_shifted = new complex [numCoeffLoc]; //ga = new int [numCoeffLoc]; //gb = new int [numCoeffLoc]; //gc = new int [numCoeffLoc]; for(int pNo=0;pNo<numCoeff;pNo++) { int x,y,z; double re,im; if(5!=fscanf(fp,"%lg%lg%d%d%d",&re,&im,&x,&y,&z)){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't parse packed state location %s\n",fromFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); }//endif stateCoeff_shifted[pNo] = complex(re, im); /* we do not need to save this for shifted states ga[pNo] = x; gb[pNo] = y; gc[pNo] = z; */ nktot++; if(doublePack && x==0 && y==0 && z==0){break;} }//endfor fclose(fp); } #if CMK_PROJECTIONS_USE_ZLIB else if(ibinary_opt==2){ // CkPrintf("Using ZLIB to load ascii states\n"); char bigenough[1000]; //we know our lines are shorter than this char localFile[1000]; // fromFile is const strcpy(localFile,fromFile); strcat(localFile,".gz"); gzFile zfp=gzopen(localFile,"rb"); if (zfp==NULL){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't open state file %s\n",localFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); }//endif int numCoeffLoc; if(gzgets(zfp,bigenough,1000)!=Z_NULL) { if(4!=sscanf(bigenough,"%d%d%d%d",&numCoeffLoc,&nx,&ny,&nz)){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't parse size line of file %s\n",localFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); }//endif numCoeff = numCoeffLoc; stateCoeff_shifted = new complex [numCoeffLoc]; //ga = new int [numCoeffLoc]; //gb = new int [numCoeffLoc]; //gc = new int [numCoeffLoc]; } else { CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't parse size line of file %s\n",localFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); } for(int pNo=0;pNo<numCoeff;pNo++) { int x,y,z; double re,im; if(gzgets(zfp,bigenough,1000)!=Z_NULL) { if(5!=sscanf(bigenough,"%lg%lg%d%d%d",&re,&im,&x,&y,&z)){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't parse packed state location %s\n",localFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); }//endif } else { CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't parse packed state location %s\n",localFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); } stateCoeff_shifted[pNo] = complex(re, im); /* we do not need to save this for shifted states ga[pNo] = x; gb[pNo] = y; gc[pNo] = z; */ nktot++; if(doublePack && x==0 && y==0 && z==0){break;} }//endfor gzclose(zfp); } else if (ibinary_opt==3) { // CkPrintf("Using ZLIB to load binary states\n"); char localFile[1000]; // fromFile is const strcpy(localFile,fromFile); strcat(localFile,".gz"); gzFile zfp=gzopen(localFile,"rb"); if (zfp==NULL){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't open state file %s\n",localFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); } int numCoeffLoc; int n=1; gzread(zfp,&(numCoeffLoc),sizeof(int)); gzread(zfp,&(nx),sizeof(int)); gzread(zfp,&(ny),sizeof(int)); gzread(zfp,&(nz),sizeof(int)); numCoeff = numCoeffLoc; stateCoeff_shifted = new complex [numCoeffLoc]; //ga = new int [numCoeffLoc]; //gb = new int [numCoeffLoc]; //gc = new int [numCoeffLoc]; for(int pNo=0;pNo<numCoeff;pNo++) { int x,y,z; double re,im; gzread(zfp,&(re),sizeof(double)); gzread(zfp,&(im),sizeof(double)); gzread(zfp,&(x),sizeof(int)); gzread(zfp,&(y),sizeof(int)); gzread(zfp,&(z),sizeof(int)); stateCoeff_shifted[pNo] = complex(re, im); /* we do not need to save this for shifted states ga[pNo] = x; gb[pNo] = y; gc[pNo] = z; */ nktot++; if(doublePack && x==0 && y==0 && z==0){break;} } gzclose(zfp); } #endif else{ FILE *fp=fopen(fromFile,"rb"); if (fp==NULL){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Can't open state file %s\n",fromFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); } int numCoeffLoc; int n=1; assert(fread(&(numCoeffLoc),sizeof(int),n,fp)>0); assert(fread(&(nx),sizeof(int),n,fp)); assert(fread(&(ny),sizeof(int),n,fp)); assert(fread(&(nz),sizeof(int),n,fp)); numCoeff = numCoeffLoc; stateCoeff_shifted = new complex [numCoeffLoc]; //ga = new int [numCoeffLoc]; //gb = new int [numCoeffLoc]; //gc = new int [numCoeffLoc]; for(int pNo=0;pNo<numCoeff;pNo++) { int x,y,z; double re,im; assert(fread(&(re),sizeof(double),n,fp)); assert(fread(&(im),sizeof(double),n,fp)); assert(fread(&(x),sizeof(int),n,fp)); assert(fread(&(y),sizeof(int),n,fp)); assert(fread(&(z),sizeof(int),n,fp)); stateCoeff_shifted[pNo] = complex(re, im); /* we do not need to save this for shifted states ga[pNo] = x; gb[pNo] = y; gc[pNo] = z; */ nktot++; if(doublePack && x==0 && y==0 && z==0){break;} }//endfor fclose(fp); }//endif #ifdef _CP_DEBUG_UTIL_VERBOSE_ CkPrintf("Done reading state from file: %s\n",fromFile); #endif //=================================================================================== if (nktot!=numCoeff){ CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkPrintf("Inconsistent number of coefficients in %s\n",fromFile); CkPrintf("@@@@@@@@@@@@@@@@@@@@_error_@@@@@@@@@@@@@@@@@@@@\n"); CkExit(); } //---------------------------------------------------------------------------------- }//end routine
/** Complex number to the x real order \ingroup complex \param[in] z Complex number \param[in] x Real number \return \f$ z^x \f$ */ complex pow(const complex& z, const double x) { return complex(gsl_complex_pow_real(z.as_gsl_type(), x)); }
complex determine_source_voltage(complex voltage_out, double r_internal, double r_load){ //placeholder, probably need to replace with iterative function complex Vs = complex(((r_internal + r_load)/(r_load)) * voltage_out.Re(), ((r_internal + r_load)/(r_load)) * voltage_out.Im()); return Vs; }
/** Secant \ingroup complex \param[in] z Complex number \return \f$ \sec z \f$*/ complex sec(const complex& z) { return complex(gsl_complex_sec(z.as_gsl_type())); }
int main() { complex I=complex(0.,1.); int N=1024;//50; double R=.05; int ord=0; HankelMatrix HH(N,R); double r_max = .05; //% Maximum radius (5cm) double dr = r_max/(N-1); //% Radial spacing //r = nr*dr; printf("N=%d\n",N); FILE *in0,*in1; in0=fopen("besselCeros.txt","r"); in1=fopen("InputFunction.txt","r"); FILE *out0,*out1,*out2,*out3,*out4,*out5; out0=fopen("pout0.txt","w"); out1=fopen("pout1.txt","w"); out2=fopen("pout2.txt","w"); out3=fopen("pout3.txt","w"); out4=fopen("pout4.txt","w"); out5=fopen("pout5.txt","w"); /***********************************/ //Transformation grid g1; //Define a grid object g1.set_grid(N,1,1,1.,1.,1.); field f,F,f2,F2,Fretrieved,fretrieved; f.put_on_grid(g1); F.put_on_grid(g1); f2.put_on_grid(g1); F2.put_on_grid(g1); fretrieved.put_on_grid(g1); Fretrieved.put_on_grid(g1); //Define the function double r0=0.; double FWHM=5.e-3; double Kr=5000.; double K=6.4387e+04; int Nz = 200; //% Number of z positions double z_max = .25; //% Maximum propagation distance double dz = z_max/(Nz-1); vector<double> phi; phi.resize(N,0.); for(int i=0;i<N;i++) { //f.w[i][0]=exp(-2*log(2)*((r[i]-r0)/FWHM)*((r[i]-r0)/FWHM) )*cos(Kr*r[i]); //f.w[i][1]=exp(-2*log(2)*((r[i]-r0)/FWHM)*((r[i]-r0)/FWHM) )*sin(Kr*r[i]); double f1,f2; fscanf(in1,"%lf %lf",&f1,&f2); f.w[i][0]=f1;//exp(-2*log(2)*((canonic_r[i]-r0)/FWHM)*((canonic_r[i]-r0)/FWHM) )*cos(Kr*canonic_r[i]); f.w[i][1]=f2;//exp(-2*log(2)*((canonic_r[i]-r0)/FWHM)*((canonic_r[i]-r0)/FWHM) )*sin(Kr*canonic_r[i]); F.w[i][0]=f.w[i][0]/HH.m1[i]; F.w[i][1]=f.w[i][1]/HH.m1[i]; } /****************************/ //Loop for (int gg=0;gg<Nz;gg++) { printf("gg =%d",gg); //Make F2 and Fretrieved variables to zero for(int i=0;i<N;i++) { F2.w[i][0]=0.; F2.w[i][1]=0.; Fretrieved.w[i][0]=0.; Fretrieved.w[i][1]=0.; } //Apply the matrix Forward Hankel for(int j=0;j<N;j++) for(int i=0;i<N;i++) { //F2.w[j][0]+=C[j*N+i]*F.w[i][0]; //F2.w[j][1]+=C[j*N+i]*F.w[i][1]; F2.w[j][0]+=HH.C[j*N+i]*F.w[i][0]; F2.w[j][1]+=HH.C[j*N+i]*F.w[i][1]; } //Prepare the real transformed function. In this case just to display //This gives f2 previous to the transformation for(int i=0;i<N;i++) { //f2.w[i][0]=F2.w[i][0]*m2[i]; //f2.w[i][1]=F2.w[i][1]*m2[i]; f2.w[i][0]=F2.w[i][0]*HH.m2[i]; f2.w[i][1]=F2.w[i][1]*HH.m2[i]; } //Multiply by the phase to make the propagation. for(int i=0;i<N;i++) { double aux0r=F2.w[i][0]; double aux0i=F2.w[i][1]; phi[i]=(sqrt(K*K - 2*pi*HH.v[i]*2*pi*HH.v[i]) - K)*gg*dz; //The phase F2.w[i][0]=( aux0r*cos(phi[i])-aux0i*sin(phi[i]) ); F2.w[i][1]=( aux0i*cos(phi[i])+aux0r*sin(phi[i]) ); } //Apply the matrix Backward Hankel Transform over F2 for(int j=0;j<N;j++) for(int i=0;i<N;i++) { Fretrieved.w[j][0]+=HH.C[j*N+i]*F2.w[i][0]; Fretrieved.w[j][1]+=HH.C[j*N+i]*F2.w[i][1]; } //Prepare the scaled functions f2 and fretrieved after the propagator and the back Hankel transform for(int i=0;i<N;i++) { f2.w[i][0]=F2.w[i][0]*HH.m2[i]; f2.w[i][1]=F2.w[i][1]*HH.m2[i]; fretrieved.w[i][0]=Fretrieved.w[i][0]*HH.m1[i]; fretrieved.w[i][1]=Fretrieved.w[i][1]*HH.m1[i]; } //Print the output of the function for(int i=0;i<N;i++) { fprintf(out3,"%e\n", (f2.w[i][0]*f2.w[i][0]+f2.w[i][1]*f2.w[i][1]) ); fprintf(out4,"%e\n", (fretrieved.w[i][0]*fretrieved.w[i][0]+fretrieved.w[i][1]*fretrieved.w[i][1]) ); fprintf(out5,"%e\n", // Fretrieved.w[i][1]);// (Fretrieved.w[i][0]*Fretrieved.w[i][0]+Fretrieved.w[i][1]*Fretrieved.w[i][1]) ); } } }
/** Hyperbolic tangent \ingroup complex \param[in] z Complex number \return \f$ \tanh z \f$*/ complex tanh(const complex& z) { return complex(gsl_complex_tanh(z.as_gsl_type())); }
constexpr complex<T> complex<T>::operator ~() const { return complex(re, -im); }
void XmlParser::handleStartElement(void) { if (DBG) std::cerr << "StartElement"; if (DBG) std::cerr << ", name: '" << STDR(mXml.name()) << "'" << std::endl; switch (mCurSection) { case IGNORED_SECTION: if (mXml.name() == "chemical_element") { mCurSection = ELEMENT_CONFIG_SECTION; mElement = new Element(); if (!mElement.isNull()) mElement->setProperty( Element::SYMBOL_PROPERTY, ATTR(mXml, symbol, StdString) ); } break; case ELEMENT_CONFIG_SECTION: if (mElement.isNull()) break; if (mXml.name() == "name") { mElement->setProperty( Element::NAME_PROPERTY, ATTR(mXml, val, StdString) ); } else if (mXml.name() == "abundance") { mElement->setProperty( Element::ABUNDANCE_PROPERTY, ATTR(mXml, val, Double) ); } else if (mXml.name() == "atomic_weight") { mElement->setProperty( Element::ATOMIC_MASS_PROPERTY, ATTR(mXml, val, Double) ); } else if (mXml.name() == "nucleons") { mElement->setProperty( Element::NUCLEONS_PROPERTY, ATTR(mXml, val, Int) ); } else if (mXml.name() == "electrons") { mElement->setProperty( Element::ELECTRONS_PROPERTY, ATTR(mXml, val, Int) ); } else if (mXml.name() == "neutron_scattering_length") { mCurSection = NS_L_SECTION; } else if (mXml.name() == "neutron_scattering_cross_section") { mCurSection = NS_C_SECTION; } else if (mXml.name() == "xray_scattering_anomalous_coefficients") { mCurSection = XRAY_COEFFICIENTS_SECTION; } break; case NS_L_SECTION: if (mElement.isNull()) break; if (mXml.name() == "coherent") { mElement->setProperty( Element::NS_L_COHERENT_PROPERTY, complex( ATTR(mXml, re, Double), ATTR(mXml, im, Double) )); } else if (mXml.name() == "incoherent") { mElement->setProperty( Element::NS_L_INCOHERENT_PROPERTY, complex( ATTR(mXml, re, Double), ATTR(mXml, im, Double) )); } break; case NS_C_SECTION: if (mElement.isNull()) break; if (mXml.name() == "coherent") { mElement->setProperty( Element::NS_CS_COHERENT_PROPERTY, ATTR(mXml, re, Double)); } else if (mXml.name() == "incoherent") { mElement->setProperty( Element::NS_CS_INCOHERENT_PROPERTY, ATTR(mXml, re, Double)); } else if (mXml.name() == "total") { mElement->setProperty( Element::NS_CS_TOTAL_PROPERTY, ATTR(mXml, val, Double) ); } else if (mXml.name() == "absorption") { mElement->setProperty( Element::NS_CS_ABSORPTION_PROPERTY, ATTR(mXml, val, Double) ); } break; case XRAY_COEFFICIENTS_SECTION: if (mElement.isNull()) break; if (mXml.name() == "ev") { mElement->addXrayCoefficient( ATTR(mXml, val, Double), ATTR(mXml, fp, Double), ATTR(mXml, fpp, Double) ); } break; default: break; } }