int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){ char *flatten; int numlen; if (is_a_phone_number(username)){ flatten=flatten_number(username); ms_message("Flattened number is '%s'",flatten); numlen=strlen(flatten); if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){ ms_message("No need to add a prefix"); /* prefix is already there */ copy_result(flatten,result,result_len,proxy->dial_escape_plus); ms_free(flatten); return 0; }else if (proxy->dial_prefix && proxy->dial_prefix[0]!='\0'){ char *prefixed; int skipped=0; ms_message("Need to prefix with %s",proxy->dial_prefix); if (numlen==10){ /*remove initial number before prepending prefix*/ skipped=1; } prefixed=append_prefix(flatten+skipped,proxy->dial_prefix); ms_free(flatten); copy_result(prefixed,result,result_len,proxy->dial_escape_plus); ms_free(prefixed); } }else strncpy(result,username,result_len); return 0; }
//------------------------------------------------------------------ simpleExport int cahnhilliard(double (*start_val_f)(double x), int gSize, double xMax, double xMin, double dt, double TMAX, double* out_p) { int i; double dx; doubleArray valArray, xArray, dArray, cubeArray, lptermArray, sumArray; allocate_double_array(&valArray, gSize); allocate_double_array(&xArray, gSize); allocate_double_array(&dArray, gSize); allocate_double_array(&cubeArray, gSize); allocate_double_array(&lptermArray, gSize); allocate_double_array(&sumArray, gSize); init_grid(&valArray, &xArray, (*start_val_f), gSize, xMax, xMin); dx = calc_dx(&xArray); do_cahn_hilliard(&valArray, &dArray, &cubeArray, &lptermArray, &sumArray, TMAX, dt, dx); copy_result(&valArray, gSize, out_p); return 7; }
int cache_find(char *u, wd_in *result){ int a; a=strlen(u)%CACHE_SIZE; if(strcmp(n[a].url,u)!=0) do{a=(a*5)%CACHE_SIZE;}while(n[a].count!=0 && strcmp(n[a].url,u)!=0); if(n[a].count==0) return 0; n[a].count++; n[a].ln.count++; lnode *p=&n[a].ln.next; while(p!=NULL && p->count < n[a].ln.count) p=p->next; if(p!=NULL && p->index!=n[a].ln.index){ if(n[a].ln.prev!=NULL){ n[a].ln.prev->next=n[a].ln.next; n[a].ln.next->prev=n[a].ln.prev; } if(p->prev!=NULL) { n[a].ln.prev=p->prev; n[a].ln.prev->next=&n[a].ln; } n[a].ln.next=p; p->prev=&n[a].ln; } copy_result(&n[a].w,result); printf("%s CACHE HIT at %d\n",u,a);fflush(stdout); return 1; }
RESULT *duplicate_result(RESULT *rp) { RESULT *result; LOG(LOG_DEBUG, "IN : duplicate_result()"); g_assert(rp != NULL); result = g_new0(RESULT, 1); copy_result(result, rp); LOG(LOG_DEBUG, "OUT : duplicate_result()"); return(result); }
//------------------------------------------------------------------ simpleExport int xvalues(double (*start_val_f)(double x), int gSize, double xMax, double xMin, double* out_p) { doubleArray valArray, xArray; allocate_double_array(&valArray, gSize); allocate_double_array(&xArray, gSize); init_grid(&valArray, &xArray, (*start_val_f), gSize, xMax, xMin); copy_result(&xArray, gSize, out_p); return 7; }
bool_t curl_op(char **arg, wd_in *result){ CURL *curl; CURLcode res; wd_in wdi; char *url = arg[0]; memset(&wdi, 0, sizeof(wdi)); curl = curl_easy_init(); if(NULL != curl) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &wdi); res = curl_easy_perform(curl); curl_easy_cleanup(curl); copy_result(&wdi,result); cache_insert(url, &wdi); return 1; } else { fprintf(stderr, "Error: could not get CURL handle.\n"); return 0; } }
//------------------------------------------------------------------ simpleExport int phasefieldcrystal(double (*start_val_f)(double x), int gSize, double xMax, double xMin, double dt, double TMAX, double epsilon, double* out_p) { int i; double dx; doubleArray valArray, xArray, dArray, cubeArray, lapOneArray, lapTwoArray, sumArray; allocate_double_array(&valArray, gSize); allocate_double_array(&xArray, gSize); allocate_double_array(&dArray, gSize); allocate_double_array(&cubeArray, gSize); allocate_double_array(&lapOneArray, gSize); allocate_double_array(&lapTwoArray, gSize); allocate_double_array(&sumArray, gSize); init_grid(&valArray, &xArray, (*start_val_f), gSize, xMax, xMin); dx = calc_dx(&xArray); do_phase_field_crystal(&valArray, &dArray, &cubeArray, &lapOneArray, &lapTwoArray, &sumArray, epsilon, TMAX, dt, dx); copy_result(&valArray, gSize, out_p); return 7; }
//------------------------------------------------------------------ simpleExport int diffusion(double (*start_val_f)(double x), int gSize, double xMax, double xMin, double dt, double TMAX, double* out_p) { int i; double dx; doubleArray valArray, xArray, dArray; allocate_double_array(&valArray, gSize); allocate_double_array(&xArray, gSize); allocate_double_array(&dArray, gSize); init_grid(&valArray, &xArray, (*start_val_f), gSize, xMax, xMin); dx = calc_dx(&xArray); do_diffusion(&valArray, &dArray, TMAX, dt, dx); copy_result(&valArray, gSize, out_p); return 7; }
void cache_insert(char *u, wd_in *result){ int a; if((cur_size+result->len) > MAX_CACHE){ while(cur_size+result->len > MAX_CACHE){ a=list_remove(); printf("Cache size exceeded. Evicting %s with count %d and size %u\n",n[a].url,n[a].count,n[a].w.len);fflush(stdout); if(a>=0){ strcpy(n[a].url,""); n[a].count=0; cur_size-=n[a].w.len; free(n[a].w.data); } } } a=strlen(u)%CACHE_SIZE; if(n[a].count!=0) do{a=(a*5)%CACHE_SIZE;}while(n[a].count!=0); n[a].count=1; list_insert(a); cur_size+=result->len; strcpy(n[a].url,u); n[a].w.data=malloc(result->len); copy_result(result,&n[a].w); //printf("%s %d\nPrinted\n",n[a].url,n[a].w.len);fflush(stdout); }
/* ************************************************************************* */ int main(int argc, char *argv[]) { struct GModule *module = NULL; N_solute_transport_data2d *data = NULL; N_geom_data *geom = NULL; N_les *les = NULL; N_les_callback_2d *call = NULL; struct Cell_head region; double error, sor; char *solver; int x, y, stat, i, maxit = 1; double loops = 1; N_array_2d *xcomp = NULL; N_array_2d *ycomp = NULL; N_array_2d *hc_x = NULL; N_array_2d *hc_y = NULL; N_array_2d *phead = NULL; double time_step, cfl, length, time_loops, time_sum; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("hydrology")); G_add_keyword(_("solute transport")); module->description = _("Numerical calculation program for transient, confined and unconfined " "solute transport in two dimensions"); /* Get parameters from user */ set_params(); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* Make sure that the current projection is not lat/long */ if ((G_projection() == PROJECTION_LL)) G_fatal_error(_("Lat/Long location is not supported by %s. Please reproject map first."), G_program_name()); /*Set the maximum iterations */ sscanf(param.maxit->answer, "%i", &(maxit)); /*Set the calculation error break criteria */ sscanf(param.error->answer, "%lf", &(error)); sscanf(param.sor->answer, "%lf", &(sor)); /*number of loops*/ sscanf(param.loops->answer, "%lf", &(loops)); /*Set the solver */ solver = param.solver->answer; if (strcmp(solver, G_MATH_SOLVER_DIRECT_LU) == 0 && !param.full_les->answer) G_fatal_error(_("The direct LU solver do not work with sparse matrices")); if (strcmp(solver, G_MATH_SOLVER_DIRECT_GAUSS) == 0 && !param.full_les->answer) G_fatal_error(_("The direct Gauss solver do not work with sparse matrices")); /*get the current region */ G_get_set_window(®ion); /*allocate the geometry structure for geometry and area calculation */ geom = N_init_geom_data_2d(®ion, geom); /*Set the function callback to the groundwater flow function */ call = N_alloc_les_callback_2d(); N_set_les_callback_2d_func(call, (*N_callback_solute_transport_2d)); /*solute_transport 2d */ /*Allocate the groundwater flow data structure */ data = N_alloc_solute_transport_data2d(geom->cols, geom->rows); /*Set the stabilizing scheme*/ if (strncmp("full", param.stab->answer, 4) == 0) { data->stab = N_UPWIND_FULL; } if (strncmp("exp", param.stab->answer, 3) == 0) { data->stab = N_UPWIND_EXP; } /*the dispersivity lengths*/ sscanf(param.al->answer, "%lf", &(data->al)); sscanf(param.at->answer, "%lf", &(data->at)); /*Set the calculation time */ sscanf(param.dt->answer, "%lf", &(data->dt)); /*read all input maps into the memory and take care of the * null values.*/ N_read_rast_to_array_2d(param.c->answer, data->c); N_convert_array_2d_null_to_zero(data->c); N_read_rast_to_array_2d(param.c->answer, data->c_start); N_convert_array_2d_null_to_zero(data->c_start); N_read_rast_to_array_2d(param.status->answer, data->status); N_convert_array_2d_null_to_zero(data->status); N_read_rast_to_array_2d(param.diff_x->answer, data->diff_x); N_convert_array_2d_null_to_zero(data->diff_x); N_read_rast_to_array_2d(param.diff_y->answer, data->diff_y); N_convert_array_2d_null_to_zero(data->diff_y); N_read_rast_to_array_2d(param.q->answer, data->q); N_convert_array_2d_null_to_zero(data->q); N_read_rast_to_array_2d(param.nf->answer, data->nf); N_convert_array_2d_null_to_zero(data->nf); N_read_rast_to_array_2d(param.cs->answer, data->cs); N_convert_array_2d_null_to_zero(data->cs); N_read_rast_to_array_2d(param.top->answer, data->top); N_convert_array_2d_null_to_zero(data->top); N_read_rast_to_array_2d(param.bottom->answer, data->bottom); N_convert_array_2d_null_to_zero(data->bottom); N_read_rast_to_array_2d(param.r->answer, data->R); N_convert_array_2d_null_to_zero(data->R); if(param.cin->answer) { N_read_rast_to_array_2d(param.cin->answer, data->cin); N_convert_array_2d_null_to_zero(data->cin); } /*initiate the values for velocity calculation*/ hc_x = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE); hc_x = N_read_rast_to_array_2d(param.hc_x->answer, hc_x); N_convert_array_2d_null_to_zero(hc_x); hc_y = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE); hc_y = N_read_rast_to_array_2d(param.hc_y->answer, hc_y); N_convert_array_2d_null_to_zero(hc_y); phead = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE); phead = N_read_rast_to_array_2d(param.phead->answer, phead); N_convert_array_2d_null_to_zero(phead); /* Set the inactive values to zero, to assure a no flow boundary */ for (y = 0; y < geom->rows; y++) { for (x = 0; x < geom->cols; x++) { stat = (int)N_get_array_2d_d_value(data->status, x, y); if (stat == N_CELL_INACTIVE) { /*only inactive cells */ N_put_array_2d_d_value(data->diff_x, x, y, 0); N_put_array_2d_d_value(data->diff_y, x, y, 0); N_put_array_2d_d_value(data->cs, x, y, 0); N_put_array_2d_d_value(data->q, x, y, 0); } } } /*compute the velocities */ N_math_array_2d(hc_x, data->nf, hc_x, N_ARRAY_DIV); N_math_array_2d(hc_y, data->nf, hc_y, N_ARRAY_DIV); N_compute_gradient_field_2d(phead, hc_x, hc_y, geom, data->grad); /*Now compute the dispersivity tensor*/ N_calc_solute_transport_disptensor_2d(data); /***************************************/ /*the Courant-Friedrichs-Lewy criteria */ /*Compute the correct time step */ if (geom->dx > geom->dy) length = geom->dx; else length = geom->dy; if (fabs(data->grad->max) > fabs(data->grad->min)) { cfl = (double)data->dt * fabs(data->grad->max) / length; time_step = 1*length / fabs(data->grad->max); } else { cfl = (double)data->dt * fabs(data->grad->min) / length; time_step = 1*length / fabs(data->grad->min); } G_message(_("The Courant-Friedrichs-Lewy criteria is %g it should be within [0:1]"), cfl); G_message(_("The largest stable time step is %g"), time_step); /*Set the number of inner loops and the time step*/ if (data->dt > time_step && param.cfl->answer) { /*safe the user time step */ time_sum = data->dt; time_loops = data->dt / time_step; time_loops = floor(time_loops) + 1; data->dt = data->dt / time_loops; G_message(_("Number of inner loops is %g"), time_loops); G_message(_("Time step for each loop %g"), data->dt); } else { if(data->dt > time_step) G_warning(_("The time step is to large: %gs. The largest time step should be of size %gs."), data->dt, time_step); time_loops = loops; data->dt = data->dt / loops; } N_free_array_2d(phead); N_free_array_2d(hc_x); N_free_array_2d(hc_y); /*Compute for each time step*/ for (i = 0; i < time_loops; i++) { G_message(_("Time step %i with time sum %g"), i + 1, (i + 1)*data->dt); /*assemble the linear equation system and solve it */ les = create_solve_les(geom, data, call, solver, maxit, error, sor); /* copy the result into the c array for output */ copy_result(data->status, data->c_start, les->x, ®ion, data->c, 1); N_convert_array_2d_null_to_zero(data->c_start); if (les) N_free_les(les); /*Set the start array*/ N_copy_array_2d(data->c, data->c_start); /*Set the transmission boundary*/ N_calc_solute_transport_transmission_2d(data); } /*write the result to the output file */ N_write_array_2d_to_rast(data->c, param.output->answer); /*Compute the the velocity field if required and write the result into three rast maps */ if (param.vector_x->answer || param.vector_y->answer) { xcomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE); ycomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE); N_compute_gradient_field_components_2d(data->grad, xcomp, ycomp); if (param.vector_x->answer) N_write_array_2d_to_rast(xcomp, param.vector_x->answer); if (param.vector_y->answer) N_write_array_2d_to_rast(ycomp, param.vector_y->answer); if (xcomp) N_free_array_2d(xcomp); if (ycomp) N_free_array_2d(ycomp); } if (data) N_free_solute_transport_data2d(data); if (geom) N_free_geom_data(geom); if (call) G_free(call); return (EXIT_SUCCESS); }
/* ************************************************************************* */ int main(int argc, char *argv[]) { struct GModule *module = NULL; N_gwflow_data2d *data = NULL; N_geom_data *geom = NULL; N_les *les = NULL; N_les_callback_2d *call = NULL; double *tmp_vect = NULL; struct Cell_head region; double error, sor, max_norm = 0, tmp; int maxit, i, inner_count = 0; char *solver; int x, y, stat; N_gradient_field_2d *field = NULL; N_array_2d *xcomp = NULL; N_array_2d *ycomp = NULL; char *buff = NULL; int with_river = 0, with_drain = 0; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, hydrology"); module->description = _("Numerical calculation program for transient, confined and unconfined groundwater flow in two dimensions."); /* Get parameters from user */ set_params(); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* Make sure that the current projection is not lat/long */ if ((G_projection() == PROJECTION_LL)) G_fatal_error(_("Lat/Long location is not supported by %s. Please reproject map first."), G_program_name()); /*Check the river parameters */ if (param.river_leak->answer == NULL && param.river_bed->answer == NULL && param.river_head->answer == NULL) { with_river = 0; } else if (param.river_leak->answer != NULL && param.river_bed->answer != NULL && param.river_head->answer != NULL) { with_river = 1; } else { G_fatal_error (_("Please provide river_head, river_leak and river_bed maps")); } /*Check the drainage parameters */ if (param.drain_leak->answer == NULL && param.drain_bed->answer == NULL) { with_drain = 0; } else if (param.drain_leak->answer != NULL && param.drain_bed->answer != NULL) { with_drain = 1; } else { G_fatal_error(_("Please provide drain_head and drain_leak maps")); } /*Set the maximum iterations */ sscanf(param.maxit->answer, "%i", &(maxit)); /*Set the calculation error break criteria */ sscanf(param.error->answer, "%lf", &(error)); sscanf(param.sor->answer, "%lf", &(sor)); /*set the solver */ solver = param.solver->answer; if (strcmp(solver, N_SOLVER_DIRECT_LU) == 0 && param.sparse->answer) G_fatal_error(_("The direct LU solver do not work with sparse matrices")); if (strcmp(solver, N_SOLVER_DIRECT_GAUSS) == 0 && param.sparse->answer) G_fatal_error(_("The direct Gauss solver do not work with sparse matrices")); if (strcmp(solver, N_SOLVER_DIRECT_CHOLESKY) == 0 && param.sparse->answer) G_fatal_error(_("The direct cholesky solver do not work with sparse matrices")); /*get the current region */ G_get_set_window(®ion); /*allocate the geometry structure for geometry and area calculation */ geom = N_init_geom_data_2d(®ion, geom); /*Set the function callback to the groundwater flow function */ call = N_alloc_les_callback_2d(); N_set_les_callback_2d_func(call, (*N_callback_gwflow_2d)); /*gwflow 2d */ /*Allocate the groundwater flow data structure */ data = N_alloc_gwflow_data2d(geom->cols, geom->rows, with_river, with_drain); /* set the groundwater type */ if (param.type->answer) { if (strncmp("unconfined", param.type->answer, 10) == 0) { data->gwtype = N_GW_UNCONFINED; } else { data->gwtype = N_GW_CONFINED; } } /*Set the calculation time */ sscanf(param.dt->answer, "%lf", &(data->dt)); G_message("Calculation time: %g", data->dt); /*read all input maps into the memory and take care of the * null values.*/ N_read_rast_to_array_2d(param.phead->answer, data->phead); N_convert_array_2d_null_to_zero(data->phead); N_copy_array_2d(data->phead, data->phead_start); N_read_rast_to_array_2d(param.status->answer, data->status); N_convert_array_2d_null_to_zero(data->status); N_read_rast_to_array_2d(param.hc_x->answer, data->hc_x); N_convert_array_2d_null_to_zero(data->hc_x); N_read_rast_to_array_2d(param.hc_y->answer, data->hc_y); N_convert_array_2d_null_to_zero(data->hc_y); N_read_rast_to_array_2d(param.s->answer, data->s); N_convert_array_2d_null_to_zero(data->s); N_read_rast_to_array_2d(param.top->answer, data->top); N_convert_array_2d_null_to_zero(data->top); N_read_rast_to_array_2d(param.bottom->answer, data->bottom); N_convert_array_2d_null_to_zero(data->bottom); /*river is optional */ if (with_river) { N_read_rast_to_array_2d(param.river_bed->answer, data->river_bed); N_read_rast_to_array_2d(param.river_head->answer, data->river_head); N_read_rast_to_array_2d(param.river_leak->answer, data->river_leak); N_convert_array_2d_null_to_zero(data->river_bed); N_convert_array_2d_null_to_zero(data->river_head); N_convert_array_2d_null_to_zero(data->river_leak); } /*drainage is optional */ if (with_drain) { N_read_rast_to_array_2d(param.drain_bed->answer, data->drain_bed); N_read_rast_to_array_2d(param.drain_leak->answer, data->drain_leak); N_convert_array_2d_null_to_zero(data->drain_bed); N_convert_array_2d_null_to_zero(data->drain_leak); } /*Recharge is optional */ if (param.r->answer) { N_read_rast_to_array_2d(param.r->answer, data->r); N_convert_array_2d_null_to_zero(data->r); } /*Sources or sinks are optional */ if (param.q->answer) { N_read_rast_to_array_2d(param.q->answer, data->q); N_convert_array_2d_null_to_zero(data->q); } /* Set the inactive values to zero, to assure a no flow boundary */ for (y = 0; y < geom->rows; y++) { for (x = 0; x < geom->cols; x++) { stat = N_get_array_2d_c_value(data->status, x, y); if (stat == N_CELL_INACTIVE) { /*only inactive cells */ N_put_array_2d_d_value(data->hc_x, x, y, 0); N_put_array_2d_d_value(data->hc_y, x, y, 0); N_put_array_2d_d_value(data->s, x, y, 0); N_put_array_2d_d_value(data->q, x, y, 0); } } } /*assemble the linear equation system and solve it */ les = create_solve_les(geom, data, call, solver, maxit, error, sor); /* copy the result into the phead array for output or unconfined calculation */ copy_result(data->status, data->phead_start, les->x, ®ion, data->phead); N_convert_array_2d_null_to_zero(data->phead); /****************************************************/ /*explicite calculation of free groundwater surface */ /****************************************************/ if (data->gwtype == N_GW_UNCONFINED) { /* allocate memory and copy the result into a new temporal vector */ if (!(tmp_vect = (double *)calloc(les->rows, sizeof(double)))) G_fatal_error(_("Out of memory")); /*copy data */ for (i = 0; i < les->rows; i++) tmp_vect[i] = les->x[i]; /*count the number of inner iterations */ inner_count = 0; do { G_message(_("Calculation of unconfined groundwater flow loop %i\n"), inner_count + 1); /* we will allocate a new les for each loop */ if (les) N_free_les(les); /*assemble the linear equation system and solve it */ les = create_solve_les(geom, data, call, solver, maxit, error, sor); /*calculate the maximum norm of the groundwater height difference */ tmp = 0; max_norm = 0; for (i = 0; i < les->rows; i++) { tmp = fabs(les->x[i] - tmp_vect[i]); if (max_norm < tmp) max_norm = tmp; /*copy the result */ tmp_vect[i] = les->x[i]; } G_message(_("Maximum difference between this and last increment: %g"), max_norm); /* copy the result into the phead array */ copy_result(data->status, data->phead_start, les->x, ®ion, data->phead); N_convert_array_2d_null_to_zero(data->phead); /**/ inner_count++; } while (max_norm > 0.01 && inner_count < 50); if (tmp_vect) free(tmp_vect); } /*write the result to the output file */ N_write_array_2d_to_rast(data->phead, param.output->answer); /*release the memory */ if (les) N_free_les(les); /*Compute the the velocity field if required and write the result into three rast maps */ if (param.vector->answer) { field = N_compute_gradient_field_2d(data->phead, data->hc_x, data->hc_y, geom, NULL); xcomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE); ycomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE); N_compute_gradient_field_components_2d(field, xcomp, ycomp); G_asprintf(&buff, "%s_x", param.vector->answer); N_write_array_2d_to_rast(xcomp, buff); G_asprintf(&buff, "%s_y", param.vector->answer); N_write_array_2d_to_rast(ycomp, buff); if (buff) G_free(buff); if (xcomp) N_free_array_2d(xcomp); if (ycomp) N_free_array_2d(ycomp); if (field) N_free_gradient_field_2d(field); } if (data) N_free_gwflow_data2d(data); if (geom) N_free_geom_data(geom); if (call) G_free(call); return (EXIT_SUCCESS); }