main.c (3017B)
1 /* Author: Julio C. Ramirez-Ceballos 2 * Licence: ISC 3 * Description: 4 * This code is used to calculate the equivalent properties of a laminate 5 * with some configuration. 6 * 7 * INPUT: 8 * - Material properties 9 * - Laminate 10 * - Loads 11 * 12 * MAIN FUNCTION: 13 * - calc_stressCLT -- Calculates stress and strain vectors according to 14 * the Classical Laminate Theory, at the top and bottom of each layer. 15 * 16 * OUTPUT: 17 * - Laminate Coord. System (LS) strains, top and bottom of layer. 18 * - Material Coord. System (MS) stresses and strains, top and bottom of 19 * layer; 20 */ 21 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include "clt.c" 26 27 void write_output 28 (const char *filename, const char *content) 29 { 30 FILE *file = fopen(filename, "w"); 31 if (file == NULL) { 32 printf("Error opening file!\n"); 33 exit(1); 34 } 35 fprintf(file, "%s", content); 36 fclose(file); 37 } 38 39 void print_usage() { 40 printf("Usage: ./main [-p | -w]\n"); 41 printf(" -p : Print output to console\n"); 42 printf(" -w : Write output to output.res file\n"); 43 } 44 45 46 int main 47 (int argc, char *argv[]) 48 { 49 if (argc != 2 || (strcmp(argv[1], "-p") != 0 && strcmp(argv[1], "-w") != 0)) { 50 print_usage(); 51 return 1; 52 } 53 54 int save_to_file = (strcmp(argv[1], "-w") == 0); 55 56 /* Improve function for saving results and debugging */ 57 /* Option to save or print output */ 58 char output[10000]; /* Assuming the output won't exceed this size */ 59 int output_length = 0; 60 61 /* Redirect stdout to our buffer if we're saving to file */ 62 if (save_to_file) { 63 freopen("output.res", "w", stdout); 64 } 65 66 int num_materials; 67 MaterialProperties *materials = 68 read_material_properties("materials.dat", &num_materials, 1); 69 70 Laminate lam; 71 read_laminate_data("laminate.dat", &lam, 1); 72 73 double F[6]; 74 read_force_vector(F, "loads.dat"); 75 76 /* Perform CLT calculation */ 77 clt(&lam, materials, num_materials, F, 1, 0, 0); 78 79 /* If we're saving to file, we need to capture the output */ 80 if (save_to_file) { 81 fclose(stdout); /* Close the file stream */ 82 FILE *file = fopen("output.res", "r"); 83 if (file == NULL) { 84 printf("Error reading file!\n"); 85 exit(1); 86 } 87 while 88 (fgets(output + output_length, 89 sizeof(output) - output_length, 90 file) != NULL) 91 { 92 output_length += strlen(output + output_length); 93 } 94 fclose(file); 95 write_output("output.res", output); 96 } 97 98 /* Free allocated memory */ 99 free(lam.thk); 100 free(lam.ang); 101 free(lam.mat_id); 102 free(materials); 103 104 return 0; 105 } 106