Skip to content

Commit

Permalink
Rework C program
Browse files Browse the repository at this point in the history
  • Loading branch information
mammatus95 committed Sep 17, 2024
1 parent 4cd2103 commit e105d6d
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 196 deletions.
24 changes: 24 additions & 0 deletions ccode/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CC=gcc
CSTD=-std=c11
CFLAGS=-c -Wall
CWARN=-Wextra -Wpedantic
SPEED=-O
OMP=-fopenmp
#DEBUG=-g

OBJ_lorenz=lorenz.o lorenzlib.o
OBJ_lorenzdiv=lorenzdiv.o lorenzlib.o
ALL=lorenz lorenzdiv

all: $(ALL)
lorenz: $(OBJ_lorenz)
$(CC) -o $@ $^ $(CSTD) $(CWARN)

lorenzdiv: $(OBJ_lorenzdiv)
$(CC) -o $@ $^ $(CSTD) $(OMP)

%.o: %.cpp
$(CC) -o $@ $< $(CSTD) $(OMP) $(CFLAGS) $(SPEED)

clean:
rm -rf *.o $(ALL)
24 changes: 24 additions & 0 deletions ccode/globals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

//**************************************************************************************************
// globals.h
// COPYRIGHT (c) 2024 by Mammatuscloud
// All rights reserved
//**************************************************************************************************


#ifndef GLOBALS_H
#define GLOBALS_H 1

// global variables
// general defines
#define N 10000
#define DeltaT 0.01
#define DeltaT2 0.005

// model constants
#define R 28.0 // Rayleigh number
#define SIG 10.0 // Prandtl number
#define B 8/3 // Aspekte number

#endif

Binary file added ccode/lorenz
Binary file not shown.
106 changes: 10 additions & 96 deletions ccode/lorenz.c
Original file line number Diff line number Diff line change
@@ -1,111 +1,25 @@
/*Lorenz Attroktor*/
//**************************************************************************************************
// Lorenz Attroctor
// lorenz.c
// COPYRIGHT (c) 2024 by Mammatuscloud
// All rights reserved
//**************************************************************************************************

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lorenz.h"

#define N 10000
#define DeltaT 0.01
#define DeltaT2 0.005
#define R 28.0 /*Rayleigh Zahl*/
#define SIG 10.0 /*Prandtl - Zahl*/
#define B 8/3 /*Aspekt - Zahl*/

float max_time (void){
return N*DeltaT;
}

/*Velocity*/
double x_func( double x, double y){
/*
This function estimate the time derivation of the Velocity (so it is acceleration).
*/
return (-SIG*x + SIG*y);
}

/*Temperature*/
double y_func(double x, double y, double z){
/*
This function estimate the time derivation of the Temprature.
*/
return (-y + R*x - x*z);
}

/*Instability (Temperaturschichtung)*/
double z_func(double x,double y,double z){
/*
This function estimate the time derivation of the Instability.
*/
return (-B*z + x*y);
}

/*conserved quantities*/
double h_func(double y,double z){
return 0.5*y*y + 0.5*z*z - R*z;
}

double c_func(double x, double z){
return 0.5*x*x - SIG*z;
}

void runge_kutta(double* X, double* Y, double* Z){
/*
Parameters :
------------
X : Velocity as double pointer
Y : Temperature as double pointer
Z : Instability as double pointer
Returns:
--------
nothing
*/

double k1_x=0, k1_y=0, k1_z=0, k2_x=0, k2_y=0, k2_z=0, k3_x=0, k3_y=0, k3_z=0, k4_x=0, k4_y=0, k4_z=0;

k1_x = x_func( (*X), (*Y) );
k1_y = y_func( (*X), (*Y), (*Z));
k1_z = z_func( (*X), (*Y), (*Z));

k2_x = x_func( (*X)+(DeltaT2*k1_x), (*Y)+(DeltaT2*k1_y) );
k2_y = y_func( (*X)+(DeltaT2*k1_x), (*Y)+(DeltaT2*k1_y), (*Z)+(DeltaT2*k1_z));
k2_z = z_func( (*X)+(DeltaT2*k1_x), (*Y)+(DeltaT2*k1_y), (*Z)+(DeltaT2*k1_z));

k3_x = x_func( (*X)+(DeltaT2*k2_x), (*Y)+(DeltaT2*k2_y));
k3_y = y_func( (*X)+(DeltaT2*k2_x), (*Y)+(DeltaT2*k2_y), (*Z)+(DeltaT2*k2_z));
k3_z = z_func( (*X)+(DeltaT2*k2_x), (*Y)+(DeltaT2*k2_y), (*Z)+(DeltaT2*k2_z));

k4_x = x_func( (*X)+(DeltaT*k3_x), (*Y)+(DeltaT*k3_y));
k4_y = y_func( (*X)+(DeltaT*k3_x), (*Y)+(DeltaT*k3_y), (*Z)+(DeltaT*k3_z));
k4_z = z_func( (*X)+(DeltaT*k3_x), (*Y)+(DeltaT*k3_y), (*Z)+(DeltaT*k3_z));

(*X) = (*X) + (DeltaT/6.0) * (k1_x +2*k2_x + 2*k3_x + k4_x);
(*Y) = (*Y) + (DeltaT/6.0) * (k1_y +2*k2_y + 2*k3_y + k4_y);
(*Z) = (*Z) + (DeltaT/6.0) * (k1_z +2*k2_z + 2*k3_z + k4_z);

return;
}

void errofile (FILE *datei) {
if( (datei=fopen("test.txt","w+")) == NULL) {
fprintf(stderr, "Kann Datei nicht oeffnen\n");
exit(-1);
}
return;
}

/*Write output in a file*/
void fileoutput (double *X, double* Y, double* Z, FILE *data){
fprintf(data,"%.10lf;", (*X));
fprintf(data,"%.10lf;", (*Y));
fprintf(data,"%.10lf\n", (*Z));
return;
}

}

int main (void){

int i = 0;
double X=10,Y=0,Z=0;
double X=10, Y=0, Z=0;
//errofile (datei);

X = 10.0;
Expand All @@ -116,6 +30,6 @@ int main (void){
runge_kutta( &X, &Y, &Z);
i++;
}
printf("%.10lf" , X);
printf("%.10lf\n" , X);
return 0;
}
21 changes: 21 additions & 0 deletions ccode/lorenz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//**************************************************************************************************
// Lorenz Attroctor
// lorenz.h
// COPYRIGHT (c) 2024 by Mammatuscloud
// All rights reserved
//**************************************************************************************************

#ifndef LORENZ_H
#define LORENZ_H 1

#include "globals.h"
#include "lorenzlib.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// prototypes
void errofile (FILE *datei);

#endif
Binary file added ccode/lorenz.o
Binary file not shown.
Binary file added ccode/lorenzdiv
Binary file not shown.
115 changes: 15 additions & 100 deletions ccode/lorenz_div.c → ccode/lorenzdiv.c
Original file line number Diff line number Diff line change
@@ -1,91 +1,11 @@
/*Lorenz Attroktor*/
//**************************************************************************************************
// Lorenz Attroctor
// lorenzdiv.c
// COPYRIGHT (c) 2024 by Mammatuscloud
// All rights reserved
//**************************************************************************************************

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>


#define N 10000
#define DeltaT 0.01
#define DeltaT2 0.005
#define R 28.0 /*Rayleigh Zahl*/
#define SIG 10.0 /*Prandtl - Zahl*/
#define B 8/3 /*Aspekt - Zahl*/

float max_time (void){
return N*DeltaT;
}

/*Velocity*/
double x_func( double x, double y){
/*
This function estimate the time derivation of the Velocity (so it is acceleration).
*/
return (-SIG*x + SIG*y);
}

/*Temperature*/
double y_func(double x, double y, double z){
/*
This function estimate the time derivation of the Temprature.
*/
return (-y + R*x - x*z);
}

/*Instability (Temperaturschichtung)*/
double z_func(double x,double y,double z){
/*
This function estimate the time derivation of the Instability.
*/
return (-B*z + x*y);
}

/*conserved quantities*/
double h_func(double y,double z){
return 0.5*y*y + 0.5*z*z - R*z;
}

double c_func(double x, double z){
return 0.5*x*x - SIG*z;
}

void runge_kutta(double* X, double* Y, double* Z){
/*
Parameters :
------------
X : Velocity as double pointer
Y : Temperature as double pointer
Z : Instability as double pointer
Returns:
--------
nothing
*/

double k1_x=0, k1_y=0, k1_z=0, k2_x=0, k2_y=0, k2_z=0, k3_x=0, k3_y=0, k3_z=0, k4_x=0, k4_y=0, k4_z=0;

k1_x = x_func( (*X), (*Y) );
k1_y = y_func( (*X), (*Y), (*Z));
k1_z = z_func( (*X), (*Y), (*Z));

k2_x = x_func( (*X)+(DeltaT2*k1_x), (*Y)+(DeltaT2*k1_y) );
k2_y = y_func( (*X)+(DeltaT2*k1_x), (*Y)+(DeltaT2*k1_y), (*Z)+(DeltaT2*k1_z));
k2_z = z_func( (*X)+(DeltaT2*k1_x), (*Y)+(DeltaT2*k1_y), (*Z)+(DeltaT2*k1_z));

k3_x = x_func( (*X)+(DeltaT2*k2_x), (*Y)+(DeltaT2*k2_y));
k3_y = y_func( (*X)+(DeltaT2*k2_x), (*Y)+(DeltaT2*k2_y), (*Z)+(DeltaT2*k2_z));
k3_z = z_func( (*X)+(DeltaT2*k2_x), (*Y)+(DeltaT2*k2_y), (*Z)+(DeltaT2*k2_z));

k4_x = x_func( (*X)+(DeltaT*k3_x), (*Y)+(DeltaT*k3_y));
k4_y = y_func( (*X)+(DeltaT*k3_x), (*Y)+(DeltaT*k3_y), (*Z)+(DeltaT*k3_z));
k4_z = z_func( (*X)+(DeltaT*k3_x), (*Y)+(DeltaT*k3_y), (*Z)+(DeltaT*k3_z));

(*X) = (*X) + (DeltaT/6.0) * (k1_x +2*k2_x + 2*k3_x + k4_x);
(*Y) = (*Y) + (DeltaT/6.0) * (k1_y +2*k2_y + 2*k3_y + k4_y);
(*Z) = (*Z) + (DeltaT/6.0) * (k1_z +2*k2_z + 2*k3_z + k4_z);

return;
}
#include "lorenzdiv.h"

void errofile (FILE *datei) {
if( (datei=fopen("test.txt","w+")) == NULL) {
Expand All @@ -95,24 +15,19 @@ void errofile (FILE *datei) {
return;
}

/*Write output in a file*/
void fileoutput (double *X, double* Y, double* Z, FILE *data){

fprintf(data,"%.10lf;", (*X));
fprintf(data,"%.10lf;", (*Y));
fprintf(data,"%.10lf\n", (*Z));
return;
}

float j=0,k=0;


int main (void){

int i = 0;
double X=10,Y=0,Z=0;
double X=10, Y=0, Z=0;
//errofile (datei);

#pragma omp parallel
{
int thread_num = omp_get_thread_num();
printf("Hello from thread %d\n", thread_num);
}

#pragma omp parallel for schedule (static) private (j,k)
for (j=0.1;j<=0.1;j+=0.01){
for (k=0.1;k<=0.1;k+=0.01){
Expand Down
26 changes: 26 additions & 0 deletions ccode/lorenzdiv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//**************************************************************************************************
// Lorenz Attroctor Divergence
// lorenzdiv.h
// COPYRIGHT (c) 2024 by Mammatuscloud
// All rights reserved
//**************************************************************************************************

#ifndef LORENZDIV_H
#define LORENZDIV_H 1

#include "globals.h"
#include "lorenzlib.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>

// prototypes
void errofile (FILE *datei);

// global variables
float j=0, k=0;


#endif
Binary file added ccode/lorenzdiv.o
Binary file not shown.
Loading

0 comments on commit e105d6d

Please sign in to comment.