rw.c (10204B)
1 /* 2 * rw.c 3 */ 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <stdbool.h> 8 9 #define MAX_mat 100 // Assuming a maximum number of mat 10 #define MAX_LINE_LENGTH 256 // Assuming lines won't exceed this length 11 #define N 6 12 13 /* Structure for material properties */ 14 typedef struct { 15 double E1, E2, nu12, G12, 16 a1, a2, b1, b2, 17 Xt, Xc, Yt, Yc, S12, S32; 18 bool isotropic; 19 } MaterialProperties; 20 21 /* Structure for laminate properties */ 22 typedef struct { 23 int *mat_id; 24 double *thk; 25 double *ang; 26 int num_layers; 27 } Laminate; 28 29 void read_laminate_data 30 (const char *filename, Laminate *lam, bool print_details) 31 { 32 FILE *file; 33 char line[100]; // Assuming each line won't exceed 100 characters 34 int layer_count = 0; 35 double h = 0; 36 37 // Attempt to open the file 38 file = fopen(filename, "r"); 39 if (file == NULL) { 40 fprintf(stderr, "Error opening file %s\n", filename); 41 exit(1); 42 } 43 44 // Count the number of lines to determine num_layers 45 while (fgets(line, sizeof(line), file) != NULL) { 46 if (line[0] != '\n' && line[0] != '#') { // Ignore empty lines or comments 47 layer_count++; 48 } 49 } 50 51 // Allocate memory for the laminate structure 52 lam->num_layers = layer_count; 53 lam->mat_id = malloc(layer_count * sizeof(int)); 54 lam->thk = malloc(layer_count * sizeof(double)); 55 lam->ang = malloc(layer_count * sizeof(double)); 56 57 if (lam->mat_id == NULL || lam->thk == NULL || lam->ang == NULL) { 58 fprintf(stderr, "Memory allocation failed\n"); 59 fclose(file); 60 exit(1); 61 } 62 63 // Reset file pointer to beginning of file 64 rewind(file); 65 66 // Read the actual data 67 int i = 0; 68 while (fgets(line, sizeof(line), file) != NULL) { 69 if (line[0] != '\n' && line[0] != '#') { // Skip empty lines or comments 70 // Temporary variables to hold the parsed data from each line 71 int mat_id; 72 double thickness, angle; 73 74 // Use sscanf to parse the line 75 if (sscanf(line, "%d %lf %lf", 76 &mat_id, &thickness, &angle) == 3) { 77 lam->mat_id[i] = mat_id; 78 lam->thk[i] = thickness; 79 lam->ang[i] = angle; 80 i++; 81 } else { 82 fprintf(stderr, "Error reading line: %s\n", line); 83 // Optionally, you could choose to continue or break here 84 } 85 } 86 } 87 for(int i = 0; i < lam->num_layers; i++) h += lam->thk[i]; 88 89 90 // Print the laminate details if requested 91 if (print_details) { 92 printf("\n"); 93 printf("+------------------------+\n"); 94 printf("| Laminate Configuration |\n"); 95 printf("+------------------------+\n\n"); 96 printf("Number of Layers: %d\n", lam->num_layers); 97 printf("Laminate total thickness = %.4e\n", h); 98 printf("Layer | Thickness (mm) | Angle (°) | Material ID\n"); 99 printf("------|----------------|-----------|------------\n"); 100 for (int i = 0; i < lam->num_layers; i++) { 101 printf(" %2d | %13.3f | %9.2f | %10d\n", 102 i + 1, 103 lam->thk[i], 104 lam->ang[i], 105 lam->mat_id[i]); 106 } 107 printf("\n"); 108 } 109 fclose(file); 110 } 111 112 MaterialProperties *read_material_properties 113 (const char *filename, int *n_mat, bool print_details) 114 { 115 FILE *file; 116 char line[MAX_LINE_LENGTH]; 117 int material_count = 0; 118 MaterialProperties *mat = NULL; 119 120 if (print_details){ 121 printf("\n"); 122 printf("+---------------------+\n"); 123 printf("| Material Properties |\n"); 124 printf("+---------------------+\n"); 125 printf("\n"); 126 } 127 file = fopen(filename, "r"); 128 if (file == NULL) { 129 fprintf(stderr, "Error opening file %s\n", filename); 130 return NULL; 131 } 132 133 // First pass: count the number of mat 134 while (fgets(line, sizeof(line), file)) { 135 if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') continue; 136 material_count++; 137 } 138 139 // Reset file pointer to beginning 140 rewind(file); 141 142 // Allocate memory for mat 143 *n_mat = material_count; 144 mat = (MaterialProperties *)malloc( 145 material_count * sizeof(MaterialProperties)); 146 if (mat == NULL) { 147 fprintf(stderr, "Memory allocation failed\n"); 148 fclose(file); 149 return NULL; 150 } 151 152 // Second pass: read the material properties 153 int mat_indx = 0; 154 while (fgets(line, sizeof(line), file)) { 155 if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') continue; 156 157 char type; 158 // Check if the material type is specified at the beginning of the line 159 if (sscanf(line, "%c", &type) != 1) { 160 fprintf(stderr, "Error reading material type from line: %s", line); 161 continue; 162 } 163 164 if (type == 'I') { // 'I' for Isotropic 165 // For isotropic mat, we only need E and nu (Poisson's ratio) 166 if (sscanf(line + 1, "%lf %lf", 167 &mat[mat_indx].E1, 168 &mat[mat_indx].nu12) != 2) { 169 fprintf(stderr, "Error reading isotropic material properties from line: %s", line); 170 continue; 171 } 172 // Set isotropic flag 173 mat[mat_indx].isotropic = true; 174 // Fill in other properties with default or derived values 175 mat[mat_indx].E2 = mat[mat_indx].E1; // For isotropic mat, E1 = E2 176 mat[mat_indx].G12 = mat[mat_indx].E1 / (2.0 * (1 + mat[mat_indx].nu12)); 177 if (print_details) { 178 printf("ISOTROPIC:\n"); 179 printf("Material ID %d Properties:\n", mat_indx); 180 printf(" E (Pa) : %.2e\n", mat[mat_indx].E1); 181 printf(" nu : %.3f\n\n", mat[mat_indx].nu12); 182 } 183 } else if (type == 'A') { // 'A' for Anisotropic 184 // Read all properties for anisotropic mat 185 if (sscanf(line + 1, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", 186 &mat[mat_indx].E1, &mat[mat_indx].E2, 187 &mat[mat_indx].nu12, &mat[mat_indx].G12, 188 &mat[mat_indx].a1, &mat[mat_indx].a2, 189 &mat[mat_indx].b1, &mat[mat_indx].b2, 190 &mat[mat_indx].Xt, &mat[mat_indx].Xc, 191 &mat[mat_indx].Yt, &mat[mat_indx].Yc, 192 &mat[mat_indx].S12, &mat[mat_indx].S32) != 14) { 193 fprintf(stderr, "Error reading anisotropic material properties from line: %s", line); 194 continue; 195 } 196 // Set isotropic flag to false 197 mat[mat_indx].isotropic = false; 198 199 if (print_details) { 200 printf("ORTHOTROPIC:\n"); 201 printf("Material ID %d Properties:\n", mat_indx); 202 printf(" E1 (Pa) : %.2e\n", mat[mat_indx].E1); 203 printf(" E2 (Pa) : %.2e\n", mat[mat_indx].E2); 204 printf(" nu12 : %.3f\n", mat[mat_indx].nu12); 205 printf(" G12 (Pa) : %.2e\n", mat[mat_indx].G12); 206 printf(" a1 (1/K) : %.2e\n", mat[mat_indx].a1); 207 printf(" a2 (1/K) : %.2e\n", mat[mat_indx].a2); 208 printf(" b1 : %.2e\n", mat[mat_indx].b1); 209 printf(" b2 : %.2e\n", mat[mat_indx].b2); 210 printf(" Xt (Pa) : %.2e\n", mat[mat_indx].Xt); 211 printf(" Xc (Pa) : %.2e\n", mat[mat_indx].Xc); 212 printf(" Yt (Pa) : %.2e\n", mat[mat_indx].Yt); 213 printf(" Yc (Pa) : %.2e\n", mat[mat_indx].Yc); 214 printf(" S12 (Pa) : %.2e\n", mat[mat_indx].S12); 215 printf(" S23 (Pa) : %.2e\n\n", mat[mat_indx].S32); 216 } 217 } else { 218 fprintf(stderr, "Unknown material type in line: %s", line); 219 continue; 220 } 221 mat_indx++; 222 } 223 fclose(file); 224 return mat; 225 } 226 227 void read_force_vector(double *F, const char *filename) { 228 FILE *file; 229 file = fopen(filename, "r"); 230 if (file == NULL) { 231 fprintf(stderr, "Error opening file %s\n", filename); 232 exit(1); 233 } 234 235 for (int i = 0; i < N; i++) { 236 if (fscanf(file, "%lf", &F[i]) != 1) { 237 fprintf(stderr, "Error reading force vector from file %s\n", filename); 238 fclose(file); 239 exit(1); 240 } 241 } 242 243 fclose(file); 244 }