[32a023]: / obsolete / bin / codeCreator.py  Maximize  Restore  History

Download this file

432 lines (351 with data), 13.3 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
'''
Copyright (C) 2013 - Juan Pablo Carbajal
Copyright (C) 2013 - Marco Vassallo
This progrm is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
# Author: Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
# Modified by: Marco Vassallo
from io import open
from string import Template
class Makefile ():
def __init__(self,**kwargs):
""" Creates a makefile with the names of the files to be compiled.
Inuts must be all strings.
"""
if map(type,kwargs.values()) != [str]*len(kwargs):
raise ValueError("Makefile accepts only str arguments.")
self.name = "Makefile_"+kwargs["name"]
self.body = Template ("""
DIR = $path
CPPFLAGS=
LIBS= -ldolfin
MKOCTFILE = mkoctfile
FFC = ffc
OCTFILES = fem_fs_$name.oct
all : $(OCTFILES)
fs : fem_fs_$name.oct
rhs : fem_rhs_$name.oct
lhs : fem_lhs_$name.oct
fem_fs_$name.o: fem_fs_$name.cc
$(MKOCTFILE) -I$(DIR) -I. $(CPPFLAGS) $(LDFLAGS) $< -c $@
fem_fs_$name.oct: $name.h fem_fs_$name.o
$(MKOCTFILE) -s fem_fs_$name.o $(DIR)fem_init_env.o $(LDFLAGS) $(LIBS) -o $@
fem_rhs_$name.o: fem_rhs_$name.cc
$(MKOCTFILE) -I$(DIR) -I. $(LDFLAGS) $(CPPFLAGS) $< -c $@
fem_rhs_$name.oct: $name.h fem_rhs_$name.o
$(MKOCTFILE) -s fem_rhs_$name.o $(DIR)fem_init_env.o $(LDFLAGS) $(LIBS) -o $@
fem_lhs_$name.o: fem_lhs_$name.cc
$(MKOCTFILE) -I$(DIR) -I. $(LDFLAGS) $(CPPFLAGS) $< -c $@
fem_lhs_$name.oct: $name.h fem_lhs_$name.o
$(MKOCTFILE) -s fem_lhs_$name.o $(DIR)fem_init_env.o $(LDFLAGS) $(LIBS) -o $@
$name.h: $name.ufl
$(FFC) -l dolfin $name.ufl
.PHONY: clean
clean:
rm -f fem_fs_$name.o fem_fs_$name.cc $name.h
rm -f fem_rhs_$name.o fem_rhs_$name.cc
rm -f fem_lhs_$name.o fem_lhs_$name.cc
rm -f $makefile
""").safe_substitute(name=kwargs["name"],
makefile=self.name,
path=kwargs["path"])
def writeout (self):
mfile = open (self.name, 'w')
mfile.write(unicode(self.body))
mfile.close()
class Functionspace ():
def __init__(self,**kwargs):
""" Creates a file fem_fs_$name with the name of the Variational problem to.
Inuts must be all strings.
"""
if map(type,kwargs.values()) != [str]*len(kwargs):
raise ValueError("Functionspace accepts only str arguments.")
self.name = "fem_fs_"+kwargs["name"]+".cc"
self.body = Template ("""
#include "functionspace.h"
#include "mesh.h"
#include "$name.h"
DEFUN_DLD (fem_fs_$name, args, , "initialize a fs from a mesh declared with fem_init_mesh")
{
int nargin = args.length ();
octave_value retval;
if (nargin < 1 || nargin > 1)
print_usage ();
else
{
if (! mesh_type_loaded)
{
mesh::register_type ();
mesh_type_loaded = true;
mlock ();
}
if (args(0).type_id () == mesh::static_type_id ())
{
const mesh & msho = static_cast<const mesh&> (args(0).get_rep ());
const dolfin::Mesh & mshd = msho.get_msh ();
boost::shared_ptr <const dolfin::FunctionSpace> g (new $name::FunctionSpace (mshd));
if (! functionspace_type_loaded)
{
functionspace::register_type ();
functionspace_type_loaded = true;
mlock ();
}
retval = new functionspace(g);
}
}
return retval;
}
""").safe_substitute(name=kwargs["name"])
def writeout (self):
mfile = open (self.name, 'w')
mfile.write(unicode(self.body))
mfile.close()
class RHS ():
def __init__(self,**kwargs):
""" Creates a file fem_rhs_$name with the name of the Variational problem to.
Inuts must be all strings.
"""
if map(type,kwargs.values()) != [str]*len(kwargs):
raise ValueError("RHS accepts only str arguments.")
self.name = "fem_rhs_"+kwargs["name"]+".cc"
self.body = Template ("""
#include "$name.h"
#include "functionspace.h"
#include "boundarycondition.h"
#include "coefficient.h"
#include "function.h"
DEFUN_DLD (fem_rhs_$name, args, , "A = fem_rhs_$name (FUNCTIONAL SPACE, COEFF, BC)")
{
int nargin = args.length ();
octave_value retval;
if (nargin < 1)
print_usage ();
else
{
if (! functionspace_type_loaded)
{
functionspace::register_type ();
functionspace_type_loaded = true;
mlock ();
}
if (args(0).type_id () == functionspace::static_type_id ())
{
const functionspace & fspo
= static_cast<const functionspace&> (args(0).get_rep ());
if (! error_state)
{
const dolfin::FunctionSpace V = fspo.get_fsp ();
$name::BilinearForm a (V, V);
std::size_t ncoef = a.num_coefficients (), nc = 0;
if (! coefficient_type_loaded)
{
coefficient::register_type ();
coefficient_type_loaded = true;
mlock ();
}
if (! function_type_loaded)
{
function::register_type ();
function_type_loaded = true;
mlock ();
}
for (std::size_t i = 1; i < nargin; ++i)
{
if (args(i).type_id () == coefficient::static_type_id ())
{
const coefficient & cf
= static_cast <const coefficient&> (args(i).get_rep ());
std::size_t n = a.coefficient_number (cf.get_str ());
const boost::shared_ptr<const expression> & pexp = cf.get_expr ();
a.set_coefficient (n, pexp);
++nc;
}
if (args(i).type_id () == function::static_type_id ())
{
const function & fun
= static_cast <const function&> (args(i).get_rep ());
std::size_t n = a.coefficient_number (fun.get_str ());
const boost::shared_ptr<const dolfin::Function> & pfun = fun.get_pfun ();
a.set_coefficient (n, pfun);
++nc;
}
}
if (nc != ncoef)
error ("Wrong number of coefficient");
else
{
dolfin::Matrix A;
dolfin::assemble (A, a);
if (! boundarycondition_type_loaded)
{
boundarycondition::register_type ();
boundarycondition_type_loaded = true;
mlock ();
}
for (std::size_t i = 1; i < nargin; ++i)
{
if (args(i).type_id () == boundarycondition::static_type_id ())
{
const boundarycondition & bc
= static_cast<const boundarycondition&> (args(i).get_rep ());
const std::vector<boost::shared_ptr <const dolfin::DirichletBC> > & pbc
= bc.get_bc ();
for (std::size_t j = 0; j < pbc.size (); ++j)
pbc[j]->apply(A);
}
}
int nr = A.size (0), nc = A.size (1);
// nz shoud be estimated in a better way
int nz = nr * nc;
SparseMatrix sm (nr, nc, nz);
int ii = 0;
sm.cidx (0) = 0;
for (int j = 0; j < nc; ++j)
{
for (int i = 0; i < nr; i++)
{
double tmp = A(i, j);
if (tmp != 0.)
{
sm.data(ii) = tmp;
sm.ridx(ii) = i;
ii++;
}
}
sm.cidx(j+1) = ii;
}
sm.maybe_compress ();
retval = sm;
}
}
}
}
return retval;
}
""").safe_substitute(name=kwargs["name"])
def writeout (self):
mfile = open (self.name, 'w')
mfile.write(unicode(self.body))
mfile.close()
class LHS ():
def __init__(self,**kwargs):
""" Creates a file fem_lhs_$name with the name of the Variational problem to.
Inuts must be all strings.
"""
if map(type,kwargs.values()) != [str]*len(kwargs):
raise ValueError("lhs accepts only str arguments.")
self.name = "fem_lhs_"+kwargs["name"]+".cc"
self.body = Template ("""
#include "$name.h"
#include "functionspace.h"
#include "boundarycondition.h"
#include "coefficient.h"
#include "function.h"
DEFUN_DLD (fem_lhs_$name, args, , " b = fem_lhs_$name (FUNCTIONAL SPACE, COEFF, BC)")
{
int nargin = args.length ();
octave_value retval;
if (nargin < 1)
print_usage ();
else
{
if (! functionspace_type_loaded)
{
functionspace::register_type ();
functionspace_type_loaded = true;
mlock ();
}
if (args(0).type_id () == functionspace::static_type_id ())
{
const functionspace & fspo
= static_cast<const functionspace&> (args(0).get_rep ());
if (! error_state)
{
const dolfin::FunctionSpace V = fspo.get_fsp ();
$name::LinearForm L (V);
std::size_t ncoef = L.num_coefficients (), nc = 0;
if (! coefficient_type_loaded)
{
coefficient::register_type ();
coefficient_type_loaded = true;
mlock ();
}
if (! function_type_loaded)
{
function::register_type ();
function_type_loaded = true;
mlock ();
}
for (std::size_t i = 1; i < nargin; ++i)
{
if (args(i).type_id () == coefficient::static_type_id ())
{
const coefficient & cf
= static_cast <const coefficient&> (args(i).get_rep ());
std::size_t n = L.coefficient_number (cf.get_str ());
const boost::shared_ptr<const expression> & pexp = cf.get_expr ();
L.set_coefficient (n, pexp);
++nc;
}
if (args(i).type_id () == function::static_type_id ())
{
const function & fun
= static_cast <const function&> (args(i).get_rep ());
std::size_t n = L.coefficient_number (fun.get_str ());
const boost::shared_ptr<const dolfin::Function> & pfun = fun.get_pfun ();
L.set_coefficient (n, pfun);
++nc;
}
}
if (nc != ncoef)
error ("Wrong number of coefficient");
else
{
dolfin::Vector b;
dolfin::assemble (b, L);
if (! boundarycondition_type_loaded)
{
boundarycondition::register_type ();
boundarycondition_type_loaded = true;
mlock ();
}
for (std::size_t i = 1; i < nargin; ++i)
{
if (args(i).type_id () == boundarycondition::static_type_id ())
{
const boundarycondition & bc
= static_cast<const boundarycondition&> (args(i).get_rep ());
const std::vector<boost::shared_ptr <const dolfin::DirichletBC> > & pbc
= bc.get_bc ();
for (std::size_t j = 0; j < pbc.size (); ++j)
pbc[j]->apply(b);
}
}
dim_vector dims;
dims.resize (2);
dims(0) = b.size ();
dims(1) = 1;
Array<double> myb (dims);
for (std::size_t i = 0; i < b.size (); ++i)
myb(i) = b[i];
retval = myb;
}
}
}
}
return retval;
}
""").safe_substitute(name=kwargs["name"])
def writeout (self):
mfile = open (self.name, 'w')
mfile.write(unicode(self.body))
mfile.close()