mirror of
https://github.com/rigreco/Runge-Kutta-Simulation.git
synced 2025-01-25 16:30:17 +00:00
RK Simulator
This commit is contained in:
parent
301ff7dcb0
commit
a114a23ea7
244
scr/DCMotor.c
Normal file
244
scr/DCMotor.c
Normal file
@ -0,0 +1,244 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
/* PROCESS MODULE */
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define rkfr 1.0e-3 /* RKF max resolution h<rk4r */
|
||||
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
extern double h,time;
|
||||
extern char fbuf[80];
|
||||
extern int cnt,fd;
|
||||
extern struct data {
|
||||
char v[20];
|
||||
char i[20];
|
||||
/*char ideal[20];*/
|
||||
};
|
||||
|
||||
/* LOCAL GLOBAL */
|
||||
char *outr[4]={"t="," ia(t)="," w(t)="};
|
||||
char *FILE_NAME="dcrk";
|
||||
/*double MC,va,ra,kf,l,j;
|
||||
|
||||
|
||||
/*Fehlberg parameters */
|
||||
double acca[5]={1.0/4.0,3.0/8.0,12.0/13.0,1.0,1.0/2.0};
|
||||
double erre1[5]={1.0/4.0,0.0,0.0,0.0,0.0};
|
||||
double erre2[5]={3.0/32.0,9.0/32.0,0.0,0.0,0.0};
|
||||
double erre3[5]={1932.0/2197.0,-7200.0/2197.0,7296.0/2197.0,0.0,0.0};
|
||||
double erre4[5]={439.0/216.0,-8.0,3680.0/513.0,-845.0/4104.0,0.0};
|
||||
double erre5[5]={-8.0/27.0,2.0,-3544.0/2565.0,1859.0/4104.0,-11.0/40.0};
|
||||
|
||||
double erre[5]={1.0/360.0,-128.0/4275.0,-2197.0/75240.0,1.0/50.0,2.0/55.0};
|
||||
|
||||
double ips5[5]={25.0/216.0,0.0,1408.0/2565.0,2197.0/4104.0,-1.0/5.0};
|
||||
/*double ips6[6]={16.0/135.0,0.0,6656.0/12825.0,28561.0/56430.0,-9.0/50.0,2.0/55.0};*/
|
||||
|
||||
|
||||
/* Integrated equation sets */
|
||||
int funrk(x,ya,eq)
|
||||
double x,ya[2],eq[2];
|
||||
{
|
||||
double va,ra,kf,la,j,MC;
|
||||
/*ya[0]=y3=ia(t) ya[1]=y4=w(t) variables array /*
|
||||
|
||||
/* ***** TEST equations WORK *******
|
||||
eq[0]=ya[0]+2*ya[1];
|
||||
eq[1]=10.0-ya[0];
|
||||
/* Harmonic oscillator (change initial condition)
|
||||
eq[0]=-2*ya[1];
|
||||
eq[1]=ya[0];*/
|
||||
|
||||
/* DATA MOTOR */
|
||||
va=(double)160.0;
|
||||
ra=(double)9.47;
|
||||
kf=(double)0.98; /* kf=k*fi */
|
||||
la=(double)0.0375;
|
||||
j=(double)0.011;
|
||||
MC=(double)0.0; /*4.2*/
|
||||
|
||||
/* DC Motor equations */
|
||||
eq[0]=(va/la)-(ra/la)*ya[0]-(kf*ya[1])/la; /* ia'(t)=(va/la)-(ra/la)*ia(t)-kf*w(t)/la */
|
||||
eq[1]=(kf*ya[0]-MC)/j; /* w'(t)=(1/j)*(MM-MC)=(1/j)*(kf*ia(t)-MC) */
|
||||
|
||||
/*
|
||||
eq[0]=42666.6-2525.3*ya[0]-261.3*ya[1];
|
||||
eq[1]=89.0*ya[0]-381.8; */
|
||||
}
|
||||
|
||||
|
||||
/*RKF45 Module */
|
||||
int runge_kutta(x,h,y_init,y)
|
||||
double x,h,y_init[2],y[2];
|
||||
{
|
||||
double r11,r21,r31,r41,r51,r61,r12,r22,r32,r42,r52,r62,ya[2],eq[2]; /*,err*/
|
||||
|
||||
/* RK inizialization */
|
||||
y[0]=(double)0.0; /* y3[0]=ia(t) return integrated value */
|
||||
y[1]=(double)0.0; /* y4[0]=w(t) return integrated value */
|
||||
eq[0]=(double)0.0;
|
||||
eq[1]=(double)0.0;
|
||||
ya[0]=y_init[0]; /* VIP */
|
||||
ya[1]=y_init[1];
|
||||
/*err=(double)0.0;*/
|
||||
|
||||
|
||||
/*
|
||||
do
|
||||
{
|
||||
|
||||
/* 1^ order */
|
||||
funrk(x,ya,eq);
|
||||
r11=eq[0];
|
||||
r12=eq[1];
|
||||
|
||||
/* 2^ order */
|
||||
ya[0]=(double)(y_init[0]+erre1[0]*r11*h);
|
||||
ya[1]=(double)(y_init[1]+erre1[0]*r12*h);
|
||||
|
||||
funrk(x+acca[0]*h,ya,eq);
|
||||
|
||||
r21=eq[0];
|
||||
r22=eq[1];
|
||||
|
||||
/* 3^ order */
|
||||
ya[0]=(double)(y_init[0]+erre2[0]*r11*h+erre2[1]*r21*h);
|
||||
ya[1]=(double)(y_init[1]+erre2[0]*r12*h+erre2[1]*r22*h);
|
||||
|
||||
funrk(x+acca[1]*h,ya,eq);
|
||||
|
||||
r31=eq[0];
|
||||
r32=eq[1];
|
||||
|
||||
/* 4^ order */
|
||||
ya[0]=(double)(y_init[0]+erre3[0]*r11*h+erre3[1]*r21*h+erre3[2]*r31*h);
|
||||
ya[1]=(double)(y_init[1]+erre3[0]*r12*h+erre3[1]*r22*h+erre3[2]*r32*h);
|
||||
|
||||
funrk(x+acca[2]*h,ya,eq);
|
||||
r41=eq[0];
|
||||
r42=eq[1];
|
||||
|
||||
/* 5^ order */
|
||||
ya[0]=(double)(y_init[0]+erre4[0]*r11*h+erre4[1]*r21*h+erre4[2]*r31*h+erre4[3]*r41*h);
|
||||
ya[1]=(double)(y_init[1]+erre4[0]*r12*h+erre4[1]*r22*h+erre4[2]*r32*h+erre4[3]*r42*h);
|
||||
|
||||
funrk(x+acca[3]*h,ya,eq);
|
||||
r51=eq[0];
|
||||
r52=eq[1];
|
||||
|
||||
/* 6^ order only for estimated error for adaptative step method
|
||||
ya[0]=(double)(y_init[0]+erre5[0]*r11*h+erre5[1]*r21*h+erre5[2]*r31*h+erre5[3]*r41*h+erre5[4]*r51*h);
|
||||
ya[1]=(double)(y_init[1]+erre5[0]*r12*h+erre5[1]*r22*h+erre5[2]*r32*h+erre5[3]*r42*h+erre5[4]*r52*h);
|
||||
|
||||
funrk(x+acca[4]*h,ya,eq);
|
||||
r61=eq[0];
|
||||
r62=eq[1];
|
||||
/*
|
||||
err=(fabs(erre[0]*r11+erre[1]*r31+erre[2]*r41+erre[3]*r51+erre[4]*r61)*h); /* estimate the error */
|
||||
/*
|
||||
|
||||
h=h/2;
|
||||
|
||||
|
||||
}
|
||||
while (err>rkfr);
|
||||
*/
|
||||
/* y3 y4 weighted average of operators 5^ order */
|
||||
y[0]=(double)(y_init[0]+(ips5[0]*r11+ips5[2]*r31+ips5[3]*r41+ips5[4]*r51)*h);
|
||||
y[1]=(double)(y_init[1]+(ips5[0]*r12+ips5[2]*r32+ips5[3]*r42+ips5[4]*r52)*h);
|
||||
|
||||
/* ------------------------------------------------------------------------------*/
|
||||
|
||||
}
|
||||
|
||||
ovmain()
|
||||
{
|
||||
char buf[20];
|
||||
/*double atof(); /* DON'T FORGET THIS */
|
||||
double x,y[2],y_init[2];
|
||||
struct data rk;
|
||||
|
||||
|
||||
/* inizialization */
|
||||
cnt= (int)0.0; /* Reset counter */
|
||||
y[0] = (double)0.0;
|
||||
y[1] = (double)0.0;
|
||||
y_init[0]= (double)0.0; /* init value of current ia(t) (for Harmonic oscillator y_3=10.0) */
|
||||
y_init[1]= (double)0.0; /* init value of angular speed w(t) (for Harmonic oscillator y_4=10.0) */
|
||||
x=(double)0.0; /* time in simulation */
|
||||
|
||||
/* Start simulation */
|
||||
scr_clear();
|
||||
scr_curs(0,0);
|
||||
puts("Pocessing...");
|
||||
|
||||
/* open and write header data to file */
|
||||
open(FILE_NAME,O_WRONLY|O_APPEND,0xC3);
|
||||
write(fd,fbuf,80);
|
||||
|
||||
buf[0]='\0'; /* buffers reset */
|
||||
fbuf[0]='\0';
|
||||
|
||||
/* RK main Cycle */
|
||||
while (x<time) {
|
||||
|
||||
|
||||
runge_kutta(x,h,y_init,y); /* call RKF45 process module */
|
||||
|
||||
|
||||
/* store output in buffer */
|
||||
ftoa(x,buf,6,2);
|
||||
strcat(fbuf,outr[0]);
|
||||
strcat(fbuf,buf); /* store time (x) */
|
||||
|
||||
ftoa(y[0],buf,6,2);
|
||||
strcat(fbuf,outr[1]);
|
||||
strcat(fbuf,buf); /* store speed i(t) (y3) */
|
||||
|
||||
ftoa(y[1],buf,6,2);
|
||||
strcat(fbuf,outr[2]);
|
||||
strcat(fbuf,buf); /* store current w(t) (y4) */
|
||||
|
||||
/* converting using max floating resolution */
|
||||
ftoa(y[0],rk.i,14,1); /* ia(t) */
|
||||
ftoa(y[1],rk.v,14,1); /* w(t) */
|
||||
|
||||
|
||||
puts(fbuf); /* output to screen */
|
||||
|
||||
/* write data result to file */
|
||||
write(fd,rk.i,20); /* write ia(t) */
|
||||
write(fd,rk.v,20); /* write w(t) */
|
||||
|
||||
|
||||
fbuf[0]='\0'; /* reset buffer */
|
||||
|
||||
y_init[0]=y[0]; /* save the current value of y3=ia(t) as new init value y_3 for next RK process step */
|
||||
y_init[1]=y[1]; /* save the current value of y4=w(t) as new init value y_4 for next RK process step */
|
||||
x=x+h; /* increment time in simulation */
|
||||
cnt++; /* renew counter */
|
||||
|
||||
}
|
||||
close(fd); /* close the data file */
|
||||
|
||||
|
||||
puts("Press any key to start plotting...");
|
||||
getch();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
34
scr/GINIT.c
Normal file
34
scr/GINIT.c
Normal file
@ -0,0 +1,34 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "d2monplt.h"
|
||||
/*
|
||||
extern int clearhgr();
|
||||
*/
|
||||
ovmain(screenmode)
|
||||
unsigned screenmode;
|
||||
{
|
||||
switch(screenmode)
|
||||
{
|
||||
case 1:
|
||||
scr_clear();
|
||||
d2hireson();
|
||||
clearhgr(911);
|
||||
maintoaux(0x4000,0x4000+8191,0x4000);
|
||||
break;
|
||||
case 2:
|
||||
d2hiresoff();
|
||||
scr_clear();
|
||||
}
|
||||
return 0;
|
||||
}
|
108
scr/INDC.c
Normal file
108
scr/INDC.c
Normal file
@ -0,0 +1,108 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
/* INPUT MODULE */
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
extern double h,time;
|
||||
extern char fbuf[80];
|
||||
extern int cnt;
|
||||
|
||||
/* LOCAL GLOBAL */
|
||||
char *title = "Runge Kutta Fehlberg integration methods";
|
||||
char *title1= "DC Motor transient state simulation:";
|
||||
char *title2= "Current: [ia'(t)=(va/la)-(ra/la)*ia(t)-kf*w(t)/la]";
|
||||
char *title3= "Anglar speed: [w'(t)=(1/j)*(MM-MC)=(1/j)*(kf*ia(t)-MC)]";
|
||||
char *title4= "in Aztec C for Apple II 128k series - by Greco Riccardo - Apple ][ Forever!";
|
||||
char *title5= "Thanks to Bill Buckels for all support - http://www.aztecmuseum.ca/ ";
|
||||
|
||||
char *out[3]={" DCMotor h="," ts="," cycles="}; /* always at global */
|
||||
|
||||
ovmain()
|
||||
{
|
||||
char buf[20],conv[4],rev;
|
||||
double atof(); /* DON'T FORGET THIS */
|
||||
|
||||
IN: /* set screen */
|
||||
#asm
|
||||
jsr $c300
|
||||
#endasm
|
||||
|
||||
buf[0]='\0'; /* buffers reset */
|
||||
fbuf[0]='\0';
|
||||
|
||||
/* Presentation */
|
||||
puts(title);
|
||||
puts(title1);
|
||||
puts(title2);
|
||||
puts(title3);
|
||||
puts(title4);
|
||||
puts(title5);
|
||||
puts(" ");
|
||||
|
||||
/* Input data */
|
||||
do{
|
||||
puts("Time step integration (Def. 0.01s) h [s] -> ");
|
||||
h=(double)atof(gets(buf));
|
||||
} while (h<0 || h<=0.005 || h>0.01);
|
||||
|
||||
strcat(fbuf,out[0]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
do{
|
||||
puts("Time simulation (Def. 5) ts [s] -> ");
|
||||
time=(double)atof(gets(buf));
|
||||
} while (time<0);
|
||||
|
||||
strcat(fbuf,out[1]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
strcat(fbuf,out[2]);
|
||||
ftoa((time/h),buf,0,1); /* 0 decimal like int */
|
||||
strncpy(conv,buf,4); /* save cnt to conv first 4 digit*/
|
||||
strcat(fbuf,buf);
|
||||
|
||||
puts(fbuf); /* Input data Review */
|
||||
|
||||
puts("Do you want to review input data (Y/N)?");
|
||||
rev=getch();
|
||||
if (rev == 'Y' || rev == 'y') goto IN;
|
||||
|
||||
/* set screen */
|
||||
scr_clear();
|
||||
scr_curs(0,0);
|
||||
|
||||
/* Simulation data review */
|
||||
puts("DC Motor Default parameters set:");
|
||||
scr_curs(2,0);
|
||||
puts("Number of input cycles:");
|
||||
scr_curs(2,31);
|
||||
puts(conv);
|
||||
puts("Input voltage E= 160.0 V");
|
||||
puts("Armor (stator) resistence ra= 9.47 Ohm");
|
||||
puts("Armor (stator) inductance la= 0.0375 Henry");
|
||||
puts("Flux gain (kf=k*flux) kf= 0.98 Wb");
|
||||
puts("Rotor inertia j= 0.011 kg*m2");
|
||||
puts("Load torque MC= 0.0 kg*m");
|
||||
|
||||
puts("");
|
||||
|
||||
puts("Press any key to start simulation...");
|
||||
getch();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,12 +1,13 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This work is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
#include <stdio.h>
|
103
scr/OUTDC.c
Normal file
103
scr/OUTDC.c
Normal file
@ -0,0 +1,103 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include "d2monplt.h"
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
extern double h,keep;
|
||||
extern char fbuf[80];
|
||||
extern int cnt,fd; /* x FILE LOW lEVEL DON'T FORGET THIS */
|
||||
extern struct data {
|
||||
char v[20];
|
||||
char i[20];
|
||||
};
|
||||
|
||||
/* LOCAL GLOBAL */
|
||||
char *FILE_NAME="dcrk";
|
||||
|
||||
ovmain()
|
||||
{
|
||||
|
||||
double nc,ia,w,xscale,iscale,wscale;
|
||||
int plotx,ploty,plotey,ic,px,py,;
|
||||
struct data rk;
|
||||
double atof(); /* DON'T FORGET THIS */
|
||||
char tx[4];
|
||||
/* inizialization */
|
||||
|
||||
px=(int)0.0;
|
||||
py=(int)0.0;
|
||||
ic=(int)0.0;
|
||||
xscale=(double)100.0; /* set x scale plot */
|
||||
iscale=(double)1.0; /* set y (ia(t)) scale plot */
|
||||
wscale=(double)0.5; /* set y (w(t)) scale plot */
|
||||
nc=(double)0.0;
|
||||
|
||||
|
||||
/* GRAPHICS SECTION */
|
||||
|
||||
/* Axes */
|
||||
for (px=0,py=96;px<=560;++px)
|
||||
{
|
||||
d2monoplot(px,py,py,1);
|
||||
} /*Axis X */
|
||||
|
||||
|
||||
/* for (px=0,py=0;py<=192;++py)
|
||||
{
|
||||
d2monoplot(px,py,py,1);
|
||||
} /* Axis Y */
|
||||
|
||||
|
||||
/* labels */
|
||||
monoplots("t=",505,36,1,'M');
|
||||
monoplots("ia(t)=",25,0,1,'M');
|
||||
monoplots("w(t)=",25,10,1,'M');
|
||||
|
||||
/* read first recort (header) for increment file pointr */
|
||||
open(FILE_NAME,O_RDONLY,0xC3);
|
||||
read(fd,fbuf,80);
|
||||
|
||||
/* Main Plot */
|
||||
for (ic=0;ic<cnt;ic++)
|
||||
{
|
||||
/* read data fromfile */
|
||||
read(fd,rk.v,20); /* read ia(t) */
|
||||
read(fd,rk.i,20); /* read w(t) */
|
||||
|
||||
/* save data in doubles */
|
||||
ia=(double)atof(rk.v);
|
||||
w=(double)atof(rk.i);
|
||||
|
||||
/* presentation data*/
|
||||
monoplots(rk.v,110,0,1,'M');
|
||||
monoplots(rk.i,110,10,1,'M');
|
||||
ftoa(nc,tx,2,1);
|
||||
monoplots(tx,535,36,1,'M');
|
||||
|
||||
/* plot graph of data*/
|
||||
plotx=(int)((nc*xscale)+1);
|
||||
ploty=(int)((-ia*iscale)+95);
|
||||
d2monoplot(plotx,ploty,ploty,1); /* ia(t) */
|
||||
ploty=(int)((-w*wscale)+97);
|
||||
d2monoplot(plotx,ploty,ploty,1); /* w(t) */
|
||||
|
||||
nc=nc+h;
|
||||
}
|
||||
close(fd);
|
||||
getch();
|
||||
|
||||
return 0;
|
||||
}
|
186
scr/OV1.C
Normal file
186
scr/OV1.C
Normal file
@ -0,0 +1,186 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
/* INPUT MODULE */
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#define micro 1.0e-6
|
||||
#define kilo 1.0e+3
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
extern double h,time,time2,sur,r,c,tau,yinit;
|
||||
extern char fbuf[80];
|
||||
extern int cnt,z;
|
||||
|
||||
/* LOCAL GLOBAL */
|
||||
char *title = "Runge Kutta and Runge Kutta Fehlberg integration methods";
|
||||
char *title1= "R-C circuit transient state simulation:";
|
||||
char *title2= "charge: [vc'(t)=(1/RC)*(E-vc(t))]- [i(t)=(E-vc(t))/R]";
|
||||
char *title3= "discharge: [vc'(t)=(1/RC)*(-vc(t))] - [i(t)=(-vc(t))/R]";
|
||||
char *title4= "in Aztec C for Apple II 128k series - by Greco Riccardo - Apple ][ Forever!";
|
||||
char *title5= "Thanks to Bill Buckels for all support - http://www.aztecmuseum.ca/ ";
|
||||
|
||||
char *out[8]={" RCCircuit h="," ts="," td="," E="," R="," C="," vc(0)="," cycles="}; /* always at global */
|
||||
|
||||
ovmain()
|
||||
{
|
||||
char buf[20],conv[4],rev;
|
||||
double atof(); /* DON'T FORGET THIS */
|
||||
|
||||
IN: /* set screen */
|
||||
#asm
|
||||
jsr $c300
|
||||
#endasm
|
||||
|
||||
buf[0]='\0'; /* buffers reset */
|
||||
fbuf[0]='\0';
|
||||
|
||||
/* Presentation */
|
||||
puts(title);
|
||||
puts(title1);
|
||||
puts(title2);
|
||||
puts(title3);
|
||||
puts(title4);
|
||||
puts(title5);
|
||||
puts(" ");
|
||||
|
||||
/* Input data */
|
||||
do{
|
||||
puts("Time step integration (Def. 0.01s) h [s] -> ");
|
||||
h=(double)atof(gets(buf));
|
||||
} while (h<0 || h<=0.005);
|
||||
|
||||
strcat(fbuf,out[0]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
do{
|
||||
puts("Time simulation charge (Def. 5) ts [s] -> ");
|
||||
time=(double)atof(gets(buf));
|
||||
} while (time<0);
|
||||
|
||||
strcat(fbuf,out[1]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
do{
|
||||
puts("Istant time to start discharge (Def. 2.5) (td<=ts) td [s] ->"); /* td istant time to discarge */
|
||||
time2=(double)atof(gets(buf));
|
||||
} while (time2<0 || time2>time);
|
||||
|
||||
strcat(fbuf,out[2]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
puts("Input Voltage value (Def. 50) E [Volt] -> "); /* Input step voltage E [V] */
|
||||
sur=(double)atof(gets(buf));
|
||||
|
||||
strcat(fbuf,out[3]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
do{
|
||||
puts("Resistence value (Def. 10) R [KiloOhm]-> "); /* Resistence R */
|
||||
r=(double)atof(gets(buf));
|
||||
} while (r<=0);
|
||||
|
||||
r=(double)(r*(kilo)); /* converting in Farad from here to the end */
|
||||
ftoa(r,buf,6,1);
|
||||
|
||||
strcat(fbuf,out[4]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
do{
|
||||
puts("Capacity value (Def. 20) C [micoFarad] -> "); /* Capacity C */
|
||||
c=(double)atof(gets(buf));
|
||||
} while (c<=0);
|
||||
|
||||
c=(double)(c*(micro)); /* converting in Farad from here to the end */
|
||||
ftoa(c,buf,6,1); /* new value of c in buf (only for show)*/
|
||||
|
||||
strcat(fbuf,out[5]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
puts("Initial charge condition (Def. 0) (@t=0s) vc(0) [Volt] -> "); /* Iniztial charge condition vc(0) */
|
||||
yinit=(double)atof(gets(buf));
|
||||
|
||||
strcat(fbuf,out[6]);
|
||||
strcat(fbuf,buf);
|
||||
|
||||
/* calc and set parameters */
|
||||
tau=(double)(r*c); /* Tau=R*C R [Ohm] C [Farad]*/
|
||||
/*cnt=(int)(time/h);*/
|
||||
|
||||
strcat(fbuf,out[7]);
|
||||
ftoa((time/h),buf,0,1); /* 0 decimal like int */
|
||||
strncpy(conv,buf,4); /* save cnt to conv first 4 digit*/
|
||||
strcat(fbuf,buf);
|
||||
|
||||
puts(fbuf); /* Input data Review */
|
||||
|
||||
puts("Do you want to review input data (Y/N)?");
|
||||
rev=getch();
|
||||
if (rev == 'Y' || rev == 'y') goto IN;
|
||||
|
||||
/* reset buffers */
|
||||
fbuf[0]='\0';
|
||||
buf[0]='\0';
|
||||
|
||||
/* set screen */
|
||||
scr_clear();
|
||||
scr_curs(0,0);
|
||||
|
||||
/* Simulation data review */
|
||||
puts("Simulation parameters set:");
|
||||
scr_curs(2,0);
|
||||
puts("Number of input cycles:");
|
||||
scr_curs(2,31);
|
||||
puts(conv);
|
||||
puts("Input voltage E=");
|
||||
scr_curs(3,31);
|
||||
puts(ftoa(sur,buf,4,1)); /* 14 digits in double precision */
|
||||
scr_curs(3,50);
|
||||
puts("V");
|
||||
puts("Init value vc(0)=");
|
||||
scr_curs(4,31);
|
||||
puts(ftoa(yinit,buf,4,1));
|
||||
scr_curs(4,50);
|
||||
puts("V");
|
||||
puts("Circuit time constant Tau=R*C=");
|
||||
scr_curs(5,31);
|
||||
puts(ftoa(tau,buf,14,1));
|
||||
scr_curs(5,50);
|
||||
puts("s");
|
||||
puts("Time step integration h=");
|
||||
scr_curs(6,31);
|
||||
puts(ftoa(h,buf,6,1));
|
||||
scr_curs(6,50);
|
||||
puts("s");
|
||||
puts("Time simulation ts = ");
|
||||
scr_curs(7,31);
|
||||
puts(ftoa(time,buf,4,1));
|
||||
scr_curs(7,50);
|
||||
puts("s");
|
||||
puts("");
|
||||
|
||||
puts("Do you want to plot exact solution too (Y/N)?");
|
||||
rev=getch();
|
||||
if (rev == 'Y' || rev == 'y') z=1;
|
||||
|
||||
rev='1';
|
||||
puts("Select the method [1]= RK4 - [2]= RKF(4)5");
|
||||
rev=getch();
|
||||
/*puts("Press any key to start simulation...");
|
||||
getch();*/
|
||||
if (rev=='2') return 2;
|
||||
else return 1;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
207
scr/OV2.C
Normal file
207
scr/OV2.C
Normal file
@ -0,0 +1,207 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
/* PROCESS MODULE */
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#define chk 1 /* set chk to 1 charge */
|
||||
#define rk4r 5.0e-3 /* RK4 max resolution h<rk4r */
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
extern double sur,r,c,tau,h,time,time2,yinit,keep;
|
||||
extern char fbuf[80];
|
||||
extern int cnt,fd;
|
||||
extern struct data {
|
||||
char v[20];
|
||||
char i[20];
|
||||
char ideal[20];
|
||||
};
|
||||
|
||||
/* LOCAL GLOBAL */
|
||||
char *outr[4]={"t="," vc(t)="," vcerr(t)="," i(t)="};
|
||||
char *FILE_NAME="rcrk";
|
||||
double swi;
|
||||
|
||||
/* Functions sets */
|
||||
int funrk(x,y,frk)
|
||||
double x,y,*frk;
|
||||
{
|
||||
frk[0] = (double)(((sur*swi)-y)/tau); /* RC Circuit Voltage frk[0]=vc'(t) Charge differential equation */
|
||||
} /* vc'(t)=(1/RC)*(E-vc(t)) charge*/
|
||||
/* vc'(t)=(1/RC)*(-vc(t)) discharge */
|
||||
double current(v)
|
||||
double(v);
|
||||
{
|
||||
return(((sur*swi)-v)/r); /* i(t)=(E-vc(t))/R charge*/
|
||||
} /* i(t)=(-vc(t))/R discharge */
|
||||
|
||||
double exact(x)
|
||||
double(x);
|
||||
{
|
||||
return (((yinit-sur)*exp(-x/tau))+sur); /* Exact solution charge vc(t)=(vc(0)-E)*exp(-t/RC)+E */
|
||||
}
|
||||
|
||||
double exact2(x)
|
||||
double (x);
|
||||
{
|
||||
return (keep*exp(-x/tau)); /* Exact solution discharge vc(t)=(vc(td))*exp(-t/RC) */
|
||||
}
|
||||
|
||||
/*RK4 Module */
|
||||
int runge_kutta(x,y,h,y3)
|
||||
double x,y,h,*y3;
|
||||
{
|
||||
double r1,r2,r3,r4,r5,h1,err,y3err,y3div,frk,frkerr;
|
||||
|
||||
/* RK inizialization */
|
||||
y3[0] = (double)0.0;
|
||||
y3err = (double)2.0;
|
||||
y3div = (double)6.0;
|
||||
frk = (double)0.0;
|
||||
frkerr = (double)0.5;
|
||||
err=(double)0.0;
|
||||
|
||||
do
|
||||
{
|
||||
h1=(double)0.5*h;
|
||||
|
||||
funrk(x,y,&frk); /* 1^ order */
|
||||
r1=h*frk;
|
||||
|
||||
funrk(x+h1,(double)y+frkerr*r1,&frk); /* 2^ order */
|
||||
r2=h*frk;
|
||||
|
||||
funrk(x+h1,(double)y+frkerr+r2,&frk); /* 3^ order */
|
||||
r3=h*frk;
|
||||
|
||||
funrk(x+h,(double)y+r3,&frk); /* 4^ order */
|
||||
r4=h*frk;
|
||||
|
||||
funrk(x+h,(double)y+r4,&frk); /* 5^ order */
|
||||
r5=h*frk;
|
||||
|
||||
err=fabs(r4-r5); /* etimate the error */
|
||||
|
||||
h=h/2;
|
||||
|
||||
}
|
||||
while (err>rk4r); /* RK4 max resolution */
|
||||
|
||||
/* y3 plus weighted average of operators 4^ order */
|
||||
y3[0] = (double)(y+(r1+y3err*r2+y3err*r3+r4)/y3div);
|
||||
|
||||
}
|
||||
|
||||
ovmain()
|
||||
{
|
||||
char buf[20],rev;/*conv[4]*/
|
||||
double atof(); /* DON'T FORGET THIS */
|
||||
double x,x2,nc,y3,y,errtx,cur,exa;
|
||||
struct data rk;
|
||||
|
||||
|
||||
/* inizialization */
|
||||
cnt=(int)0.0; /* Reset counter */
|
||||
y3 = (double)0.0;
|
||||
x=(double)0.0; /* time simulation */
|
||||
x2=(double)0.0; /* time discharge */
|
||||
keep=(double)0.0;
|
||||
|
||||
swi=(double)1.0; /* switch set to 1 = charge */
|
||||
y=yinit; /* set initial condition parameter vc(o) */
|
||||
|
||||
/* Start simulation */
|
||||
scr_clear();
|
||||
scr_curs(0,0);
|
||||
puts("Pocessing...");
|
||||
|
||||
/* open and write header data to file */
|
||||
open(FILE_NAME,O_WRONLY|O_APPEND,0xC3);
|
||||
write(fd,fbuf,80);
|
||||
|
||||
buf[0]='\0'; /* buffers reset */
|
||||
fbuf[0]='\0';
|
||||
|
||||
/* RK main Cycle */
|
||||
while (x<time) {
|
||||
|
||||
runge_kutta(x,y,h,&y3);
|
||||
cur=current(y3);
|
||||
|
||||
if (x <= time2)
|
||||
{
|
||||
exa=(double)exact(x);
|
||||
errtx=(double)fabs(y3-exa); /* exact solution in charge */
|
||||
}
|
||||
|
||||
if (x > time2)
|
||||
{
|
||||
if (chk)
|
||||
{
|
||||
swi=(double)0.0; /* switch set to 0 = discharge */
|
||||
keep=(double)y3; /* keep the value of vc(td) in the istant of discharge */
|
||||
!chk; /* NOT chk */
|
||||
}
|
||||
exa=(double)exact2(x2);
|
||||
errtx=(double)fabs(y3-exa); /* exact solution in discharge */
|
||||
x2=x2+h;
|
||||
}
|
||||
|
||||
/* store output in buffer */
|
||||
ftoa(x,buf,6,2);
|
||||
strcat(fbuf,outr[0]);
|
||||
strcat(fbuf,buf); /* store time (x) */
|
||||
|
||||
ftoa(y3,buf,6,2);
|
||||
strcat(fbuf,outr[1]);
|
||||
strcat(fbuf,buf); /* store voltage vc(t) (y3) */
|
||||
|
||||
ftoa(errtx,buf,6,2);
|
||||
strcat(fbuf,outr[2]);
|
||||
strcat(fbuf,buf); /* store voltage error vcerr(t) (errtx) */
|
||||
|
||||
ftoa(cur,buf,6,2);
|
||||
strcat(fbuf,outr[3]);
|
||||
strcat(fbuf,buf); /* store current i(t) (cur) */
|
||||
|
||||
|
||||
/* converting using max floating resolution */
|
||||
ftoa(y3,rk.v,14,1); /* vc(t) */
|
||||
ftoa(cur,rk.i,14,1); /* i(t) */
|
||||
ftoa(exa,rk.ideal,14,1); /* vc(t) exact solution */
|
||||
|
||||
puts(fbuf); /* output to screen */
|
||||
|
||||
/* write data result to file */
|
||||
write(fd,rk.v,20); /* write vc(t) */
|
||||
write(fd,rk.i,20); /* write i(t) */
|
||||
write(fd,rk.ideal,20); /* write vc(t) exact solution */
|
||||
|
||||
fbuf[0]='\0'; /* reset buffer */
|
||||
y=y3;
|
||||
x=x+h;
|
||||
cnt++; /* renew counter */
|
||||
|
||||
}
|
||||
close(fd); /* close the data file */
|
||||
|
||||
/*printf("Cycles number= %d",(cnt-1)); /* NOT YET itoa ? */
|
||||
|
||||
|
||||
puts("Press any key to start plotting...");
|
||||
getch();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
243
scr/OV2F.c
Normal file
243
scr/OV2F.c
Normal file
@ -0,0 +1,243 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
/* PROCESS MODULE */
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#define chk 1 /* set chk to 1 charge */
|
||||
#define rkfr 1.0e-6 /* RKF max resolution h<rk4r */
|
||||
/*#define hmax 0.25 /* need for adaptative step
|
||||
#define emax 20.0e-3
|
||||
#define emin 10.0e-3*/
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
extern double sur,r,c,tau,h,time,time2,yinit,keep;
|
||||
extern char fbuf[80];
|
||||
extern int cnt,fd;
|
||||
extern struct data {
|
||||
char v[20];
|
||||
char i[20];
|
||||
char ideal[20];
|
||||
};
|
||||
|
||||
/* LOCAL GLOBAL */
|
||||
char *outr[4]={"t="," vc(t)="," vcerr(t)="," i(t)="};
|
||||
char *FILE_NAME="rcrk";
|
||||
double swi;
|
||||
|
||||
/*Fehlberg parameters */
|
||||
double acca[5]={1.0/4.0,3.0/8.0,12.0/13.0,1.0,1.0/2.0};
|
||||
double erre1[5]={1.0/4.0,0.0,0.0,0.0,0.0};
|
||||
double erre2[5]={3.0/32.0,9.0/32.0,0.0,0.0,0.0};
|
||||
double erre3[5]={1932.0/2197.0,-7200.0/2197.0,7296.0/2197.0,0.0,0.0};
|
||||
double erre4[5]={439.0/216.0,-8.0,3680.0/513.0,-845.0/4104.0,0.0};
|
||||
double erre5[5]={-8.0/27.0,2.0,-3544.0/2565.0,1859.0/4104.0,-11.0/40.0};
|
||||
|
||||
double erre[5]={1.0/360.0,-128.0/4275.0,-2197.0/75240.0,1.0/50.0,2.0/55.0};
|
||||
|
||||
double ips5[5]={25.0/216.0,0.0,1408.0/2565.0,2197.0/4104.0,-1.0/5.0};
|
||||
/*double ips6[6]={16.0/135.0,0.0,6656.0/12825.0,28561.0/56430.0,-9.0/50.0,2.0/55.0};*/
|
||||
|
||||
|
||||
|
||||
/* Functions sets */
|
||||
int funrk(x,y,frk)
|
||||
double x,y,*frk;
|
||||
{
|
||||
frk[0] = (double)(((sur*swi)-y)/tau); /* RC Circuit Voltage frk[0]=vc'(t) Charge differential equation */
|
||||
} /* vc'(t)=(1/RC)*(E-vc(t)) charge*/
|
||||
/* vc'(t)=(1/RC)*(-vc(t)) discharge */
|
||||
double current(v)
|
||||
double(v);
|
||||
{
|
||||
return(((sur*swi)-v)/r); /* i(t)=(E-vc(t))/R charge*/
|
||||
} /* i(t)=(-vc(t))/R discharge */
|
||||
|
||||
double exact(x)
|
||||
double(x);
|
||||
{
|
||||
return (((yinit-sur)*exp(-x/tau))+sur); /* Exact solution charge vc(t)=(vc(0)-E)*exp(-t/RC)+E */
|
||||
}
|
||||
|
||||
double exact2(x)
|
||||
double (x);
|
||||
{
|
||||
return (keep*exp(-x/tau)); /* Exact solution discharge vc(t)=(vc(td))*exp(-t/RC) */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*RKF45 Module */
|
||||
int runge_kutta(x,y,h,y3)
|
||||
double x,y,h,*y3;
|
||||
{
|
||||
double r1,r2,r3,r4,r5,r6,frk,err; /*sigma,div,div2;
|
||||
|
||||
/* RK inizialization */
|
||||
y3[0] = (double)0.0;
|
||||
frk = (double)0.0;
|
||||
err=(double)0.0;
|
||||
|
||||
/* do
|
||||
{
|
||||
|
||||
/* 1^ order */
|
||||
funrk(x,y,&frk);
|
||||
r1=frk;
|
||||
/* 2^ order */
|
||||
funrk(x+acca[0]*h,(double)y+erre1[0]*r1*h,&frk);
|
||||
r2=frk;
|
||||
/* 3^ order */
|
||||
funrk(x+acca[1]*h,(double)y+erre2[0]*r1*h+erre2[1]*r2*h,&frk);
|
||||
r3=frk;
|
||||
/* 4^ order */
|
||||
funrk(x+acca[2]*h,(double)y+erre3[0]*r1*h+erre3[1]*r2*h+erre3[2]*r3*h,&frk);
|
||||
r4=frk;
|
||||
/* 5^ order */
|
||||
funrk(x+acca[3]*h,(double)y+erre4[0]*r1*h+erre4[1]*r2*h+erre4[2]*r3*h+erre4[3]*r4*h,&frk);
|
||||
r5=h*frk;
|
||||
|
||||
/* 6^ order only for estimated error for adaptative step method
|
||||
funrk(x+acca[4]*h,(double)y+erre5[0]*r1*h+erre5[1]*r2*h+erre5[2]*r3*h+erre5[3]*r4*h+erre5[4]*r5*h,&frk);
|
||||
r6=frk;
|
||||
|
||||
err=(fabs(erre[0]*r1+erre[1]*r3+erre[2]*r4+erre[3]*r5+erre[4]*r6)*h); /* estimate the error
|
||||
|
||||
h=h/2; /*non adaptative*/
|
||||
|
||||
/* adaptative step integration
|
||||
div=(double)(rk4r/err);
|
||||
div2=(double)pow(div,0.25);
|
||||
sigma=(double)(0.84*div2);
|
||||
|
||||
if (sigma<=0.1) h=0.1*h;
|
||||
else if (sigma>=4) h=4*h;
|
||||
else h=sigma*h;
|
||||
/* Alternative method
|
||||
if (err>=emax) h=h/2;
|
||||
if (err<=emin) h=h*2;
|
||||
|
||||
if (h>hmax) h=hmax; */
|
||||
/*
|
||||
}
|
||||
while (err>rkfr);
|
||||
*/
|
||||
|
||||
/* y3 weighted average of operators 5^ order */
|
||||
y3[0]=(double)(y+(ips5[0]*r1+ips5[2]*r3+ips5[3]*r4+ips5[4]*r5)*h);
|
||||
}
|
||||
|
||||
ovmain()
|
||||
{
|
||||
char buf[20],rev;
|
||||
double atof(); /* DON'T FORGET THIS */
|
||||
double x,x2,nc,y3,y,errtx,cur,exa;
|
||||
struct data rk;
|
||||
|
||||
|
||||
/* inizialization */
|
||||
cnt=(int)0.0; /* Reset counter */
|
||||
y3 = (double)0.0;
|
||||
x=(double)0.0; /* time in simulation */
|
||||
x2=(double)0.0; /* time in discharge */
|
||||
keep=(double)0.0;
|
||||
|
||||
swi=(double)1.0; /* switch set to 1 = charge */
|
||||
y=yinit; /* set initial condition parameter vc(o) */
|
||||
/*h=hmax; /* need for adaptative step */
|
||||
|
||||
/* Start simulation */
|
||||
scr_clear();
|
||||
scr_curs(0,0);
|
||||
puts("Pocessing...");
|
||||
|
||||
/* open and write header data to file */
|
||||
open(FILE_NAME,O_WRONLY|O_APPEND,0xC3);
|
||||
write(fd,fbuf,80);
|
||||
|
||||
buf[0]='\0'; /* buffers reset */
|
||||
fbuf[0]='\0';
|
||||
|
||||
/* RK main Cycle */
|
||||
while (x<time) {
|
||||
|
||||
runge_kutta(x,y,h,&y3);
|
||||
cur=current(y3);
|
||||
|
||||
if (x <= time2)
|
||||
{
|
||||
exa=(double)exact(x);
|
||||
errtx=(double)fabs(y3-exa); /* exact solution in charge */
|
||||
}
|
||||
|
||||
if (x > time2)
|
||||
{
|
||||
if (chk)
|
||||
{
|
||||
swi=(double)0.0; /* switch set to 0 = discharge */
|
||||
keep=(double)y3; /* keep the value of vc(td) in the istant of discharge */
|
||||
!chk; /* NOT chk */
|
||||
}
|
||||
exa=(double)exact2(x2);
|
||||
errtx=(double)fabs(y3-exa); /* exact solution in discharge */
|
||||
x2=x2+h; /* increment time in discharge */
|
||||
}
|
||||
|
||||
|
||||
/* store output in buffer */
|
||||
ftoa(x,buf,6,2);
|
||||
strcat(fbuf,outr[0]);
|
||||
strcat(fbuf,buf); /* store time (x) */
|
||||
|
||||
ftoa(y3,buf,6,2);
|
||||
strcat(fbuf,outr[1]);
|
||||
strcat(fbuf,buf); /* store voltage vc(t) (y3) */
|
||||
|
||||
ftoa(errtx,buf,6,2);
|
||||
strcat(fbuf,outr[2]);
|
||||
strcat(fbuf,buf); /* store voltage error vcerr(t) (errtx) */
|
||||
|
||||
ftoa(cur,buf,6,2);
|
||||
strcat(fbuf,outr[3]);
|
||||
strcat(fbuf,buf); /* store current i(t) (cur) */
|
||||
|
||||
/* converting using max floating resolution */
|
||||
ftoa(y3,rk.v,14,1); /* vc(t) */
|
||||
ftoa(cur,rk.i,14,1); /* i(t) */
|
||||
ftoa(exa,rk.ideal,14,1); /* vc(t) exact solution */
|
||||
|
||||
puts(fbuf); /* output to screen */
|
||||
|
||||
/* write data result to file */
|
||||
write(fd,rk.v,20); /* write vc(t) */
|
||||
write(fd,rk.i,20); /* write i(t) */
|
||||
write(fd,rk.ideal,20); /* write vc(t) exact solution */
|
||||
|
||||
fbuf[0]='\0'; /* reset buffer */
|
||||
|
||||
y=y3;
|
||||
x=x+h; /* increment time in simulation */
|
||||
cnt++; /* renew counter */
|
||||
|
||||
}
|
||||
close(fd); /* close the data file */
|
||||
|
||||
/*printf("Cycles number= %d",(cnt-1)); /* NOT YET itoa ?*/
|
||||
|
||||
puts("Press any key to start plotting...");
|
||||
getch();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
123
scr/OV3.c
Normal file
123
scr/OV3.c
Normal file
@ -0,0 +1,123 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include "d2monplt.h"
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
extern double h,keep;
|
||||
extern char fbuf[80];
|
||||
extern int cnt,fd,z; /* x FILE LOW lEVEL DON'T FORGET THIS */
|
||||
extern struct data {
|
||||
char v[20];
|
||||
char i[20];
|
||||
char ideal[20];
|
||||
};
|
||||
|
||||
/* LOCAL GLOBAL */
|
||||
char *FILE_NAME="rcrk";
|
||||
|
||||
ovmain()
|
||||
{
|
||||
|
||||
double nc,vr,ir,exa,xscale,iscale,vscale;
|
||||
int plotx,ploty,plotey,ic,px,py;
|
||||
struct data rk;
|
||||
double atof(); /* DON'T FORGET THIS */
|
||||
char tx[4];
|
||||
/* inizialization */
|
||||
|
||||
px=(int)0.0;
|
||||
py=(int)0.0;
|
||||
ic=(int)0.0;
|
||||
xscale=(double)100.0; /* set x scale plot */
|
||||
iscale=(double)2500.0; /* set y (i(t)) scale plot */
|
||||
vscale=(double)1.0; /* set y (vc(t)) scale plot */
|
||||
nc=(double)0.0;
|
||||
|
||||
|
||||
/* GRAPHICS SECTION */
|
||||
|
||||
/* Axes */
|
||||
for (px=0,py=96;px<=560;++px)
|
||||
{
|
||||
d2monoplot(px,py,py,1);
|
||||
/*d2monoplot(px,py-sur,py-sur,2); /* final voltage value E */
|
||||
} /*Axis X */
|
||||
|
||||
/*
|
||||
for (px=0,py=0;py<=192;++py)
|
||||
{
|
||||
d2monoplot(px,py,py,1);
|
||||
} /* Axis Y */
|
||||
|
||||
/* labels */
|
||||
monoplots("t=",505,36,1,'M');
|
||||
monoplots("v(t)=",25,0,1,'M');
|
||||
monoplots("i(t)=",25,10,1,'M');
|
||||
|
||||
/* read first recort (header) for increment file pointr */
|
||||
open(FILE_NAME,O_RDONLY,0xC3);
|
||||
read(fd,fbuf,80);
|
||||
|
||||
/* Main Plot */
|
||||
for (ic=0;ic<cnt;ic++)
|
||||
{
|
||||
/* read data fromfile */
|
||||
read(fd,rk.v,20); /* read vc(t) */
|
||||
read(fd,rk.i,20); /* read i(t) */
|
||||
read(fd,rk.ideal,20); /* read exact vc(t) */
|
||||
|
||||
/* save data in doubles */
|
||||
vr=(double)atof(rk.v);
|
||||
ir=(double)atof(rk.i);
|
||||
exa=(double)atof(rk.ideal);
|
||||
|
||||
/* presentation data*/
|
||||
monoplots(rk.v,110,0,1,'M');
|
||||
monoplots(rk.i,110,10,1,'M');
|
||||
ftoa(nc,tx,2,1);
|
||||
monoplots(tx,535,36,1,'M');
|
||||
|
||||
/* plot graph of data*/
|
||||
plotx=(int)((nc*xscale)+1);
|
||||
ploty=(int)((-vr*vscale)+95);
|
||||
d2monoplot(plotx,ploty,ploty,1); /* vc(t) */
|
||||
ploty=(int)((-ir*iscale)+97);
|
||||
d2monoplot(plotx,ploty,ploty,1); /* i(t) */
|
||||
if (z) {
|
||||
|
||||
plotey=(int)((-exa*vscale)+95);
|
||||
d2monoplot(plotx,plotey,plotey,1); /* plot charge exact solution in double pixel */
|
||||
d2monoplot(plotx+1,plotey,plotey,1);
|
||||
}
|
||||
/* if (chk=1)
|
||||
{
|
||||
plotey=(int)(-exact(nc)+95); /* Exact solution in charge
|
||||
}
|
||||
else
|
||||
{
|
||||
plotey=(int)(-exact2(nc,keep)+95); /* Exact solution in discharge
|
||||
}
|
||||
|
||||
d2monoplot(plotx,plotey,plotey,1); /* plot charge exact solution in double pixel
|
||||
d2monoplot(plotx+1,plotey,plotey,1);
|
||||
*/
|
||||
nc=nc+h;
|
||||
}
|
||||
close(fd);
|
||||
getch();
|
||||
|
||||
return 0;
|
||||
}
|
103
scr/P8FILE.c
Normal file
103
scr/P8FILE.c
Normal file
@ -0,0 +1,103 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <prodir.h>
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
|
||||
extern int fd; /* x FILE LOW lEVEL DON'T FORGET THIS */
|
||||
|
||||
/* LOCAL GLOBAL */
|
||||
|
||||
char *FILE_NAME1="rcrk";
|
||||
char *FILE_NAME2="dcrk";
|
||||
|
||||
/* pointing to unused memory at 4192 for a buffer
|
||||
if this gives problems change to something else */
|
||||
struct fileinfo *fi = (struct fileinfo *)4192;
|
||||
|
||||
int p8create(name, maintype, subtype )
|
||||
char *name;
|
||||
unsigned maintype,subtype;
|
||||
{
|
||||
int fh;
|
||||
|
||||
/* overwrite file if it already exists - set all access */
|
||||
if((fh=open(name, O_WRONLY|O_TRUNC|O_CREAT,0xc3)) == -1) return fh;
|
||||
/* close it and get prodos to change the filetype for you */
|
||||
close(fh);
|
||||
|
||||
/* get the fileinfo from ProDOS */
|
||||
if (getfinfo (name, fi) == -1) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* set the file info to whatever you wish -
|
||||
you can set other info as well using this call sequence */
|
||||
fi->file_type = (unsigned char)maintype;
|
||||
fi->aux_type = subtype;
|
||||
setfinfo(name,fi);
|
||||
|
||||
/* open the file again */
|
||||
fh=open(name, O_WRONLY,0xc3);
|
||||
/* return the open handle */
|
||||
return fh;
|
||||
}
|
||||
|
||||
ovmain(filename)
|
||||
unsigned filename;
|
||||
{
|
||||
|
||||
switch(filename)
|
||||
{
|
||||
case 1:
|
||||
/* create or or owerwrite output file BIN format */
|
||||
fd=p8create(FILE_NAME1,6,0);
|
||||
break;
|
||||
case 2:
|
||||
/* create or or owerwrite output file BIN format */
|
||||
fd=p8create(FILE_NAME2,6,0);
|
||||
}
|
||||
|
||||
if (fd == -1)
|
||||
{ if (errno == EACCES)
|
||||
{
|
||||
puts("unable to access file");
|
||||
getch();
|
||||
/*_exit();*/
|
||||
}
|
||||
else if (errno == ENOENT)
|
||||
{
|
||||
puts("unable to open file");
|
||||
getch();
|
||||
/*_exit();*/
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("open error number");
|
||||
puts(errno);
|
||||
getch();
|
||||
/*_exit();*/
|
||||
}
|
||||
}
|
||||
else if (fd==-2)
|
||||
{ puts("do not get fileinfo");
|
||||
}
|
||||
close(fd);
|
||||
|
||||
/*getch(); */
|
||||
return 0;
|
||||
}
|
||||
|
92
scr/RCRK.C
Normal file
92
scr/RCRK.C
Normal file
@ -0,0 +1,92 @@
|
||||
/* Copyright (C) 2013 Riccardo Greco rigreco.grc@gmail.com.
|
||||
*
|
||||
* This project is based on 1999-2000 Thesis work of Greco Riccardo.
|
||||
* It implement an Runge Kutta 4(5)^ order integration numerical method of differential equations set
|
||||
* by use of double precision floating point operation in Aztec C65 language.
|
||||
* It allow to simulate different mathematical models such as:
|
||||
* Resistance Capacitor electrical circuit, Direct Current electric motor,
|
||||
* Alternative Current three phase induction motor.
|
||||
*
|
||||
* Thanks to Bill Buckels for his invaluable support:
|
||||
* Aztec C compilers http://www.aztecmuseum.ca/compilers.htm
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
/* GENERAL GLOBAL */
|
||||
|
||||
double h,time,time2,sur,r,c,tau,yinit,keep;
|
||||
char fbuf[80];
|
||||
int cnt,fd,fd2,z;
|
||||
struct data {
|
||||
char v[20];
|
||||
char i[20];
|
||||
char ideal[20];
|
||||
};
|
||||
|
||||
int clearhgr(i)
|
||||
int i;
|
||||
{
|
||||
|
||||
/* Funzioni globali devono essere in modulo principale per essere condivisi
|
||||
tra sovrapposizioni */
|
||||
if (i!=911)
|
||||
{return 0;}
|
||||
maintoaux (0,0,0);
|
||||
black();
|
||||
hgr();
|
||||
|
||||
}
|
||||
|
||||
main()
|
||||
{
|
||||
int a,b,c;
|
||||
z=0;
|
||||
b=0;
|
||||
c=0;
|
||||
clearhgr(0);
|
||||
IN:
|
||||
a=ovloader("mainm");
|
||||
switch (a){
|
||||
case 0:
|
||||
_exit();
|
||||
break;
|
||||
case 1:
|
||||
b=ovloader("ov1"); /* input module */
|
||||
if (b==2) {
|
||||
ovloader("p8file",1); /* create or owerwrite work file */
|
||||
ovloader("ov2f"); /* process module by RKF45*/
|
||||
}
|
||||
else {
|
||||
ovloader("p8file",1); /* create or owerwrite work file rcrk*/
|
||||
ovloader("ov2"); /* process module by RK4*/
|
||||