| [5be6a1d] | 1 | /***************************************************************************
|
|---|
| 2 | * FILE: ser_wave.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * Serial Concurrent Wave Equation - C Version
|
|---|
| 5 | * This program implements the concurrent wave equation described
|
|---|
| 6 | * in Chapter 5 of Fox et al., 1988, Solving Problems on Concurrent
|
|---|
| 7 | * Processors, vol 1.
|
|---|
| 8 | * AUTHOR: unknown
|
|---|
| 9 | * LAST REVISED: 04/15/05 Blaise Barney
|
|---|
| 10 | ***************************************************************************/
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 | #include <math.h>
|
|---|
| 14 | #include <time.h>
|
|---|
| 15 |
|
|---|
| 16 | #define MAXPOINTS 1000
|
|---|
| 17 | #define MAXSTEPS 1000
|
|---|
| 18 | #define MINPOINTS 20
|
|---|
| 19 | #define PI 3.14159265
|
|---|
| 20 |
|
|---|
| 21 | void init_param(void);
|
|---|
| 22 | void init_line(void);
|
|---|
| 23 | void update (void);
|
|---|
| 24 | void printfinal (void);
|
|---|
| 25 |
|
|---|
| 26 | int nsteps, /* number of time steps */
|
|---|
| 27 | tpoints, /* total points along string */
|
|---|
| 28 | rcode; /* generic return code */
|
|---|
| 29 | double values[MAXPOINTS+2], /* values at time t */
|
|---|
| 30 | oldval[MAXPOINTS+2], /* values at time (t-dt) */
|
|---|
| 31 | newval[MAXPOINTS+2]; /* values at time (t+dt) */
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | /***************************************************************************
|
|---|
| 35 | * Obtains input values from user
|
|---|
| 36 | ***************************************************************************/
|
|---|
| 37 | void init_param(void)
|
|---|
| 38 | {
|
|---|
| 39 | char tchar[8];
|
|---|
| 40 |
|
|---|
| 41 | /* set number of points, number of iterations */
|
|---|
| 42 | tpoints = 0;
|
|---|
| 43 | nsteps = 0;
|
|---|
| 44 | while ((tpoints < MINPOINTS) || (tpoints > MAXPOINTS)) {
|
|---|
| 45 | printf("Enter number of points along vibrating string [%d-%d]: ",
|
|---|
| 46 | MINPOINTS, MAXPOINTS);
|
|---|
| 47 | scanf("%s", tchar);
|
|---|
| 48 | tpoints = atoi(tchar);
|
|---|
| 49 | if ((tpoints < MINPOINTS) || (tpoints > MAXPOINTS))
|
|---|
| 50 | printf("Invalid. Please enter value between %d and %d\n",
|
|---|
| 51 | MINPOINTS, MAXPOINTS);
|
|---|
| 52 | }
|
|---|
| 53 | while ((nsteps < 1) || (nsteps > MAXSTEPS)) {
|
|---|
| 54 | printf("Enter number of time steps [1-%d]: ", MAXSTEPS);
|
|---|
| 55 | scanf("%s", tchar);
|
|---|
| 56 | nsteps = atoi(tchar);
|
|---|
| 57 | if ((nsteps < 1) || (nsteps > MAXSTEPS))
|
|---|
| 58 | printf("Invalid. Please enter value between 1 and %d\n", MAXSTEPS);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | printf("Using points = %d, steps = %d\n", tpoints, nsteps);
|
|---|
| 62 |
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /***************************************************************************
|
|---|
| 66 | * Initialize points on line
|
|---|
| 67 | **************************************************************************/
|
|---|
| 68 | void init_line(void)
|
|---|
| 69 | {
|
|---|
| 70 | int i, j;
|
|---|
| 71 | double x, fac, k, tmp;
|
|---|
| 72 |
|
|---|
| 73 | /* Calculate initial values based on sine curve */
|
|---|
| 74 | fac = 2.0 * PI;
|
|---|
| 75 | k = 0.0;
|
|---|
| 76 | tmp = tpoints - 1;
|
|---|
| 77 | for (j = 1; j <= tpoints; j++) {
|
|---|
| 78 | x = k/tmp;
|
|---|
| 79 | values[j] = sin (fac * x);
|
|---|
| 80 | k = k + 1.0;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /* Initialize old values array */
|
|---|
| 84 | for (i = 1; i <= tpoints; i++)
|
|---|
| 85 | oldval[i] = values[i];
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /***************************************************************************
|
|---|
| 89 | * Calculate new values using wave equation
|
|---|
| 90 | **************************************************************************/
|
|---|
| 91 | void do_math(int i)
|
|---|
| 92 | {
|
|---|
| 93 | double dtime, c, dx, tau, sqtau;
|
|---|
| 94 |
|
|---|
| 95 | dtime = 0.3;
|
|---|
| 96 | c = 1.0;
|
|---|
| 97 | dx = 1.0;
|
|---|
| 98 | tau = (c * dtime / dx);
|
|---|
| 99 | sqtau = tau * tau;
|
|---|
| 100 | newval[i] = (2.0 * values[i]) - oldval[i]
|
|---|
| 101 | + (sqtau * (values[i-1] - (2.0 * values[i]) + values[i+1]));
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /***************************************************************************
|
|---|
| 105 | * Update all values along line a specified number of times
|
|---|
| 106 | **************************************************************************/
|
|---|
| 107 | void update()
|
|---|
| 108 | {
|
|---|
| 109 | int i, j;
|
|---|
| 110 |
|
|---|
| 111 | /* Update values for each time step */
|
|---|
| 112 | for (i = 1; i<= nsteps; i++) {
|
|---|
| 113 | /* Update points along line for this time step */
|
|---|
| 114 | for (j = 1; j <= tpoints; j++) {
|
|---|
| 115 | /* global endpoints */
|
|---|
| 116 | if ((j == 1) || (j == tpoints))
|
|---|
| 117 | newval[j] = 0.0;
|
|---|
| 118 | else
|
|---|
| 119 | do_math(j);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /* Update old values with new values */
|
|---|
| 123 | for (j = 1; j <= tpoints; j++) {
|
|---|
| 124 | oldval[j] = values[j];
|
|---|
| 125 | values[j] = newval[j];
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /***************************************************************************
|
|---|
| 131 | * Print final results
|
|---|
| 132 | **************************************************************************/
|
|---|
| 133 | void printfinal()
|
|---|
| 134 | {
|
|---|
| 135 | int i;
|
|---|
| 136 | for (i = 1; i <= tpoints; i++) {
|
|---|
| 137 | printf("%6.2f ", values[i]);
|
|---|
| 138 | if (i%10 == 0)
|
|---|
| 139 | printf("\n");
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /***************************************************************************
|
|---|
| 144 | * Main program
|
|---|
| 145 | **************************************************************************/
|
|---|
| 146 | int main(int argc, char *argv[])
|
|---|
| 147 | {
|
|---|
| 148 | /*
|
|---|
| 149 | int left, right;
|
|---|
| 150 | */
|
|---|
| 151 |
|
|---|
| 152 | printf("Starting serial version of wave equation...\n");
|
|---|
| 153 | init_param();
|
|---|
| 154 | printf("Initializing points on the line...\n");
|
|---|
| 155 | init_line();
|
|---|
| 156 | printf("Updating all points for all time steps...\n");
|
|---|
| 157 | update();
|
|---|
| 158 | printf("Printing final results...\n");
|
|---|
| 159 | printfinal();
|
|---|
| 160 | printf("\nDone.\n\n");
|
|---|
| 161 | }
|
|---|