1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-06-30 00:44:52 +02:00
qmckl/src/qmckl_context.org

4.2 KiB

Context

This file is written in C because it is more natural to express the context in C than in Fortran.

#include <stdlib.h>   /* malloc */
#include "qmckl_context.h"

Context

The context variable is a handle for the state of the library, and is stored in the following data structure, which can't be seen outside of the library.

#define QMCKL_DEFAULT_PRECISION 53
#define QMCKL_DEFAULT_RANGE     2

/* 64-bit integer */
typedef long long int qmckl_context ;
typedef struct qmckl_context_struct_ {
struct qmckl_context_struct_ * prev;
int precision;
int range;
} qmckl_context_struct;

qmckl_context_create

To create a new context, use qmckl_context_create(). If the creation failed, the function returns 0. On success, a pointer to a context is returned as a 64-bit integer.

qmckl_context qmckl_context_create();
qmckl_context qmckl_context_create() {

qmckl_context_struct*  context;

context = (qmckl_context_struct*) malloc (sizeof(qmckl_context_struct));
if (context == NULL) {
 return (qmckl_context) 0;
}

context->prev      = NULL;
context->precision = QMCKL_DEFAULT_PRECISION;
context->range     = QMCKL_DEFAULT_RANGE;

return (qmckl_context) context;
}

qmckl_context_copy

qmckl_context qmckl_context_copy(qmckl_context context);
qmckl_context qmckl_context_copy(qmckl_context context) {

qmckl_context_struct* old_context;
qmckl_context_struct* new_context;

new_context = (qmckl_context_struct*) malloc (sizeof(qmckl_context_struct));
if (new_context == NULL) {
 return (qmckl_context) 0;
}

old_context = (qmckl_context_struct*) context;

new_context->prev      = old_context;
new_context->precision = old_context->precision;
new_context->range     = old_context->range;

return (qmckl_context) new_context;
}

Precision

The following functions set and get the expected required precision and range. precision should be an integer between 2 and 53, and range should be an integer between 2 and 11. The setter functions functions return a new context as a 64-bit integer. The getter functions return the value, as a 32-bit integer.

qmckl_context_set_precision

qmckl_context qmckl_context_set_precision(qmckl_context context, int precision);
qmckl_context qmckl_context_set_precision(qmckl_context context, int precision) {
qmckl_context_struct* ctx;

if (precision <  2) return (qmckl_context) 0;
if (precision > 53) return (qmckl_context) 0;

ctx = (qmckl_context_struct*) qmckl_context_copy(context);
ctx->precision = precision;
return (qmckl_context) ctx;
}

qmckl_context_set_range

qmckl_context qmckl_context_set_range(qmckl_context context, int range);
qmckl_context qmckl_context_set_range(qmckl_context context, int range) {
qmckl_context_struct* ctx;

if (range <  2) return (qmckl_context) 0;
if (range > 11) return (qmckl_context) 0;

ctx = (qmckl_context_struct*) qmckl_context_copy(context);
ctx->range = range;
return (qmckl_context) ctx;
}

qmckl_context_get_precision

int qmckl_context_get_precision(qmckl_context context);
int qmckl_context_get_precision(qmckl_context context) {
qmckl_context_struct* ctx;
ctx = (qmckl_context_struct*) context;
return ctx->precision;
}

qmckl_context_get_range

int qmckl_context_get_range(qmckl_context context);
int qmckl_context_get_range(qmckl_context context) {
qmckl_context_struct* ctx;
ctx = (qmckl_context_struct*) context;
return ctx->range;
}