Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15358
  • 博文数量: 7
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-24 12:17
文章分类

全部博文(7)

文章存档

2010年(7)

我的朋友
最近访客

分类:

2010-01-29 22:59:26

结构力学电算实习报告

 

--结构力学可扩展程序设计

 

 

程序功能介绍:

1.       本程序实现基本功能的结构力学计算,只能够计算“连续梁的杆端弯矩”。

2.       本程序具有设计机构力学计算程序应注意的方方面面,通过本程序能够了解结构力学程序设计的基本要点。

3.       本程序旨在提供一个可扩展模块,使之能够轻易的扩展为更加复杂的计算程序。

 

 

程序注释:

1.       程序名:lianxuliang.exe

2.       功能:求解连续梁的角位移,杆端弯矩。

3.       作者:周第伟。

4.       日期:2010-1-25

5.       简介:本程序输入结点数,杆的EIlFq等性质和固结点号,输出连续梁节点的角位移和各杆端弯矩。

6.       注意:本程序在TC3.0环境下编译成功,经试验在TC2.0环境下编译出现错误,原因是TC2.0的浮点数模板调用的著名BUG

7.       其他:本程序有一处扩展模块供将来扩展为更复杂的程序使用。

 

 

 

程序内容:

程序分为beam.hmain.hinit_scr.cinit_e.cinit_beam.coutput_c_disp.coutput_c_beamforces.ccalculate.ctab.cmlcmem.c九个文件。

l  beam.h


 

/********************************************************************************
 * 本函数用TC3.0编译成功,TC2.0存在bug。 *
 ********************************************************************************/

#include <stdlib.h> //使用exit所需的库文件

#include <conio.h> //使用clrscr所需的库文件

#include <malloc.h> //使用malloc、free所需的库文件

#include <math.h> //使用数学函数所需的库文件

#include <stdio.h> //使用标准输入输出所需的库文件


#define ACCURACY_LOWER -0.0000001 //定义精度

#define ACCURACY_UPPER 0.0000001

#define STIFF(i) (((beam_ptr+(i))->E)*((beam_ptr+(i))->I)/((beam_ptr+(i))->l))
#define M_q_left(i) (-((beam_ptr+(i))->l)*((beam_ptr+(i))->l)*((beam_ptr+(i))->q)/12)
#define M_q_right(i) (((beam_ptr+(i))->l)*((beam_ptr+(i))->l)*((beam_ptr+(i))->q)/12)
#define M_F_left(i) (-((beam_ptr+(i))->a)*(((beam_ptr+(i))->l)-((beam_ptr+(i))->a))*(((beam_ptr+(i))->l)-((beam_ptr+(i))->a))*((beam_ptr+(i))->F)/(((beam_ptr+(i))->l) * ((beam_ptr+(i))->l)))
#define M_F_right(i) (((beam_ptr+(i))->a)*((beam_ptr+(i))->a)*(((beam_ptr+(i))->l)-((beam_ptr+(i))->a))*((beam_ptr+(i))->F)/(((beam_ptr+(i))->l) * ((beam_ptr+(i))->l)))

int n; //保存节点数

char choose;

double *p;
struct beam{
    double E;
    double I;
    double l;
    double F;
    double a;
    double q;
}*beam_ptr; //保存杆件组的首地址


FILE *fp;

int tab(int i,int j); //用于访问矩阵的标签

void init_scr(void); //初始化屏幕

void init_beam(double *ptr); //初始化连续梁的各数值

void calculate(double *ptr); //解线型方程组

void output_c_disp(double *ptr); //输出位移

void output_c_beamforces(double *ptr); //输出杆端力

void init_e(void);
void mlcmem(void); //申请内存空间

l  main.h


/********************************************************************************
 * 本函数用TC3.0编译成功,TC2.0存在bug。 *
 ********************************************************************************/

#include <stdlib.h> //使用exit所需的库文件

#include <conio.h> //使用clrscr,getch所需的库文件

#include <malloc.h> //使用malloc、free所需的库文件

#include <stdio.h> //使用标准输入输出所需的库文件

#include "beam.h" //自定义头文件


void main(void) {

    clrscr();
    if((fp = fopen("OUTPUT.TXT", "w+")) == NULL) exit (1);
    printf("enter rods_num(>1):\n");
    fprintf(fp,"enter rods_num(>1):\n");
    scanf("%d",&n);
    fprintf(fp,"%d\n", n);

    while(1){
        init_scr();
        choose = getch();
        switch(choose){
            case 'c':
                mlcmem();
                init_beam(p);
                calculate(p);
                output_c_disp(p);
                output_c_beamforces(p);
                break;
            case 'e':
                mlcmem();
                init_beam(p);
                calculate(p);
                output_c_disp(p);
                output_c_beamforces(p);
                break;
            case 'q':
                free(p);
                free(beam_ptr);
                fclose(fp);
                exit (0);
        }
    }
}

l  init_scr.c

 

#include <conio.h> //使用clrscr,getch所需的库文件

#include <stdio.h> //使用标准输入输出所需的库文件

#include "beam.h" //自定义头文件


void init_scr(){
    clrscr();
    printf("Press 'q' to quit.\n");
    printf("Press 'e' to end point yingxiangxian.\n");
    printf("Press 'c' to continuous beams.\n");
    printf("\n");
}

l  init_e.c


#include <conio.h> //使用clrscr,getch所需的库文件

#include <stdio.h> //使用标准输入输出所需的库文件


void init_beam(double *ptr) {
    printf("Not complition!\n");
    getch();

l  init_beam.c


#include <stdlib.h> //使用exit所需的库文件

#include <conio.h> //使用clrscr,getch所需的库文件

#include <malloc.h> //使用malloc、free所需的库文件

#include <stdio.h> //使用标准输入输出所需的库文件

#include "beam.h" //自定义头文件


void init_beam(double *ptr) {
    int i,j;
    char c='0';
    int fix_node;
    double *nodf_ptr;

    nodf_ptr = (double *)malloc(n*sizeof(double));
    if (nodf_ptr == NULL){
        printf("Create nodf_mem Error!\n");
        fprintf(fp,"Create nodf_mem Error!\n");
        free(ptr);
        free(beam_ptr);
        fclose(fp);
        getch();
        exit (1);
    }
    else {
        printf("Init nodal forces:\n");
        fprintf(fp,"Init nodal forces:\n");
        for(i=0;i<n;i++) {
            scanf("%lf", nodf_ptr+i);
            fprintf(fp,"%lf\n", *(nodf_ptr+i));
        }
    }

    printf("\nInit beams:\n");
    fprintf(fp,"\nInit beams:\n");
    for(i=0;i<n-1;i++) {
        printf("\n\nBeam %d\n",i+1);
        fprintf(fp,"\n\nBeam %d\n",i+1);
        printf("Input E:\n");
        fprintf(fp,"Input E:\n");
        scanf("%lf", &((beam_ptr+i)->E));
        fprintf(fp,"%lf\n", (beam_ptr+i)->E);
        printf("Input I:\n");
        fprintf(fp,"Input I:\n");
        scanf("%lf", &((beam_ptr+i)->I));
        fprintf(fp,"%lf\n", (beam_ptr+i)->I);
        printf("Input l:\n");
        fprintf(fp,"Input l:\n");
        scanf("%lf", &((beam_ptr+i)->l));
        fprintf(fp,"%lf\n", (beam_ptr+i)->l);
        printf("Input F:\n");
        fprintf(fp,"Input F:\n");
        if (choose == 'c') {
            scanf("%lf", &((beam_ptr+i)->F));
            if ((ACCURACY_LOWER > ((beam_ptr+i)->F)) || (((beam_ptr+i)->F) > ACCURACY_UPPER)) {
                printf("Input a:\n");
                fprintf(fp,"Input a:\n");
                scanf("%lf", &((beam_ptr+i)->a));
                fprintf(fp,"%lf\n", (beam_ptr+i)->a);
            }
            printf("Input q:\n");
            fprintf(fp,"Input q:\n");
            scanf("%lf", &((beam_ptr+i)->q));
            fprintf(fp,"%lf\n", (beam_ptr+i)->q);
        }
    }

    for(i=1;i<=n;i++) {
        for(j=1;j<=n;j++) {
            *(ptr+tab(i,j)) = 0;
        }
    }
    *(ptr+tab(1,1)) = 4 * STIFF(0);
    *(ptr+tab(n,n)) = 4 * STIFF(n-2);
    *(ptr+tab(n-1,n)) = 2 * STIFF(n-2);
    *(ptr+tab(n,n-1)) = 2 * STIFF(n-2);
    for(i=2;i<n;i++) {
        *(ptr+tab(i,i)) = 4 * (STIFF(i-1) + STIFF(i-2));
        *(ptr+tab(i,i-1)) = 2 * STIFF(i-2);
        *(ptr+tab(i-1,i)) = *(ptr+tab(i,i-1));
    }
    *(ptr+tab(1,n+1)) = *nodf_ptr + M_q_left(0) + M_F_left(0);
    *(ptr+tab(n,n+1)) = *(nodf_ptr+n-1) + M_q_right(n-2) + M_F_right(n-2);
    for(i=2;i<n;i++) {
        *(ptr+tab(i,n+1)) = *(nodf_ptr+i-1) + M_q_right(i-2) + M_F_right(i-2) + M_q_left(i-1) + M_F_left(i-1);
    }

    printf("Init fixed crunode\n");
    fprintf(fp,"Init fixed crunode\n");
    while(c != 'q') {
        printf("Input fixed node num:\n");
        fprintf(fp,"Input fixed node num:\n");
        scanf("%d",&fix_node);
        fprintf(fp,"%d\n",fix_node);
        if((fix_node >= 1) && (fix_node <= n)) {
            for(i=1;i<=n;i++) {
                *(ptr+tab(i,fix_node)) = 0;
            }
            for(i=1;i<=n+1;i++) {
                *(ptr+tab(fix_node,i)) = 0;
            }
            *(ptr+tab(fix_node,fix_node)) = 1;
        }
        printf("Press 'q' if u wanto end input fixed node!\n");
        fprintf(fp,"Press 'q' if u wanto end input fixed node!\n");
        c = getch();
    }

    free(nodf_ptr);
}

l  output_c_disp.c


#include <conio.h> //使用clrscr,getch所需的库文件

#include <stdio.h> //使用标准输入输出所需的库文件

#include "beam.h" //自定义头文件


void output_c_disp(double *ptr) {
    int i;
    
    printf("Output is:\n");
    for(i=1;i<=n;i++) {
        printf("%f\n",*(ptr+tab(i,n+1)));
        fprintf(fp,"%f\n",*(ptr+tab(i,n+1)));
    }
    getch();
}

l  output_c_beamforces.c


#include <conio.h> //使用clrscr,getch所需的库文件

#include <stdio.h> //使用标准输入输出所需的库文件

#include "beam.h" //自定义头文件


void output_c_beamforces(double *ptr){
    int i;
    printf("\nOutput of the beams'force:\n");
    fprintf(fp,"\nOutput of the beams'force:\n");
    for(i=1;i<=n-1;i++) {
        fprintf(fp,"Beam %d:\n",i);
        fprintf(fp,"%f\t",4 * STIFF(i-1) * (*(ptr+tab(i,n+1))) + 2 * STIFF(i-1) * (*(ptr+tab(i+1,n+1))) + M_q_right(i) + M_F_right(i-1));
        fprintf(fp,"%f\n",2 * STIFF(i-1) * (*(ptr+tab(i,n+1))) + 4 * STIFF(i-1) * (*(ptr+tab(i+1,n+1))) + M_q_left(i) + M_F_left(i-1));
        printf("Beam %d:\n",i);
        printf("%f\t",4 * STIFF(i-1) * (*(ptr+tab(i,n+1))) + 2 * STIFF(i-1) * (*(ptr+tab(i+1,n+1))) + M_q_right(i-1) + M_F_right(i-1));
        printf("%f\n",2 * STIFF(i-1) * (*(ptr+tab(i,n+1))) + 4 * STIFF(i-1) * (*(ptr+tab(i+1,n+1))) + M_q_left(i-1) + M_F_left(i-1));
    }
    getch();
}

l  calculate.c


/********************************************************
** 伪代码: *
** for (j=1;j<=n;j++) { *
** for (i=j;i<=n;i++) { *
**     if (xij != 0) {第i行与第j行互换;跳出;} *
** } *
** 第j行除以xjj; *
** for (i=j+1;i<=n;i++) { *
** if (xij != 0) 第i行加上(-xij)乘以第j行; *
** } *
*********************************************************/


#include "beam.h" //自定义头文件


void calculate(double *ptr) {
    int i,j,k;
    float tmp;
    for(j=1;j<=n;j++) {
        for(i=j;i<=n;i++) {
            if ((ACCURACY_LOWER > *(ptr + tab(i,j))) || (*(ptr + tab(i,j)) > ACCURACY_UPPER)) {
                for(k=1;k<=n+1;k++) {
                    tmp = *(ptr+tab(i,k));
                    *(ptr+tab(i,k)) = *(ptr+tab(j,k));
                    *(ptr+tab(j,k)) = tmp;
                }
                break;
            }
        }
        tmp = *(ptr+tab(j,j));
        for(k=1;k<=n+1;k++) {
            *(ptr+tab(j,k)) = *(ptr+tab(j,k)) / tmp;
        }
        for(i=j+1;i<=n;i++) {
            if ((ACCURACY_LOWER > *(ptr + tab(i,j))) || (*(ptr + tab(i,j)) > ACCURACY_UPPER)) {
                tmp = *(ptr+tab(i,j));
                for(k=1;k<=n+1;k++) {
                    *(ptr+tab(i,k)) = *(ptr+tab(i,k)) - tmp * *(ptr+tab(j,k));
                }
            }
        }
    }
    for(j=n;j>=1;j--) {
        for(i=j-1;i>=1;i--) {
            tmp = *(ptr+tab(i,j));
            for(k=1;k<=n+1;k++) {
                *(ptr+tab(i,k)) = *(ptr+tab(i,k)) - tmp * *(ptr+tab(j,k));
            }
        }
    }
}

l  tab.c


#include "beam.h" //自定义头文件


int tab(int i,int j){
    return (i-1)*(n+1)+j-1;
}

l  mlcmem.c


#include <stdlib.h> //使用exit所需的库文件

#include <conio.h> //使用clrscr,getch所需的库文件

#include <malloc.h> //使用malloc、free所需的库文件

#include <stdio.h> //使用标准输入输出所需的库文件

#include "beam.h" //自定义头文件


void mlcmem(void) {
    beam_ptr = (struct beam *)malloc((n-1)*sizeof(struct beam));
    if (beam_ptr == NULL) {
        printf("Create beams_mem Error!\n");
        fclose(fp);
        exit (1);
    }
    p = (double *)malloc(n*(n+1)*sizeof(double));
    if (p == NULL) {
        printf("Create Mem Error!\n");
        free(beam_ptr);
        fclose(fp);
        getch();
        exit (1);
    }
}

程序算例:

 

 

试题如上图:三段梁长分别为:8m12m8mF为第一段梁的中点。

程序结果保存在同一目录下output.txt文件中:


 

enter rods_num(>1):
4
Init nodal forces:
0.000000
0.000000
0.000000
0.000000

Init beams:

Beam 1
Input E:
6.000000
Input I:
1.000000
Input l:
8.000000
Input F:
10.000000
Input a:
4.000000
Input q:
0.000000


Beam 2
Input E:
24.000000
Input I:
1.000000
Input l:
12.000000
Input F:
0.000000
Input q:
4.000000


Beam 3
Input E:
6.000000
Input I:
1.000000
Input l:
8.000000
Input F:
0.000000
Input q:
0.000000
Init fixed crunode
Input fixed node num:
1
Press 'q' if u wanto end input fixed
Input fixed node num:
4
Press 'q' if u wanto end input fixed
Output is:
0.000000
-5.809524
6.476190
0.000000

Output of the beams-force:
Beam 1:
1.285714    -27.428571
Beam 2:
27.428570    -19.428575
Beam 3:
19.428570    9.714285

程序扩展:

l  calculate.c文件通过增广矩阵行变换实现A(n*n) * X(n*1) = B(n*1)的求解,只要将对应的结构力学问题转换为求解矩阵方程的根即可用该函数计算。

l  init_e.c文件保存了一个未完成的函数,通过完成该函数可将程序扩展求解复杂问题。

l  main.c文件通过case语句实现了程序可扩展的基础,通过扩展case语句程序可实现更多更复杂的问题。

l  beam.h文件定义了程序所用的各种数据类型和数据结构,运用它们可修改程序的适用情况。

阅读(1309) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:单片机概述

给主人留下些什么吧!~~