/* LS-SCRIPT: Extract solid element stress tensor, then write them out to a file
*/

define:
void main(void)
{
    Int numState;
    Int numEle;
    Int i = 0;
    Int j = 0;
    Int eid;
    char filename[256];
    FILE *fp;
    Float *results = NULL;
    Float *stressx = NULL;
    Float *stressy = NULL;
    Float *stressz = NULL;
    Float *stressxy = NULL;
    Float *stressyz = NULL;
    Float *stresszx = NULL;
    Float *times=NULL;


/* Inquire no. of solid elements in this model */
    numEle = SCLGetDataCenterInt("num_solid_elements");

    if (numEle == 0) {
       Echo("No solid elements in model");
       exit(0);
    }

/* Inquire no. of states in this run */
    numState = SCLGetDataCenterInt("num_states");

/* Allocate memeory for time array */
    times = malloc(numState*sizeof(Float));
    
/* Allocate memory for the stress arrays */
    results = malloc(numEle*sizeof(Float));
    stressx = malloc(numEle*sizeof(Float));
    stressy = malloc(numEle*sizeof(Float));
    stressz = malloc(numEle*sizeof(Float));
    stressxy = malloc(numEle*sizeof(Float));
    stressyz = malloc(numEle*sizeof(Float));
    stresszx = malloc(numEle*sizeof(Float));

/* Get time array */
    numState = SCLGetDataCenterFloatArray("state_times",0,0,&times);

/* Assign a file name */
        sprintf(filename, "solid_stress.dat");
/* Open the file for writting element stress data */
        fp = fopen(filename,"w+");

/* Loop through the last 10 states */
    for ( i = numState-10; i<numState; i = i+1)
    {
/* Switch to the ith state */
        SCLSwitchStateTo(i+1);

        /*Get stress for solid element*/
        /* char* usrname: compenont name
         * Int typename: 0=Whole, SHELL, TSHELL, BEAM, SOLID
         * Int ipt: MID, INNER, OUTTER
         * Float** results: results array
         */
        numEle = SCLGetDataCenterFloatArray("stress_x",SOLID,0,&stressx);
        numEle = SCLGetDataCenterFloatArray("stress_y",SOLID,0,&stressy);
        numEle = SCLGetDataCenterFloatArray("stress_z",SOLID,0,&stressz);
        numEle = SCLGetDataCenterFloatArray("stress_xy",SOLID,0,&stressxy);
        numEle = SCLGetDataCenterFloatArray("stress_yz",SOLID,0,&stressyz);
        numEle = SCLGetDataCenterFloatArray("stress_zx",SOLID,0,&stresszx);

        /* compute the average stress */
        for(j = 0; j < numEle; j = j+1) {
              results[j] = (stressx[j] + stressy[j] + stressz[j])/3.0;
        }

        fprintf(fp," Solid element stress at state number %5d Time=%13e\n",i+1,times[i]);
        fprintf(fp,"    Ele ID       X-Stress       Y-Stress       Z-Stress      XY-Stress      YZ-Stress      ZX-Stress     Ave.Stress\n");
/* Output all solid elements stress data */
        for (j=0; j<numEle; j=j+1) {
          eid = SCLGetUserId(j,SOLID);
          fprintf(fp,"%10d  %13e  %13e  %13e  %13e  %13e  %13e  %13e\n",eid,stressx[j],stressy[j],stressz[j],
            stressxy[j],stressyz[j],stresszx[j],results[j]);
        }
    }
    fclose(fp);
    free(results);
    free(stressx);
    free(stressy);
    free(stressz);
    free(stressxy);
    free(stressyz);
    free(stresszx);
}
main();
