PhaseFieldConvert.h 4.33 KB
Newer Older
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
/** \file PhaseFieldConvert.h */

#ifndef PHASE_FIELD_CONVERT_H
#define PHASE_FIELD_CONVERT_H

using namespace AMDiS;

struct SignedDistFctToPhaseField : AbstractFunction<double, WorldVector<double> >
{
  SignedDistFctToPhaseField(double epsilon_, 
			    AbstractFunction<double,WorldVector<double> > *dist_, 
			    double scalingFactor_ = 1.0/sqrt(2.0))
  : AbstractFunction<double, WorldVector<double> >(6), 
    epsilon(epsilon_), 
    dist(dist_),
    scalingFactor(scalingFactor_) {};

  double operator()(const WorldVector<double> &x) const
  {
    return 0.5 * (1.0 - tanh(scalingFactor * (*dist)(x) / epsilon));
  }
        
private:

  double epsilon;
  AbstractFunction<double,WorldVector<double> > *dist;
  double scalingFactor;
};

struct SignedDistFctListToPhaseField : AbstractFunction<double, WorldVector<double> >
{
  SignedDistFctListToPhaseField(double epsilon_, 
				std::vector<AbstractFunction<double,WorldVector<double> >*> dist_, 
				double scalingFactor_ = 1.0/sqrt(2.0))
  : AbstractFunction<double, WorldVector<double> >(6), 
    epsilon(epsilon_), 
    dist(dist_),
    scalingFactor(scalingFactor_) {};

  double operator()(const WorldVector<double> &x) const
  {
    double d = 1.e12;
    for (size_t i = 0; i < dist.size(); ++i) {
      d = std::min((*(dist[i]))(x), d);
    }
    return 0.5 * (1.0 - tanh(scalingFactor * d / epsilon));
  }
        
private:

  double epsilon;
  std::vector<AbstractFunction<double,WorldVector<double> >*> dist;
  double scalingFactor;
};


struct SignedDistList : AbstractFunction<double, WorldVector<double> >
{
  SignedDistList(std::vector<AbstractFunction<double,WorldVector<double> >*> dist_)
  : AbstractFunction<double, WorldVector<double> >(1), 
    dist(dist_) {};

  double operator()(const WorldVector<double> &x) const
  {
    d.setX(x);
    return std::for_each(dist.begin(), dist.end(), d);
  }

private:  
  struct MyMax {
    mutable double val;
    mutable WorldVector< double > x;
    MyMax()
    : val(-1.0e12) {}
    
    void operator()(const AbstractFunction<double,WorldVector<double> >* v) const
    {
      val = max((*v)(x), val);
    }
    
    void setX(const WorldVector<double> &x_) const
    {
      x= x_;
    }
    
    inline operator double() { return val; }
  } d;
  
  std::vector<AbstractFunction<double,WorldVector<double> >*> dist;
};

struct PhaseFieldToSignedDist : AbstractFunction<double, double>
{
  PhaseFieldToSignedDist(double epsilon_= -1.0, double scalingFactor_ = 1.0/sqrt(2.0))
  : AbstractFunction<double, double>(6), 
    epsilon(epsilon_),
    scalingFactor(scalingFactor_)
  {
    if (epsilon < 0.0)
      Parameters::get("mesh->refinement->epsilon", epsilon);
  }
  
  double operator()(const double &phi) const
  {
    double z = std::max(-1.0 + 1.e-10, std::min(1.0 - 1.e-10, -phi));
    return (epsilon/scalingFactor) * atanh(z);
  }
  
private:
  double epsilon;
  double scalingFactor;
};

struct SignedDistToPhaseField : AbstractFunction<double, double>
{
  SignedDistToPhaseField(double epsilon_ = -1.0, double scalingFactor_ = 1.0/sqrt(2.0))
  : AbstractFunction<double, double>(6),
    epsilon(epsilon_),
    scalingFactor(scalingFactor_)
  {
    if (epsilon < 0.0)
      Parameters::get("mesh->refinement->epsilon", epsilon);
  }
  
  double operator()(const double &dist) const
  {
    return 0.5 * (1.0 - tanh(scalingFactor * dist / epsilon));
  }
  
private:
  double epsilon;
  double scalingFactor;
};

struct SignedDistToCh : AbstractFunction<double, double>
{
  SignedDistToCh(double epsilon_ = -1.0, double scalingFactor_ = 1.0/sqrt(2.0))
  : AbstractFunction<double, double>(6),
    epsilon(epsilon_),
    scalingFactor(scalingFactor_)
  {
    if (epsilon < 0.0)
      Parameters::get("mesh->refinement->epsilon", epsilon);
  }
  
  double operator()(const double &dist) const
  {
    return -tanh(scalingFactor * dist / epsilon);
  }
  
private:
  double epsilon;
  double scalingFactor;
};

struct ChToPhaseField : AbstractFunction<double, double>
{
  ChToPhaseField() : AbstractFunction<double, double>(1) {};
  double operator()(const double &c) const
  {
    return std::max(0.0, std::min(1.0, 0.5 * (c + 1.0)));
  }
};

struct PhaseFieldToCh : AbstractFunction<double, double>
{
  PhaseFieldToCh() : AbstractFunction<double, double>(1) {};
  double operator()(const double &c) const
  {
    return std::max(-1.0, std::min(1.0, 2.0 * c - 1.0));
  }
};

#endif // PHASE_FIELD_CONVERT_H