summaryrefslogtreecommitdiff
path: root/hyp2mat/lib/exec_stackup.cc
blob: 5680cd58afba2a9b0c205a91c5685bbf6fbeccde (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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * hyp2mat - convert hyperlynx files to matlab scripts
 * Copyright 2012 Koen De Vleeschauwer.
 *
 * This file is part of hyp2mat.
 *
 * This program 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/>.
 */

#include <iterator>
#include <iostream>
#include <algorithm>
#include "hypfile.h"

using namespace std;
using namespace HypFile;

/*
 * Hyperlynx 'STACKUP' record. 
 * Defines pcb layers.
 */

/*
 * Hyperlynx 'OPTIONS' subrecord of STACKUP record. 
 * Defines dielectric constant and loss tangent of metal layers. 
 */

bool HypFile::Hyp::exec_options(parse_param& h)
{
  if (trace_hyp) cerr << "options: use_die_for_metal = " << h.use_die_for_metal << endl;
  use_die_for_metal = h.use_die_for_metal;

  return false;
}

/*
 * Hyperlynx 'SIGNAL' subrecord of 'STACKUP' record. 
 * Defines a pcb signal layer.
 */

bool HypFile::Hyp::exec_signal(parse_param& h)
{
  if (trace_hyp)
    cerr << "signal:";
  trace_layer(h);

  add_metal_layer(h);

  /* 
   *The difference between signal and plane layers is 
   * - signal layers are drawn positive, plane layers are drawn negative
   * - signal layers need a name
   */

  stackup.back().layer_type = LAYER_SIGNAL;

  if (!h.layer_name_set)
    error("missing signal layer name" );

  calc_layer_position();
  calc_layer_epsilon();

  return false;
}

/*
 * Hyperlynx 'DIELECTRIC' subrecord of 'STACKUP' record. 
 * Defines a pcb dielectric layer.
 */

bool HypFile::Hyp::exec_dielectric(parse_param& h)
{
  if (trace_hyp)
    cerr << "dielectric:";
  trace_layer(h);

  add_dielectric_layer(h);

  calc_layer_position();
  calc_layer_epsilon();

  return false;
}

/*
 * Hyperlynx 'PLANE' subrecord of 'STACKUP' record. 
 * Defines a pcb power or signal plane. 
 */

bool HypFile::Hyp::exec_plane(parse_param& h)
{
  if (trace_hyp)
    cerr << "plane:";
  trace_layer(h);

  add_metal_layer(h);

  stackup.back().layer_type = LAYER_PLANE;

  calc_layer_position();
  calc_layer_epsilon();

  return false;
}

/*
 * Debug output for layers
 */

bool HypFile::Hyp::trace_layer(parse_param& h)
{
  if (trace_hyp) {
    if (h.thickness_set) cerr << " thickness = " << h.thickness;
    if (h.plating_thickness_set) cerr << " plating_thickness = " << h.plating_thickness;
    if (h.bulk_resistivity_set) cerr << " bulk_resistivity = " << h.bulk_resistivity;
    if (h.temperature_coefficient_set) cerr << " temperature_coefficient = " << h.temperature_coefficient;
    if (h.epsilon_r_set) cerr << " epsilon_r = " << h.epsilon_r;
    if (h.loss_tangent_set) cerr << " loss_tangent = " << h.loss_tangent;
    if (h.conformal_set) cerr << " conformal = " << h.conformal;
    if (h.prepreg_set) cerr << " prepreg = " << h.prepreg;
    if (h.layer_name_set) cerr << " layer_name = " << h.layer_name;
    if (h.material_name_set) cerr << " material_name = " << h.material_name;
    if (h.plane_separation_set) cerr << " plane_separation = " << h.plane_separation;
    cerr << endl;
    }

  return true;
}

/*
 * Add a signal or plane layer.
 */

bool HypFile::Hyp::add_metal_layer(parse_param& h)
{
  Layer l;

  l.layer_type = LAYER_SIGNAL;

  if (h.thickness_set)
    l.thickness = h.thickness * metal_thickness_unit;
  else {
    error ("thickness missing");
    return true;
    }
 
  if (h.plating_thickness_set)
    l.plating_thickness = h.plating_thickness * metal_thickness_unit;
  else
    l.plating_thickness = 0;

  if (h.bulk_resistivity_set)
    l.bulk_resistivity = h.bulk_resistivity;
  else 
    l.bulk_resistivity = copper_bulk_resistivity;

  if (h.temperature_coefficient_set)
    l.temperature_coefficient = h.temperature_coefficient;
  else
    l.temperature_coefficient = copper_temperature_coefficient;

  if (h.conformal_set) {
    error("only dielectric layers conformal");
    return true;
    }
  else
    l.conformal = false;

  if (h.epsilon_r_set)
    l.epsilon_r = h.epsilon_r;
  else
    l.epsilon_r = fr4_epsilon_r;

  if (h.loss_tangent_set)
    l.loss_tangent = h.loss_tangent;
  else
    l.loss_tangent = fr4_loss_tangent;

  // use use_die_for_metal ? XXX

  if (h.prepreg_set) {
    error("only dielectric layers prepreg");
    return true;
    }
  else
    l.prepreg = false;

  if (h.layer_name_set)
    l.layer_name = h.layer_name;
  else {
    error("missing signal layer name" );
    l.layer_name = ""; // Continue anyhow
    }

  if (h.material_name_set)
    l.material_name = h.material_name;
  else
    l.material_name = "";

  if (h.plane_separation_set)
    l.plane_separation = h.plane_separation * unit;
  else
    l.plane_separation = board.plane_separation;

  // Add layer to stackup
  stackup.push_back(l);

  return false;
}

/*
 * add a substrate layer
 */

bool HypFile::Hyp::add_dielectric_layer(parse_param& h)
{

  Layer l;

  l.layer_type = LAYER_DIELECTRIC;

  if (h.thickness_set)
    l.thickness = h.thickness * unit;
  else {
    error ("thickness missing");
    return true;
    }
 
  if (h.plating_thickness_set) {
    error ("dielectric has no plating thickness");
    return true;
    }
  else
    l.plating_thickness = 0;

  if (h.bulk_resistivity_set) {
    error ("dielectric has no bulk resistivity");
    return true;
    }
  else 
    l.bulk_resistivity = 0;

  if (h.temperature_coefficient_set) {
    error ("dielectric has no bulk resistivity temperature coefficient");
    return true;
    }
  else
    l.temperature_coefficient = 0;

  if (h.conformal_set)
    l.conformal = h.conformal;
  else
    l.conformal = false;

  if (h.epsilon_r_set)
    l.epsilon_r = h.epsilon_r;
  else if (l.conformal)
    l.epsilon_r = conformal_epsilon_r;
  else
    l.epsilon_r = fr4_epsilon_r;

  if (h.loss_tangent_set)
    l.loss_tangent = h.loss_tangent;
  else
    l.loss_tangent = fr4_loss_tangent;

  if (h.prepreg_set)
    l.prepreg = h.prepreg;
  else
    l.prepreg = true;

  if (h.layer_name_set)
    l.layer_name = h.layer_name;
  else
    l.layer_name = "";

  if (h.material_name_set)
    l.material_name = h.material_name;
  else
    l.material_name = "";

  if (h.plane_separation_set) {
    error ("dielectric has no plane separation");
    return true;
    }
  else
    l.plane_separation = 0;

  // Add layer to stackup
  stackup.push_back(l);

  return false;
}

 /*
  * Recalculate vertical position of all layers.
  * Layers are ordered top first, bottom last.
  * Called when a new layer is added.
  */

bool HypFile::Hyp::calc_layer_position()
{
  // calculate vertical position, from bottom to top
  double z_top = 0; // current vertical position
  for (LayerList::reverse_iterator it = stackup.rbegin(); it != stackup.rend(); ++it) {
    if (it->layer_type == LAYER_DIELECTRIC) {
      /* substrate and prepreg */
      it->z0 = z_top;
      it->z1 = z_top + it->thickness;
      z_top = it->z1;
      }
    else {
      /* metal layers */
      it->z0 = z_top;
      it->z1 = z_top;
      }
    }

  return true;
}

bool HypFile::Hyp::calc_layer_epsilon()
{

  /* no calculation needed if dielectric constant have been specified */
  if (use_die_for_metal)
    return true;

  /* calculate dielectric constants */
  for (LayerList::iterator i = stackup.begin(); i != stackup.end(); ++i) {

    /* skip dielectric layers */
    if (i->layer_type == LAYER_DIELECTRIC)
      continue;

    /* find first dielectric layer above current metal layer */
    bool upper_layer_found = false;
    LayerList::iterator upper_layer;
    for (LayerList::iterator j = stackup.begin(); j != i; ++j)
      if (j->layer_type == LAYER_DIELECTRIC) {
        upper_layer_found = true;
        upper_layer = j;
        };

    /* find first dielectric layer below current metal layer */
    bool lower_layer_found = false;
    LayerList::iterator lower_layer;
    for (LayerList::iterator j = i; j != stackup.end(); ++j)
      if (j->layer_type == LAYER_DIELECTRIC) {
        lower_layer_found = true;
        lower_layer = j;
        break;
        };

    if (!upper_layer_found || !lower_layer_found) {
      /* assume outer copper layers are in air */
      i->epsilon_r = 1.0;
      i->loss_tangent = 0.0;
      }
    else if (upper_layer->prepreg && !lower_layer->prepreg) {
      /* upper layer is prepreg. use values of upper layer */
      i->epsilon_r = upper_layer->epsilon_r;
      i->loss_tangent = upper_layer->loss_tangent;
      }
    else if (!upper_layer->prepreg && lower_layer->prepreg) {
      /* lower layer is prepreg. use values of lower layer */
      i->epsilon_r = lower_layer->epsilon_r;
      i->loss_tangent = lower_layer->loss_tangent;
      } 
    else {
      /* use average of upper and lower layer */
      i->epsilon_r = (upper_layer->epsilon_r + lower_layer->epsilon_r)/2;
      i->loss_tangent = (upper_layer->loss_tangent + lower_layer->loss_tangent) /2;
      }
    }

  return false;
}

/*
 * Flood specified layers with copper 
 */

void HypFile::Hyp::flood_layers_with_copper()
{

  /* check if we have to flood all plane layers */
  bool flood_plane_layers = std::find(flood_layers.begin(), flood_layers.end(), "plane_layers") != flood_layers.end(); /* look for special value "plane_layers" */

  for (LayerList::iterator l = stackup.begin(); l != stackup.end(); ++l) {
   
    /* metallic layers only */
    if (l->layer_type == LAYER_DIELECTRIC) continue;

    /* check if we have to flood this layer */
    bool flood_this_layer = std::find(flood_layers.begin(), flood_layers.end(), l->layer_name) != flood_layers.end();

    /* flood with copper if we have to flood this layer, or if this is a plane layer and we have to flood all plane layers */
    if (!(flood_this_layer || (flood_plane_layers && (l->layer_type == LAYER_PLANE)))) continue;

    /* create default copper on plane layer */
    Polygon default_copper;
    default_copper.vertex = board.edge.front().vertex; /* polygon is board outline */
    default_copper.polygon_type = POLYGON_TYPE_PLANE;
    default_copper.net_name = l->layer_name; /* flood layer VCC with copper belonging to net VCC, layer GND with copper belonging to net GND */
    default_copper.id = -1;
    default_copper.positive = true;
    default_copper.width = 0.0;
    default_copper.plane_separation = -1.0;
    default_copper.left_plane_separation = -1.0;
    default_copper.layer_name = l->layer_name;

    /* Look up net with same name as layer */
    NetList::iterator n;
    for (n = net.begin(); n != net.end(); ++n)
      if (n->net_name == l->layer_name) {
        add_polygon_to_net(*n, default_copper);
        break;
        }

    /* Net with same name as layer not found. Create net */
    if (n == net.end()) {
      Net plane_net;
      plane_net.net_name = l->layer_name;
      plane_net.plane_separation = -1.0;
      net.push_back(plane_net);
      add_polygon_to_net(net.back(), default_copper);
      }

    }

  return;
}

/* not truncated */