Commit 7c3a9cfe authored by Ruben Farinelli's avatar Ruben Farinelli
Browse files

Added routines for computing rest frame quantities from files

parent 87a65648
Loading
Loading
Loading
Loading

.clang-format

0 → 100644
+50 −0
Original line number Diff line number Diff line
Language: Cpp
AccessModifierOffset: -4  
AlignEscapedNewlinesLeft: true  
AlignTrailingComments: true  
AllowAllParametersOfDeclarationOnNextLine: true  
AllowShortBlocksOnASingleLine: true  
AllowShortFunctionsOnASingleLine: None  
AllowShortIfStatementsOnASingleLine: false  
AllowShortLoopsOnASingleLine: false  
AlwaysBreakBeforeMultilineStrings: true  
AlwaysBreakTemplateDeclarations: true  
BinPackParameters: false  
BreakBeforeBinaryOperators: false  
BreakBeforeBraces: Allman  
BreakBeforeTernaryOperators: true  
BreakConstructorInitializersBeforeComma: false  
ColumnLimit: 100
CommentPragmas: ''  
ConstructorInitializerAllOnOneLineOrOnePerLine: true  
ConstructorInitializerIndentWidth: 4  
ContinuationIndentWidth: 4  
DerivePointerAlignment: false  
DisableFormat: false  
ExperimentalAutoDetectBinPacking: false  
IndentCaseLabels: false  
IndentWidth: 2  
IndentWrappedFunctionNames: false  
IndentFunctionDeclarationAfterType: false  
MaxEmptyLinesToKeep: 1  
KeepEmptyLinesAtTheStartOfBlocks: false  
NamespaceIndentation: None  
PenaltyBreakBeforeFirstCallParameter: 1  
PenaltyBreakComment: 300  
PenaltyBreakString: 1000  
PenaltyBreakFirstLessLess: 120  
PenaltyExcessCharacter: 1  
PenaltyReturnTypeOnItsOwnLine: 1000  
PointerAlignment: Left  
SpaceBeforeAssignmentOperators: true  
SpaceBeforeParens: ControlStatements  
SpaceInEmptyParentheses: false  
SpacesBeforeTrailingComments: 1  
SpacesInAngles: false  
SpacesInCStyleCastParentheses: false  
SpacesInContainerLiterals: true  
SpacesInParentheses: false  
Cpp11BracedListStyle: true  
Standard: Cpp11  
TabWidth: 4  
UseTab: Never 
+1 −1
Original line number Diff line number Diff line
gcc -O3 -Wall -o bl_pol main_program.c hcubature.c restframe_quantities.c polarization_angle.c numpar.c stringpar.c strrev.c trig_funct.c -lm
gcc -O3 -Wall -o bl_pol main_program.c hcubature.c restframe_quantities.c polarization_angle.c numpar.c stringpar.c strrev.c interpolate_trial.c trig_funct.c -lm 
+767 −701

File changed.

Preview size limit exceeded, changes collapsed.

+5 −2
Original line number Diff line number Diff line
@@ -16,8 +16,10 @@
#define PI 3.14159
#define enep 2.73

extern int FlagFile;
extern char* poltype;
extern char* simulfile;
extern char* simul1;



double blackbody(double energy, double ktbb);
@@ -51,3 +53,4 @@ double cot(double a);
double csc(double a);
double arctan(double yq, double xq);
double reduce_azim(double phi);
void Calculate_InterpolatedIntensity(double target_costheta, double* interpolated_intensity, double* interpolated_pdeg);
+115 −100
Original line number Diff line number Diff line
@@ -5,115 +5,130 @@
#include <string.h>

// Function for linear interpolation
void interpolate(double target_costheta, double *Array_costheta, double *Array_pdeg, double *Array_Intensity, int data_size,
                 double *interpolated_pdeg, double *interpolated_intensity) {

void interpolate(double target_costheta,
                 double* Array_costheta,
                 double* Array_pdeg,
                 double* Array_Intensity,
                 int data_size,
                 double* interpolated_pdeg,
                 double* interpolated_intensity)
{
  int i;
    for (i = 1; i < data_size; i++) {
        if (Array_costheta[i] >= target_costheta) {
  for (i = 0; i < data_size-1; i++)
  {
    if (target_costheta >= Array_costheta[i] && target_costheta <= Array_costheta[i+1])
    {

     //printf("%lf [%lf - %lf]\n", target_costheta, Array_costheta[i], Array_costheta[i+1]);

      *interpolated_pdeg = Array_pdeg[i - 1] + (target_costheta - Array_costheta[i - 1]) *
                                                   (Array_pdeg[i] - Array_pdeg[i - 1]) /
                                                   (Array_costheta[i] - Array_costheta[i - 1]);
            *interpolated_intensity = Array_Intensity[i - 1] + (target_costheta - Array_costheta[i - 1]) *

      *interpolated_intensity =
          Array_Intensity[i - 1] + (target_costheta - Array_costheta[i - 1]) *
                                       (Array_Intensity[i] - Array_Intensity[i - 1]) /
                                       (Array_costheta[i] - Array_costheta[i - 1]);
                                                            
             //printf("Target costheta range is: %d \n", data_size);                                    
      break;
            
    }
  }

    //if (i == data_size) 
        //printf("Target costheta is out of range. %d \n", data_size);
}

void Calculate_InterpolatedIntensity(double target_costheta, double* interpolated_intensity, double* interpolated_pdeg)
{
  double* Array_costheta;
  double* Array_pdeg;
  double* Array_Intensity;

double* Calculate_InterpolatedIntensity(double target_costheta, char* simul1) {
    double *Array_costheta = NULL;
    double *Array_pdeg = NULL;
    double *Array_Intensity = NULL;

  int tot_lines = 0;
  int ii;
  char line[50];

  FILE* file;
    //char filename[100] = *simul1; // Replace "data.dat" with your actual .dat file name
    //size_t length = strlen(simulfile);
    //char* filename = (char*)malloc(length + 1);
    //strcpy(filename, simulfile);


    // Open the file in read mode
  file = fopen(simul1, "r");
    if (file == NULL) {
  if (file == NULL)
  {
    printf("Error opening file '%s'. Please check the file name and location.\n", simul1);
        return 1;
    exit(1);
  }

    // Read the data and store it in dynamically allocated arrays
    int data_size = 0;
    double costheta, pdeg, intensity;
    while (fscanf(file, "%lf %lf %lf", &costheta, &pdeg, &intensity) == 3) {
        // Dynamically allocate memory for each array
        Array_costheta = (double *)realloc(Array_costheta, (data_size + 1) * sizeof(double));
        Array_pdeg = (double *)realloc(Array_pdeg, (data_size + 1) * sizeof(double));
        Array_Intensity = (double *)realloc(Array_Intensity, (data_size + 1) * sizeof(double));

        // Check if memory allocation was successful
        if (Array_costheta == NULL || Array_pdeg == NULL || Array_Intensity == NULL) {
            printf("Memory allocation failed. Exiting...\n");
  /*====================================================*/
  /*First determine the number of lines of the file*/
  /*====================================================*/

  while (!feof(file))
  {
    if (fgets(line, 50, file) == NULL)
    {
      break;
    }
    else if (strstr(line, "#") != NULL)
    {
      continue;
    }
    else if (strstr(line, "!") != NULL)
    {
      continue;
    }
    else
    {
      tot_lines++;
    }
  }

  rewind(file);

            // Free the memory before exiting

  //printf("Number of lines %d\n", tot_lines);
  Array_costheta = (double*) malloc(tot_lines * sizeof(double));
  Array_pdeg = (double*) malloc(tot_lines * sizeof(double));
  Array_Intensity = (double*)malloc(tot_lines * sizeof(double));

  if (Array_costheta == NULL || Array_pdeg == NULL || Array_Intensity == NULL)
  {
    printf("Memory allocation failed. Exiting...\n");
    free(Array_costheta);
    free(Array_pdeg);
    free(Array_Intensity);
    fclose(file);
            return 1;
    exit(1);
  }

        // Store the data into arrays
        Array_costheta[data_size] = costheta;
        Array_pdeg[data_size] = pdeg;
        Array_Intensity[data_size] = intensity;

        data_size++;

  ii=0;
  while (!feof(file))
  {
	  if (fgets(line, 50, file) == NULL)
	  {
	      break;
	  }
	  else if (strstr(line, "!") != NULL)
	  {
		  continue;
	  }
	  else
	  {
		 sscanf(line, "%lg %lg %lg\n", &Array_costheta[ii], &Array_pdeg[ii], &Array_Intensity[ii]);
		 //printf("CIAO %lf %lf %lf\n", Array_costheta[ii], Array_pdeg[ii], Array_Intensity[ii]);

		  ii++;
	  }
  }

  //exit(1);


  // Close the file
  fclose(file);

    // Print the data (optional, you can remove this part if you don't need it)
    //printf("Array_costheta\tArray_pdeg\tArray_Intensity\n");
    //for (int i = 0; i < data_size; i++) {
        //printf("%lf\t%lf\t%lf\n", Array_costheta[i], Array_pdeg[i], Array_Intensity[i]);
    //}

    // Get the target costheta as input from the user
    //double target_costheta;
    //printf("\nEnter the target costheta for interpolation: ");
    //scanf("%lf", &target_costheta);

    // Perform linear interpolation
    double interpolated_pdeg = 0.0;
    double interpolated_intensity = 0.0;
    interpolate(target_costheta, Array_costheta, Array_pdeg, Array_Intensity, data_size, &interpolated_pdeg,
                &interpolated_intensity);

    // Print the interpolated values
    //printf("\nInterpolated values:\n");
    //printf("Costheta: %lf\n", target_costheta);
    //printf("Interpolated pdeg: %lf\n", interpolated_pdeg);
    //printf("Interpolated intensity: %lf\n", interpolated_intensity);

    // Free the dynamically allocated memory
  interpolate(target_costheta, Array_costheta, Array_pdeg, Array_Intensity, tot_lines,
              interpolated_pdeg, interpolated_intensity);

  free(Array_costheta);
  free(Array_pdeg);
  free(Array_Intensity);
    
    //Array of the interpolated intensity and interpolated PD respectively
    
    double I_PD[2] = {interpolated_intensity,interpolated_pdeg};
    
    

    return I_PD;
}
Loading