aboutsummaryrefslogtreecommitdiff
path: root/src/Ell_Register.c
blob: 2bc2ae0f0655a93fef782be7d57fb19d11eb30a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 /*@@
   @header    Ell_Register.h
   @date      
   @author    Gerd Lanferman
   @desc 
   Registration routines for elliptic classes and solvers   
   @enddesc 
   @version $Header$
 @@*/

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

#include "cctk.h"
#include "cctk_Parameters.h"

#include "EllBase.h"

#include "StoreNamedData.h"

static pNamedData *EqNameDB;

int Ell_RegisterSolver(void (*function),  
		       const char *sname, 
		       const char *eqname); 

int Ell_RegisterEq(void *(function)(const char *, void*), const char *eqname);


/* Ell_RegisterEq takes a routine ("function") and registers that routine 
   under the name "eqname" in the EqNameDB"
   Application: Call Ell_Register with the routine that registers a solver 
   for a elliptic equation class. */


int Ell_RegisterEq(void *(function)(const char *, void*), const char *eqname) 
{

  DECLARE_CCTK_PARAMETERS

  int retval = ELL_FAILURE;
  /* Register if function not already there with this name */

  if (!GetNamedData(EqNameDB, eqname))
  {

    if (StoreNamedData(&EqNameDB, eqname, (void*)function)==0)
    {
      if CCTK_EQUALS(elliptic_verbose,"yes") 
      {
	char *msg;
	msg = (char *)malloc( (200+strlen(eqname))*sizeof(char) );
	sprintf(msg,"Registered elliptic class: %s",eqname);
	CCTK_INFO(msg);
	free(msg);
      }
      retval = ELL_SUCCESS;
    }
    else
    {
      char *msg;
      msg = (char *)malloc( (200+strlen(eqname))*sizeof(char) );
      sprintf(msg,"Failed to register elliptic class: %s",eqname);
      CCTK_INFO(msg);
      free(msg);
    }
  }
  else
  {
    char *msg;
    msg = (char *)malloc( (200+strlen(eqname))*sizeof(char) );
    sprintf(msg,"Elliptic class %s already registered",eqname);
    CCTK_WARN(0,msg);
    free(msg);
    retval = ELL_CLASSEXISTS;
  }

  return retval;

  

}

/* Ell_RegisterSolver takes a routine ("function") and registers that 
   routine under the name  "sname" with the database specified by "eqname".
   So, how do we get to the database, after all it needs to be hardcoded
   as pNamedData ? Well, we know its name and in Ell_RegisterEq we have 
   registered a function under the equation class name. 
   We now get that function and use that function to register the solver.
   Sounds confusing, well it is. The advantage is, that somebody can come 
   up with a new equation class and can keep the database (the pNamedData
   declaration) in his own routine and does not have to put it in a central
   place. Amen*/

int Ell_RegisterSolver(void (*function),  
		       const char *sname, 
		       const char *eqname) 
{

  DECLARE_CCTK_PARAMETERS

  int retval=ELL_FAILURE;
  int ierr;
  int (*fn)(void *, const char *);

  fn = (int(*)(void (*function), const char *sname))
    GetNamedData(EqNameDB, eqname);

  if (fn)
  {

    ierr = fn(function,sname);

    if (ierr==ELL_SUCCESS)
    {

      if CCTK_EQUALS(elliptic_verbose,"yes") 
      {
	char *msg;
	msg = (char *)malloc((200+strlen(eqname)+strlen(sname))*sizeof(char));
	sprintf(msg,"Registered elliptic solver %s for %s",sname,eqname);
	CCTK_INFO(msg);
	free(msg);
      }
      
      retval = ELL_SUCCESS;
    }
    else if (ierr==ELL_SOLVEREXISTS)
    {
      char *msg;
      msg = (char *)malloc((200+strlen(eqname)+strlen(sname))*sizeof(char));
      sprintf(msg,"Registered second solver %s for %s",sname,eqname);
      CCTK_WARN(0,msg);
      free(msg);

      retval = ELL_SOLVEREXISTS;
    }
    else 
    {
      char *msg;
      msg = (char *)malloc((200+strlen(eqname)+strlen(sname))*sizeof(char));
      sprintf(msg,"Failed to register solver %s for %s",sname,eqname);
      CCTK_WARN(1,msg);
      free(msg);
    }

  }
  else
  {
    CCTK_WARN(0,"Cannot get function in EqName");
    retval = ELL_NOCLASS;
  }
    
  return retval; 

  

}