C: Assignments Session 3

Today we play with typdef, struct and linked lists.

Problem 1: User define STRESS type

In the directory assignments/C-Session3/stressTransformStruct are three files (main.cpp stresstransform.h and stresstransform.c) that perform the transformation a hard-coded stress (12.0,-5.5,3.0) by a hard-coded angle (10.0 degrees). Change the files to use the following user defined STRESS type:

typedef struct stress {
        double sigx;
        double sigy;
        double tau;
} STRESS ;

The included CMakeList.txt can be used to compile your code.

Note

Your modified StressTransform(...) will require a pointer to a STRESS type object so that the type can be modified as C is pass by value. Inside the function that receives the pointer to a structure, assigning a new value to the variables in such a structure requires the syntax

void StressTransform(STRESS stressIn, STRESS *stressOut, double theta) {
  ...
  stressOut->sigx =  stressIn.sigX .... ;
}

This replaces the form

*sigx = ... ;

used for scalar-valued arguments.

Problem 2: Writing data for use by other programs: CSV

While C is very powerful for numeric computations, it can be painful to generate graphs or fancy images in a C program. Not impossible, just painful! A simpler way to generate graphs and look at the outputs is to use C to do the numerical computations, then have the C application write the results to an easily readable file, and use specialized tools for the post-processing. One common and simple format is CSV (comma-separated-values), which can be read easily by MATLAB, Python, or Excel.

Your task: Copy the code you just created to a new directory and modify it. Have the user to the application provide an argument such that the argument \(\Delta\theta\). Then we want the application to ouput all values of the transfomed stress from 0 through 180 using this \(\Delta\theta\). The format of the output shall be one angle per line, organized as follows:

theta, sigma_x, sigma_y, tau_xy
...

Once your code outputs the information, run it once more and save the results to a file named list.csv . You can make that modification in the code or an easier way is to direct the program output to a file using the following (make sure to add the spaces around the ‘>’)

$ ./appName 5.0 > list.csv

Note

You may want to download the file list.csv to your local computer before trying the next step, for it will require access to your display. That file can be opened in Excel and plotted there. A more efficient way is to prepare some nice plotting code, such as the provided plotterCSV.py. In the same folder where you placed list.csv run

Windows 10

>> python.exe plotterCSV.py

MacOS or Linux

$ python3 plotterCSV.py

Problem 3: Use a List data structure & Write to binary file

Copy the previous solution into a new directory. Modify the code to do the following:

  1. Keep the intermediate values of stress around. (Can’t thinkl of a valid reason why, but this is a programming exercise!) Do this by changing the definition of the STRESS type to include a pointer and store the values in a linked list, with the first element being the original stress value, and subsequent ones being new STRESS objects created with new.

    typedef struct stress {
       double sigx;
       double sigy;
       double tau;
       struct stress *next;
    } STRESS ;
    
  2. When done, loop over the linked list and output the results to a binary file.

Note

How large do you expect the binary file to be? Predict, and check using

$ ls -l mohrcircle.dta

You should be able to predict the exact number (to the byte!).

Note

There is another file parse.c that will open the contents of the binary file and print out a csv file. Use it to compare the output with your original file from the previous exercise. The parse.c just keeps reading bunches of 3 double values until it reaches the end, so there is no need to output the number of stress values at the start of the output file.

Note

Binary files are not readable by traditional ASCII editors (text editors). Doings so, usually shows some unintelligible scramble of characters, sometimes leaving your terminal in an unusable state.

However, you may view binary files using a hex-dump utility. That approach may help you understand and recover the structure of a binary file (though it still requires some practice and skill and luck). You may try such a tool on your binary file using

$ xxd mohrcircle.dta | less

where the | less pipes the output in a pager utility that allows you to search the output, jump pages forward and backward, or move to any specific line. Press q to exit this utility.