/* re-send everything from last acked to last sent */ void alarmHandler(int sig){ struct itimerval tout_val; node* curWindow; int i; time_t curTime; double dif; signal(SIGALRM,alarmHandler); tout_val.it_interval.tv_sec = 0; tout_val.it_interval.tv_usec = 0; tout_val.it_value.tv_sec = 0; tout_val.it_value.tv_usec = 100000; // 100 ms curWindow = windowSets.headp; //traverse windowSets for(i = 0; i < windowSets.length; i++){ chunkEle* cep = (chunkEle*) curWindow->data; // which means not all packets have been acked yet if(cep->inProgress == 1){ time(&curTime); dif = difftime(curTime,cep->afterLastAckedTime); if(dif >= timeout){ sendAfterLastAcked(cep); time(&cep->afterLastAckedTime); //cut ssthresh in half and set window 1 cep->ssthresh = halve(cep->windowSize); cep->windowSize = 1; cep->mode = SLOWSTART; } //reset rtt timer dif = difftime(curTime,cep->rttCounter); if(dif >= cep->fromThisPeer->rtt){ cep->haveACK = 0; cep->rttCounter = curTime; } } curWindow = curWindow->prevp; } setitimer(ITIMER_REAL, &tout_val,0); setitimer(ITIMER_REAL, &tout_val,0); time(&curTime); dif = difftime(curTime, getRetryTimer); if((getRetryTimer != 0) && (dif >= (double)20.0)){ printf("20 timer went off!\n"); sendPendingGetRequest(&chunkList, sock); time(&getRetryTimer); } return; }
int func(int b,int n) { if (n == 0) { return 1; } else { return iseven( n ) ? func ( square(b) , halve( n ) ) : b * func( b , n-1 ) ; } }
int main() { int i, j, k, t; memset(a, 0, sizeof(a)); a[0] = 1; n = 0; for (i = 0; i <= 10000; i++, halve()) { for (j = k = 0; j < 5; j++) k = k * 10 + a[j]; rd[i] = (k + 5) / 10; re[i] = n; } for (scanf("%d", &t); t-- > 0 && scanf("%d", &n) == 1;) printf("2^-%d = %d.%.3dE-%d\n", n, rd[n] / 1000, rd[n] % 1000, re[n]); return 0; }
void process_inbound_udp(int sock) { #define BUFLEN 1500 struct sockaddr_in from; socklen_t fromlen; char buf[BUFLEN]; fromlen = sizeof(from); spiffy_recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *) &from, &fromlen); //recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *) &from, &fromlen); packetHead* pHead = (packetHead* )buf; peerEle* peer = resolvePeer(from, peerList); switch(pHead->type){ case WHOHAS:{ printf("Recieve WHOHAS request\n"); void* ihavep = ihaveCons(buf, &haschunkList); if( ihavep!= NULL){ unsigned int bufSize = ((packetHead *)ihavep)->packLen; spiffy_sendto(sock, ihavep, bufSize, 0, (struct sockaddr *) &from, fromlen); //sendto(sock, ihavep, bufSize, 0, (struct sockaddr *) &from, fromlen); time(&start); } break; } case IHAVE:{ printf("Receieve IHAVE request\n"); printf("packet length is %u\n", pHead->packLen); char tmp[sizeofHash]; memcpy(tmp, (buf+20), sizeofHash); printf("chunk hash: %s\n", tmp); peerEle* thisPeer = resolvePeer(from, peerList); time_t finish; time(&finish); if( thisPeer == NULL ){ printf("RESOLVE FAILED\n"); } thisPeer->rtt = difftime(finish, start); AddResponses(thisPeer, buf, &chunkList, sock); printChunkList(chunkList); break; } case GET:{ printf("Receive GET Request\n"); printf("packet length is %u\n", pHead->packLen); //check for free connections printf("maxcon:%d id: %d numout: %d\n",maxCon,mePeer->id,mePeer->numOut); if(mePeer->numOut == maxCon){ chunkEle* dcp = lookupChunkHash((buf+20),&haschunkList); void* deniedp = deniedCons(dcp->chunkId); spiffy_sendto(sock, deniedp, headerSize, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr)); //sendto(sock, deniedp, headerSize, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr)); break; } chunkEle* thisWindow = buildNewWindow(&windowSets, &haschunkList, peer, masterDataFilePath, buf); // send first element, no need to clean or fill sendWindow(thisWindow, sock); mePeer->numOut++;//increase num break; } case DATA:{ unsigned int bufSize = ((packetHead *)buf)->packLen; void* newBuf = malloc(bufSize); memcpy(newBuf,buf,bufSize); chunkEle* cep = resolveChunk(peer, chunkList); getRetryTimer = 0; // reject packets from finished chunk if(cep && (cep->chunkId != ((packetHead*)buf)->ackNum)){ printf("Wrong chunk!\n"); break; } orderedAdd(cep,newBuf); printf("Receive data packet %d, with size %d \n", ((packetHead *)buf)->seqNum, ((packetHead *)buf)->packLen - headerSize); findMex(cep); printPacketList(cep->packetList); void* packet = ackCons(cep->nextExpectedSeq - 1); printf("Send Ack %d\n", cep->nextExpectedSeq - 1); spiffy_sendto(sock, packet, headerSize, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr)); //sendto(sock, packet, headerSize, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr)); printf("bytes read for hash %s : %d\n",cep->chunkHash, cep->bytesRead); //check if finish receiving the whole chunk if(cep->bytesRead == chunkSize ){ if ( cep->fromThisPeer->inUse == 1){ printf("Sucessfully receive the chunk %s\n", cep->chunkHash); chunkList.finished ++; cep->fromThisPeer->inUse = 0; cep->inProgress = 0; mePeer->numOut--; } printf("FINISHED %d\n", chunkList.finished); if(chunkList.finished == chunkList.length){ printf("Finish receiving the whole file\n"); // merge the chunks and write to the corresponding output file buildOuputFile(outputFile, &chunkList); fclose(outputFile); printf("GOT %s\n",outputFilePath); }else{ // try to send rest of pending get requests // they are deferred to make sure no concurrent downloading from the same peer sendPendingGetRequest(&chunkList, sock); } } break; } case ACK:{ chunkEle* cep = resolveChunk(peer, windowSets); if ( cep->inProgress == 0){ break; } //check which wether SLOWSTART or CONGAVOID if(cep->mode == SLOWSTART){ cep->windowSize++; printf("####in slow start\n"); } else printf("@@@@in cong avoid rtt: %f\n",cep->fromThisPeer->rtt); //check if enough to enter cong avoid if(cep->windowSize == cep->ssthresh) cep->mode = CONGAVOID; //check for no ack in rtt if((cep->mode == CONGAVOID) && (cep->haveACK == 0)){ cep->windowSize++; printf("IIIIincreasing in cong avoid\n"); } //set ack cep->haveACK = 1; printf("Receive Ack %d\n", *(int*)(buf+12)); if( (cep->lastAcked) && ((packetHead* )(cep->lastAcked->data))->seqNum == ((packetHead* )(buf))->ackNum ){ cep->lastAckedCount ++; printf("Incrementing last ack counter for %d to %d\n", ((packetHead* )(buf))->ackNum,cep->lastAckedCount); if( cep->lastAckedCount == 3 ){ //cut ssthresh in half and set window 1 cep->ssthresh = halve(cep->windowSize); cep->windowSize = 1; cep->mode = SLOWSTART; //reset this ones time time(&cep->afterLastAckedTime); //try retrasmit cep->lastAckedCount = 0; node* retry = cep->lastAcked->prevp; printf("Retramsmit packet %d\n", ((packetHead* )retry->data)->seqNum); spiffy_sendto(sock, retry->data, ((packetHead* )retry->data)->packLen, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr)); break; } }else{ cep->lastAcked = resolveLastPacketAcked( ((packetHead*)buf)->ackNum, cep); cleanList(cep); time(&cep->afterLastAckedTime); } //check if finish sending the whole chunk if( cep->bytesRead == chunkSize ){ printf("Successfully send the chunk %s\n", cep->chunkHash); if(cep->lastAcked == cep->lastSent){ // some clear below printf("All sent packets have been acked\n"); cep->inProgress = 0; mePeer->numOut--; }else{ printf("lastAcked:%d, lastSent:%d\n",((packetHead*)cep->lastAcked->data)->seqNum, ((packetHead*)cep->lastSent->data)->seqNum ); } }else{ cleanList(cep); fillWindow(cep); sendWindow(cep, sock); } printPacketList(cep->packetList); printf("window: %d, ssthresh: %d, mode: %d\n",cep->windowSize, cep->ssthresh, cep->mode); break; } case DENIED:{ printf("Received a denied ack!\n"); chunkEle* cep = resolveChunk(peer, chunkList); //fclose(outputFile); time(&getRetryTimer); cep->fromThisPeer->inUse = 0; break; } } printf("PROCESS_INBOUND_UDP SKELETON -- replace!\n" "Incoming message from %s:%d\n%s\n\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port), buf); }
/* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: calc_PR(void) /* ---------------------------------------------------------------------- */ /* Calculate fugacity and fugacity coefficient for gas pressures if critical T and P are defined. 1) Solve molar volume V_m or total pressure P from Peng-Robinson's EOS: P = R * T / (V_m - b) - a * aa / (V_m^2 + 2 * b * V_m - b^2) a = 0.457235 * (R * T_c)^2 / P_c b = 0.077796 * R * T_c / P_c aa = (1 + kk * (1 - T_r^0.5))^2 kk = 0.37464 + 1.54226 * omega - 0.26992 * omega^2 T_r = T / T_c multicomponent gas phase: use: b_sum = Sum(x_i * b), x_i is mole-fraction a_aa_sum = Sum_i( Sum_j(x_i * x_j * (a_i * aa_i * a_j * aa_j)^0.5) ) 2) Find the fugacity coefficient phi for gas i: log(phi_i) = B_ratio * (z - 1) - log(z - B) + A / (2.8284 * B) * (B_ratio - 2 / a_aa_sum * a_aa_sum2) *\ log((z + 2.4142 * B) / (z - 0.4142 * B)) B_ratio = b_i / b_sum A = a_aa_sum * P / R_TK^2 B = b_sum * P / R_TK a_aa_sum2 = Sum_j(x_j * (a_aa_i * a_aa_j)^0.5 3) correct the solubility of gas i with: pr_si_f = log10(phi_i) - Delta_V_i * (P - 1) / (2.303 * R * TK); */ { LDBLE T_c, P_c; LDBLE A, B, B_r, /*b2,*/ kk, oo, a_aa, T_r; LDBLE m_sum, /*b_sum, a_aa_sum,*/ a_aa_sum2; LDBLE phi; LDBLE /*R_TK,*/ R = R_LITER_ATM; /* L atm / (K mol) */ LDBLE r3[4], r3_12, rp, rp3, rq, rz, ri, ri1, one_3 = 0.33333333333333333; LDBLE disct, vinit, v1, ddp, dp_dv, dp_dv2; int it; struct phase *phase_ptr; LDBLE V_m = 0, P = 0; LDBLE TK = tk_x; bool halved; R_TK = R * TK; m_sum = b_sum = a_aa_sum = 0.0; size_t i; if (gas_unknowns.size() == 0) { error_msg("No gas unknowns.", STOP); } cxxGasPhase * gas_phase_ptr = use.Get_gas_phase_ptr(); for (i = 0; i < gas_unknowns.size(); i++) { m_sum += gas_unknowns[i]->moles; phase_ptr = gas_unknowns[i]->phase; if (phase_ptr->t_c == 0.0 || phase_ptr->p_c == 0.0) error_msg("Cannot calculate a mixture of ideal and Peng_Robinson gases,\n please define Tc and Pc for the active gases in PHASES.", STOP); //continue; if (!phase_ptr->pr_a) { T_c = phase_ptr->t_c; P_c = phase_ptr->p_c; phase_ptr->pr_a = 0.457235 * R * R * T_c * T_c / P_c; phase_ptr->pr_b = 0.077796 * R * T_c / P_c; T_r = TK / T_c; oo = phase_ptr->omega; kk = 0.37464 + oo * (1.54226 - 0.26992 * oo); phase_ptr->pr_alpha = pow(1 + kk * (1 - sqrt(T_r)), 2); phase_ptr->pr_tk = TK; phase_ptr->pr_in = true; } if (phase_ptr->pr_tk != TK) { T_r = TK / phase_ptr->t_c; oo = phase_ptr->omega; kk = 0.37464 + oo * (1.54226 - 0.26992 * oo); phase_ptr->pr_alpha = pow(1 + kk * (1 - sqrt(T_r)), 2); phase_ptr->pr_tk = TK; phase_ptr->pr_in = true; } } if (m_sum == 0) return (OK); gas_phase_ptr->Set_v_m(gas_phase_ptr->Get_volume() / m_sum); for (i = 0; i < gas_unknowns.size(); i++) { phase_ptr = gas_unknowns[i]->phase; phase_ptr->fraction_x = gas_unknowns[i]->moles / m_sum; // phase_ptr->fraction_x updated } for (i = 0; i < gas_unknowns.size(); i++) { a_aa_sum2 = 0.0; phase_ptr = gas_unknowns[i]->phase; //if (phase_ptr->t_c == 0.0 || phase_ptr->p_c == 0.0) // continue; b_sum += phase_ptr->fraction_x * phase_ptr->pr_b; size_t i1; struct phase *phase_ptr1; for (i1 = 0; i1 < gas_unknowns.size(); i1++) { phase_ptr1 = gas_unknowns[i1]->phase; //if (phase_ptr1->t_c == 0.0 || phase_ptr1->p_c == 0.0) // continue; if (phase_ptr1->fraction_x == 0) continue; a_aa = sqrt(phase_ptr->pr_a * phase_ptr->pr_alpha * phase_ptr1->pr_a * phase_ptr1->pr_alpha); if (!strcmp(phase_ptr->name, "H2O(g)")) { if (!strcmp(phase_ptr1->name, "CO2(g)")) a_aa *= 0.81; // Soreide and Whitson, 1992, FPE 77, 217 else if (!strcmp(phase_ptr1->name, "H2S(g)")) a_aa *= 0.81; else if (!strcmp(phase_ptr1->name, "CH4(g)")) a_aa *= 0.51; else if (!strcmp(phase_ptr1->name, "N2(g)")) a_aa *= 0.51; } if (!strcmp(phase_ptr1->name, "H2O(g)")) { if (!strcmp(phase_ptr->name, "CO2(g)")) a_aa *= 0.81; else if (!strcmp(phase_ptr->name, "H2S(g)")) a_aa *= 0.81; else if (!strcmp(phase_ptr->name, "CH4(g)")) a_aa *= 0.51; else if (!strcmp(phase_ptr->name, "N2(g)")) a_aa *= 0.51; } a_aa_sum += phase_ptr->fraction_x * phase_ptr1->fraction_x * a_aa; a_aa_sum2 += phase_ptr1->fraction_x * a_aa; } phase_ptr->pr_aa_sum2 = a_aa_sum2; } b2 = b_sum * b_sum; if (gas_phase_ptr->Get_type() == cxxGasPhase::GP_VOLUME) { V_m = gas_phase_ptr->Get_volume() / m_sum; P = 0.0; while (P <= 0) { P = R_TK / (V_m - b_sum) - a_aa_sum / (V_m * (V_m + 2 * b_sum) - b2); if (P <= 0.0) { V_m *= 2.0; //a_aa_sum /= 2.0; } } if (iterations > 0 && P < 150 && V_m < 1.01) { // check for 3-roots... r3[1] = b_sum - R_TK / P; r3[2] = -3.0 * b2 + (a_aa_sum - R_TK * 2.0 * b_sum) / P; r3[3] = b2 * b_sum + (R_TK * b2 - b_sum * a_aa_sum) / P; // the discriminant of the cubic eqn... disct = 18. * r3[1] * r3[2] * r3[3] - 4. * pow(r3[1], 3) * r3[3] + r3[1] * r3[1] * r3[2] * r3[2] - 4. * pow(r3[2], 3) - 27. * r3[3] * r3[3]; if (disct > 0) { // 3-roots, find the largest P... it = 0; halved = false; ddp = 1e-9; v1 = vinit = 0.729; dp_dv = f_Vm(v1, this); while (fabs(dp_dv) > 1e-11 && it < 40) { it +=1; dp_dv2 = f_Vm(v1 - ddp, this); v1 -= (dp_dv * ddp / (dp_dv - dp_dv2)); if (!halved && (v1 > vinit || v1 < 0.03)) { if (vinit > 0.329) vinit -= 0.1; else vinit -=0.05; if (vinit < 0.03) { vinit = halve(f_Vm, 0.03, 1.0, 1e-3); if (f_Vm(vinit - 2e-3, this) < 0) vinit = halve(f_Vm, vinit + 2e-3, 1.0, 1e-3); halved = true; } v1 = vinit; } dp_dv = f_Vm(v1, this); if (fabs(dp_dv) < 1e-11) { if (f_Vm(v1 - 1e-4, this) < 0) { v1 = halve(f_Vm, v1 + 1e-4, 1.0, 1e-3); dp_dv = f_Vm(v1, this); } } } if (it == 40) { // accept a (possible) whobble in the curve... // error_msg("No convergence when calculating P in Peng-Robinson.", STOP); } if (V_m < v1 && it < 40) P = R_TK / (v1 - b_sum) - a_aa_sum / (v1 * (v1 + 2 * b_sum) - b2); } } if (P <= 0) // iterations = -1 P = 1.; gas_phase_ptr->Set_total_p(P); // phase_ptr->total_p updated gas_phase_ptr->Set_v_m(V_m); } else { assert(false); P = gas_phase_ptr->Get_total_p(); r3[1] = b_sum - R_TK / P; r3_12 = r3[1] * r3[1]; r3[2] = -3.0 * b2 + (a_aa_sum - R_TK * 2.0 * b_sum) / P; r3[3] = b2 * b_sum + (R_TK * b2 - b_sum * a_aa_sum) / P; // solve t^3 + rp*t + rq = 0. // molar volume V_m = t - r3[1] / 3... rp = r3[2] - r3_12 / 3; rp3 = rp * rp * rp; rq = (2.0 * r3_12 * r3[1] - 9.0 * r3[1] * r3[2]) / 27 + r3[3]; rz = rq * rq / 4 + rp3 / 27; if (rz >= 0) // Cardono's method... { ri = sqrt(rz); if (ri + rq / 2 <= 0) { V_m = pow(ri - rq / 2, one_3) + pow(- ri - rq / 2, one_3) - r3[1] / 3; } else { ri = - pow(ri + rq / 2, one_3); V_m = ri - rp / (3.0 * ri) - r3[1] / 3; } } else // use complex plane... { ri = sqrt(- rp3 / 27); // rp < 0 ri1 = acos(- rq / 2 / ri); V_m = 2.0 * pow(ri, one_3) * cos(ri1 / 3) - r3[1] / 3; } gas_phase_ptr->Set_v_m(V_m); // phase_ptr->fraction_x updated } // calculate the fugacity coefficients... for (i = 0; i < gas_unknowns.size(); i++) { phase_ptr = gas_unknowns[i]->phase; if (phase_ptr->fraction_x == 0.0) { phase_ptr->pr_p = 0; phase_ptr->pr_phi = 1; phase_ptr->pr_si_f = 0.0; //phase_ptr->logk[vm0] = 0.0; // *** continue; } phase_ptr->pr_p = phase_ptr->fraction_x * P; //if (phase_ptr->t_c == 0.0 || phase_ptr->p_c == 0.0) //{ // phase_ptr->pr_phi = 1; // continue; //} rz = P * V_m / R_TK; A = a_aa_sum * P / (R_TK * R_TK); B = b_sum * P / R_TK; B_r = phase_ptr->pr_b / b_sum; if (rz > B) { phi = B_r * (rz - 1) - log(rz - B) + A / (2.828427 * B) * (B_r - 2.0 * phase_ptr->pr_aa_sum2 / a_aa_sum) * log((rz + 2.41421356 * B) / (rz - 0.41421356 * B)); if (phi > 4.44) phi = 4.44; } else phi = -3.0; // fugacity coefficient > 0.05 phase_ptr->pr_phi = exp(phi); phase_ptr->pr_si_f = phi / LOG_10; // pr_si_f updated // **** //phase_ptr->logk[vm0] = V_m * 1e3 * phase_ptr->fraction_x; phase_ptr->pr_in = true; } return (V_m); }