-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain1d.c
More file actions
118 lines (95 loc) · 2.59 KB
/
domain1d.c
File metadata and controls
118 lines (95 loc) · 2.59 KB
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
#include <stdlib.h>
#include <string.h>
#include "fd-solver.h"
#include "datafile.h"
struct Domain1D* CreateDomain1D(char *name,
int width,
double dx,
double dt,
int NumVars,
int times)
{
struct Domain1D *domain;
struct Node1D *PrevNode;
int i;
PrevNode = NULL;
domain = (struct Domain1D*) calloc(1, sizeof(struct Domain1D));
domain->Name = (char*) calloc(80, sizeof(char));
strncpy(domain->Name, name, 80);
domain->NumNodes = width;
domain->dt = dt;
domain->Nodes = (struct Node1D**) calloc(times, sizeof(struct Node1D*));
for(i=0; i<width; i++) {
domain->Nodes[i] = CreateNode1D(dx, dt, NumVars, times);
domain->Nodes[i]->Prev = PrevNode;
domain->Nodes[i]->NodeNum = i;
domain->Nodes[i]->varlst = domain->varlst;
if(PrevNode)
PrevNode->Next = domain->Nodes[i];
PrevNode = domain->Nodes[i];
}
domain->NumTimes = times;
domain->varlst = new_var();
strcpy(domain->varlst->name, "Root");
domain->varlst->value = 2.71828183;
return domain;
}
void DestroyDomain1D(struct Domain1D *domain)
{
int i;
if(domain) {
if(domain->Name)
free(domain->Name);
if(domain->Nodes) {
for(i=0; i<domain->NumNodes; i++)
DestroyNode1D(domain->Nodes[i]);
free(domain->Nodes);
}
destroy_list(domain->varlst);
free(domain);
}
}
int UpdateDomain(struct Domain1D *domain)
{
int i;
struct NodeStack *UpdateLater;
struct Node1D *node;
UpdateLater = NULL;
if(domain->TimeIndex+1 == domain->NumTimes) {
return 1;
}
for(i = 0; i < domain->NumNodes; i++) {
if(UpdateNode(domain->Nodes[i]) == 1)
UpdateLater = Push(UpdateLater, domain->Nodes[i]);
}
while(UpdateLater) {
node = UpdateLater->Node;
UpdateNode(node);
node->TimeIndex++;
UpdateLater = Pop(UpdateLater);
}
domain->TimeIndex++;
return 0;
}
void DomainApplyInitialCondition(struct Domain1D *domain, int Var, double value)
{
int i;
for(i = 0; i < domain->NumNodes; i++) {
NodeApplyInitialCondition(domain->Nodes[i], Var, value);
}
}
struct NodeStack* Push(struct NodeStack *stack, struct Node1D *node)
{
struct NodeStack *tmp;
tmp = (struct NodeStack*) calloc(1, sizeof(struct NodeStack));
stack->Node = node;
stack->Next = stack;
return tmp;
}
struct NodeStack* Pop(struct NodeStack *stack)
{
struct NodeStack *tmp;
tmp = stack->Next;
free(stack);
return tmp;
}