source:
issm/oecreview/Archive/13393-13976/ISSM-13568-13569.diff@
27230
Last change on this file since 27230 was 13980, checked in by , 12 years ago | |
---|---|
File size: 5.8 KB |
-
../trunk-jpl/src/c/shared/Sorting/binary_search.cpp
10 10 11 11 #include <stdio.h> 12 12 13 int binary_search(int* poffset,int target,int* sorted_integers,int num_integers){ 13 int binary_search(int* poffset,int target,int* sorted_integers,int num_integers){ /*{{{*/ 14 14 15 15 /*output: */ 16 16 int offset; //offset, if found … … 62 62 63 63 /*Return result: */ 64 64 return found; 65 } 65 } /*}}}*/ 66 int binary_search(int* poffset,double target,double* sorted_doubles,int num_doubles){ /*{{{*/ 67 68 /*output: */ 69 int offset=0; 70 int found=0; /*found=0: not found. 71 found=1: found, and target is == to value at offset 72 found=2: found, and target is > to value at offset and < to value at offset+1 73 */ 74 75 /*intermediary: */ 76 double* beg=NULL; 77 double* end=NULL; 78 double* mid=NULL; 79 80 // point to beginning and end of the array 81 beg=sorted_doubles; 82 end=sorted_doubles+num_doubles; 83 mid=beg+(int)(num_doubles/2.0); 84 85 if (target<*beg){ 86 offset==-1; 87 found=0; 88 } 89 if (*beg==target){ 90 found=1; 91 offset=0; 92 } 93 else if(*(end-1)==target){ 94 found=1; 95 offset=num_doubles-1; 96 } 97 else{ 98 while((beg <= end) && !( target>=*mid && target<*(mid+1)) ){ 99 // is the target in lower or upper half? 100 if (target < *mid) { 101 end = mid - 1; //new end 102 mid = beg + (end-beg)/2; //new middle 103 } 104 else { 105 beg = mid + 1; //new beginning 106 mid = beg + (end-beg)/2; //new middle 107 } 108 } 109 110 //did we find the target? 111 if( target>*mid && target<*(mid+1)){ 112 found=2; 113 offset=mid-sorted_doubles; 114 } 115 else if( target==*mid){ 116 found=1; 117 offset=mid-sorted_doubles; 118 } 119 else { 120 found=0; 121 } 122 } 123 124 /*Assign output pointers:*/ 125 *poffset=offset; 126 127 /*Return result: */ 128 return found; 129 } /*}}}*/ -
../trunk-jpl/src/c/shared/Sorting/sorting.h
6 6 #define _SORTING_H_ 7 7 8 8 int binary_search(int* poffset,int target,int* sorted_integers,int num_integers); 9 int binary_search(int* poffset,double target,double* sorted_doubles,int num_doubles); 9 10 10 11 #endif //ifndef _SORTING_H_ -
../trunk-jpl/src/c/classes/objects/Inputs/TransientInput.cpp
425 425 /*FUNCTION TransientInput::GetTimeInput{{{*/ 426 426 Input* TransientInput::GetTimeInput(IssmDouble intime){ 427 427 428 int i,j;429 428 IssmDouble deltat; 430 429 IssmDouble alpha1,alpha2; 431 bool found=false;430 432 431 Input* input=NULL; 433 432 Input* input1=NULL; 434 433 Input* input2=NULL; 434 435 bool found=false; 436 int found_offset=0; 437 int offset; 435 438 436 439 /*Ok, we have the time, go through the timesteps, and figure out which interval we 437 440 *fall within. Then interpolate the values on this interval: */ 438 if(intime<this->timesteps[0]){ 439 /*get values for the first time: */ 440 input=(Input*)((Input*)this->inputs->GetObjectByOffset(0))->copy(); 441 found=true; 441 found_offset=binary_search(&offset,intime,this->timesteps,this->numtimesteps); 442 443 if (found_offset==0){ 444 if(intime<this->timesteps[0]){ 445 /*get values for the first time: */ 446 input=(Input*)((Input*)this->inputs->GetObjectByOffset(0))->copy(); 447 found=true; 448 } 449 else if(intime>this->timesteps[this->numtimesteps-1]){ 450 /*get values for the last time: */ 451 input=(Input*)((Input*)this->inputs->GetObjectByOffset(numtimesteps-1))->copy(); 452 found=true; 453 } 442 454 } 443 else if(intime>this->timesteps[this->numtimesteps-1]){444 /*get values for the last time: */445 input=(Input*)((Input*)this->inputs->GetObjectByOffset(numtimesteps-1))->copy();446 found=true;447 }448 455 else{ 449 /*Find which interval we fall within: */ 450 for(i=0;i<this->numtimesteps;i++){ 451 if(intime==this->timesteps[i]){ 452 /*We are right on one step time: */ 453 input=(Input*)((Input*)this->inputs->GetObjectByOffset(i))->copy(); 454 found=true; 455 break; //we are done with the time interpolation. 456 } 457 else{ 458 if(this->timesteps[i]<intime && intime<this->timesteps[i+1]){ 459 /*ok, we have the interval ]i:i+1[. Interpolate linearly for now: */ 460 deltat=this->timesteps[i+1]-this->timesteps[i]; 461 alpha2=(intime-this->timesteps[i])/deltat; 462 alpha1=(1.0-alpha2); 456 if (found_offset==1){ 457 /*intime is exactly equal to this->timesteps[offset]: */ 458 input=(Input*)((Input*)this->inputs->GetObjectByOffset(offset))->copy(); 459 found=true; 460 } 461 else if (found_offset==2){ 462 463 /*ok, we have the interval ]offset:offset+1[. Interpolate linearly for now: */ 464 deltat=this->timesteps[offset+1]-this->timesteps[offset]; 465 alpha2=(intime-this->timesteps[offset])/deltat; 466 alpha1=(1.0-alpha2); 463 467 464 input1=(Input*)this->inputs->GetObjectByOffset(i);465 input2=(Input*)this->inputs->GetObjectByOffset(i+1);468 input1=(Input*)this->inputs->GetObjectByOffset(offset); 469 input2=(Input*)this->inputs->GetObjectByOffset(offset+1); 466 470 467 468 469 471 input=(Input*)input1->copy(); 472 input->Scale(alpha1); 473 input->AXPY(input2,alpha2); 470 474 471 found=true; 472 break; 473 } 474 else continue; //keep looking on the next interval 475 } 475 found=true; 476 476 } 477 else _error_("binary_search returned the following value for found: " << found_offset<< "which is not supported yet!"); 477 478 } 479 478 480 if(!found)_error_("did not find time interval on which to interpolate forcing values!"); 479 481 480 482 /*Assign output pointer*/
Note:
See TracBrowser
for help on using the repository browser.