forked from certik/numpy-fortran
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "compute.h" | ||
|
||
void derivs(double x, double y, double *dx, double *dy, void *data) | ||
{ | ||
char *str = data; | ||
if (str == "eq1") { | ||
*dx = -y; | ||
*dy = x; | ||
} else if (str == "eq2") { | ||
*dx = y; | ||
*dy = x; | ||
} else { | ||
printf("Not implemented...\n"); | ||
abort(); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
eq d; | ||
init(d); | ||
register_func(d, derivs); | ||
run(d, 0, 1, 0.1, 10, "eq1"); | ||
printf("\n"); | ||
run(d, 0, 1, 0.1, 10, "eq2"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#! /bin/bash | ||
|
||
set -e | ||
gcc -c a.c -o a.o | ||
gcc -c compute.c -o compute.o | ||
gcc -o a a.o compute.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <stdio.h> | ||
|
||
#include "compute.h" | ||
|
||
void init(eq d) | ||
{ | ||
} | ||
|
||
void register_func(eq d, derivs_pt func) | ||
{ | ||
d.func = func; | ||
} | ||
|
||
void run(eq d, double x0, double y0, double dt, double n_steps, | ||
void *data) | ||
{ | ||
double x, y, dx, dy, t; | ||
int i; | ||
x = x0; | ||
y = y0; | ||
t = 0; | ||
for (i=0; i < n_steps; i++) { | ||
d.func(x, y, &dx, &dy, data); | ||
printf("%f %f\n", x, y); | ||
x += dx * dt; | ||
y += dy * dt; | ||
t += dt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
typedef void (*derivs_pt)(double x, double y, double *dx, double *dy, | ||
void *data); | ||
|
||
typedef struct { | ||
derivs_pt func; | ||
} eq; | ||
|
||
void init(eq d); | ||
void register_func(eq d, derivs_pt func); | ||
void run(eq d, double x0, double y0, double dt, double n_steps, | ||
void *data); |